rune-rs / rune

An embeddable dynamic programming language for Rust.
https://rune-rs.github.io
Apache License 2.0
1.71k stars 89 forks source link

Support non-qualified imports of macros and imports which happen after the macro is used #525

Open ModProg opened 1 year ago

ModProg commented 1 year ago

Now that item macros are working again (#521) it is visible, that importing them does not:

use std::experiments::make_function;

pub fn main() {
    hi()
}

make_function!( hi => { println("hello") } );

One should also not forget the order shouldn't matter:

pub fn main() {
    hi()
}

make_function!( hi => { println("hello") } );

use std::experiments::make_function;

And to make matters worse, probably a macro could resolve to an import that imports a different macro.

The issue is probably this loop: https://github.com/rune-rs/rune/blob/ebfb91ee930d52767748eb1c2cde34b2e13748f6/crates/rune/src/indexing/index.rs#L404

This should probably whenever it finds a macro it cannot resove push it back to be processed after the other imports.

In rust the use in macro-expansion works:

macro_rules! use_you {
    () => {
        use hello::you;
    };
}

fn main() {
    you::println!("Hello, world!");

    use_you!();
}

mod hello {
    pub mod you {
        pub use std::println;
    }
}
udoprog commented 1 year ago

These are two know limitations:

This is simply an effect of how macros are integrated into the system, for now. It can clearly be improved but we don't have the necessary subsystems to do so! This could possibly be fixed by further deferring macro expansion to happen after at least one round of complete indexing - then uses which happen after the macro would be seen.

A technical limitation to make this kind of deferment happen is that specific points of the AST can't be very easily referenced for later processing, we just currently rely only mutable access to the AST as we walk over it.

I've added this bug report as linked in #27.

ModProg commented 1 year ago
  • Macro imports must use ::std, or fully qualified paths.

oh sorry, that I missed.

udoprog commented 1 year ago

Not your fault. The macro system is still work in progress, and I'm not sure this is expressed elsewhere except in maybe the issue which implemented it.

udoprog commented 1 year ago

I might've been a bit hasty, after re-reading the code it seems like we can actually handle use processing during indexing (leading to #526).

What we currently do not handle is module expansion though, but one out of two problems is pretty neat anyways. Thanks for pointing me in the right direction!