#Macro ident name resolution.

19 messages · Page 1 of 1 (latest)

desert dock
#

I am writing a proc-macro and i require a canonical representation of an ident in the implementation.
Is this even possible to achieve without parsing the currently executed and running the name resolution algorithm from the call-site.

What do I mean with name-resolution

given the following code

my_proc!(hello_world);

I wish to canonicalise the hello_world. There are a few different scenarios i need to take into account here

fn hello_world () -> ! { ... }

my_proc!(hello_world); // canonicalises hello_world to crate::path::to::mod::hello_world; (even though hello_world is not pub i still require the full name as if it was pub based of the fact it will be used in a pre-processor stage
struct MyStruct;

impl MyStruct {
  fn something() -> ! {
    my_proc!(hello_world); // canonicalises to crate::path::to::mod::MyStruct::something::hello_world

    fn hello_world() -> ! { ... }
  }
}
struct MyStruct;
trait MyTrait { fn something () -> ! }

impl MyTrait for MyStruct {
  fn something() -> ! {
    let hello_world = "something";
    use path::to::func as hello_world;
    my_proc!(hello_world); // canonicalises to path::to::func
  }
}

does a macro like my_proc exist or will i have to implement it myself ?

pseudo axle
#

I don't think it's implementable, to be honest

#

Macros only see their input, which doesn't include, to my knowledge, "which namespace am I in"

humble geode
#

?eval ```rs
mod path {
pub mod to {
pub fn func() {}
}
}
macro_rules! my_proc {
($i:ident) => {{

    fn type_name_of_val<T>(v: &T) -> &'static str {
        ::core::any::type_name::<T>()
    }
    type_name_of_val(&$i)
}};

}
struct MyStruct;
trait MyTrait { fn something () -> !; }

impl MyTrait for MyStruct {
fn something() -> ! {
// let hello_world = "something";
// println!("{:?}", hello_world);
use path::to::func as hello_world;
println!("{:?}", hello_world()); // this is illegal if you have the let binding above
println!("{:?}", my_proc!(hello_world));
panic!()
}
}

fn main() {
MyStruct::something()
}

pearl mistBOT
#
thread 'main' panicked at 'explicit panic', src/main.rs:25:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

()
"playground::path::to::func"
humble geode
#

here ya go @desert dock , works fine

#

if you want to strip the crate name, you'll need to do some text process but its straightforward

desert dock
pseudo axle
#

Huh, nevermind

desert dock
#

but i will test this

humble geode
#

yes, you've got no chance to access that info inside a proc macro

#

though there's a lot you can do inside const fns, especially if you augment them with macros (this allows allocation, pretty much)

desert dock
#

unfortunetly i dont think const fns will cut it here. i have an idea of how to implement compile-time reflection and for this i need something a bit more complex i think

#

but if its not implemented then i guess ill just commit violence upon the compiler and do some way overkill hacks

humble geode
#

o i c

#

this sounds horrible :D

#

good luck