These are chat archives for rust-lang/rust

28th
May 2018
F001
@F001
May 28 2018 03:10
Hello , I need some helps about the internal of rustc. I want to get the function signature of an "use item". Below are my steps:
  1. I can get a rustc::hir::Item_::ItemUse(P<Path>, UseKind)
  2. From Path, I matched a hir::def::Def::Fn(DefId) to detect whether the item is an fn or not
    The question is, how can I get the function signature from the DefId? This function might come from an external crate.
Farzeen
@thefzsalam
May 28 2018 07:59
Why doesn't String implement Drop?
I'm trying to write a function similar to this and got this error: https://play.rust-lang.org/?gist=5bf8afd3c4b5bf7d4fbb20a81a40f1e7&version=stable&mode=debug
Michal 'vorner' Vaner
@vorner
May 28 2018 08:16
Drop is somewhat crazy trait. You just don't put it into the bounds, you can call drop(stuff) on anything.
The fact if there's special code running at the top-level of the type at drop-time (which is what implementing Drop means) is no indication of what runs over the whole thing.
(my guess is that String is actually a Vec<u8> in disguise and the Drop is implemented on that)
Ravi Shankar
@wafflespeanut
May 28 2018 08:20
Also worth noting that mem::drop is not bound to Drop
Kelly Thomas Kline
@kellytk
May 28 2018 08:52
I'm attempting to build Rust code on macOS for FreeBSD using cross but it's failing. (https://ghostbin.com/paste/n3omj) Per https://github.com/japaric/cross I've installed cross, Docker, and I run cross build --target x86_64-unknown-freebsd from the project directory. What mistake am I making?
Christian Witts
@ChristianWitts
May 28 2018 08:57
according to the cross docs, it only runs on Linux hosts
Kelly Thomas Kline
@kellytk
May 28 2018 09:01
I missed "Disclaimer: Only works on a x86_64 Linux host", pardon me. Are any other options available to me?
Kelly Thomas Kline
@kellytk
May 28 2018 10:31
Where do the checksums for crates in the [metadata] section of the Cargo.lock file come from? I'd like to see if backtrace-sys 0.1.21 compiles for FreeBSD to isolate when the regression occured
Farzeen
@thefzsalam
May 28 2018 14:25
In this declaration: struct Foo<'a>(), whose lifetime does 'a denote?
red75prime
@red75prime
May 28 2018 14:42
it is unused
Farzeen
@thefzsalam
May 28 2018 15:00

static variables created inside generic functions are shared across all variations of type parameter.
https://play.rust-lang.org/?gist=99f2a1c2b4fd34ec2098d4e8e54d2bf1&version=stable&mode=debug

Is this the expected behaviour?
Doesn't rust generate two different functions and name-mangle them for a generic function in the below example:

fn test<T: Display>(t: T) {
   println!("{}", t);
}

fn main() {
    test::<u8>(123);
    test::<u16>(123);
}
Zakarum
@omni-viral
May 28 2018 15:01
@thefzsalam What if type of the variable depends on generic parameter?
Farzeen
@thefzsalam
May 28 2018 15:01
i don't get you @omni-viral
Zakarum
@omni-viral
May 28 2018 15:02
fn foo<T> () { static mut VARIABLE: Option<T> = None; }
Hm. This construction is invalid
Farzeen
@thefzsalam
May 28 2018 15:03
Oh..
Why is that invalid?
Zakarum
@omni-viral
May 28 2018 15:03
Because you can't make use generic paramter for some things
Like new type declarations
And static variable declarations
Farzeen
@thefzsalam
May 28 2018 15:04
:(
Zakarum
@omni-viral
May 28 2018 15:04
fn foo<T>() { struct X { x: T} } is also invalid
Since static variables are independent from generic parameters it makes sence for monomorphized functions to use the same one
Farzeen
@thefzsalam
May 28 2018 15:06
I need to cache a variable in a generic function so that subsequent calls return the same value without computation.
This value is different for different types.
Any way to achieve this?
Oh I think a HashMap with TypeId as keys might work. Let me try that.
Is there a better way?
Zakarum
@omni-viral
May 28 2018 15:08
Can you add trait bound to the generic parameter?
Farzeen
@thefzsalam
May 28 2018 15:16
I was about to ask that :D
I am not sure if I can add 'static to T
what does T:'static mean?
TypeId needs that to work
Hans W. Uhlig
@huhlig
May 28 2018 15:18
T:'static means the type lifetime must live for the duration of the program
Farzeen
@thefzsalam
May 28 2018 15:19

:point_up: May 28, 2018 7:55 PM

I understand lifetime of a variable.
But I don't understand lifetime of a type well :(

red75prime
@red75prime
May 28 2018 15:47
Type with the 'static lifetime must contain only 'static references or no references at all.
Farzeen
@thefzsalam
May 28 2018 16:21
I changed T to T: 'static. Looks like it is okay.

But ..

static mut TYPES: HashMap<TypeId, glib_ffi::GType> = HashMap::new();

is causing the error

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
  --> src/gobject_fzn/ptr_gobject.rs:66:66
   |
66 |             static mut TYPES: HashMap<TypeId, glib_ffi::GType> = HashMap::new();
   |                                                                  ^^^^^^^^^^^^^^
Dylan DPC
@Dylan-DPC
May 28 2018 16:38
you might have to use lazy_static if it is possible
Farzeen
@thefzsalam
May 28 2018 16:43
I just solved it :)
static mut TYPES_CACHE: Option<HashMap<TypeId, glib_ffi::GType>> = None;
if TYPES_CACHE == None {
    TYPES_CACHE = Some(HashMap::new());
}
thanks anyway @Dylan-DPC :)
Dylan DPC
@Dylan-DPC
May 28 2018 16:43
uhmm
maybe you don't need a static?
Sylwester Rąpała
@xoac
May 28 2018 19:56
Hi, is this correct?
#[cfg(target_os = "linux" not(targert_env = "musl"))]
Zakarum
@omni-viral
May 28 2018 19:57
#[cfg(target_os = "linux", not(targert_env = "musl"))]
Sylwester Rąpała
@xoac
May 28 2018 20:00
maybe more proper:
#[cfg(target_os = "linux", targert_env = "gnu")]
since for linux we can only have "musl" and "gnu"? Am I right?
Sylwester Rąpała
@xoac
May 28 2018 20:06
No. I am not right. there can by also target_env unknown?