rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.26k stars 1.61k forks source link

Macros cannot be expanded in certain situations. #17837

Open coderfreii opened 2 months ago

coderfreii commented 2 months ago

rust-analyzer version: v0.3.2062

rustc version: rustc 1.80.0 (051478957 2024-07-21)

editor or extension: VSCode

relevant settings: default

repository link (if public, optional): (https://github.com/jkb0o/belly/blob/main/crates/belly_widgets/src/input/slider.rs)

code snippet to reproduce:

#[widget]
#[extends(RangeWidget)]
#[styles(
    slider .slider-grabber {
      margin: 0px;
      min-width: 16px;
      min-height: 16px;
      width: 16px;
      height: 16px;
    }
)]
fn slider(ctx: &mut WidgetContext) {
    let grabber = SliderGrabber {
        slider: ctx.entity(),
    };
    let params = ctx.params();
    ctx.render(eml! {
        <range c:slider params=params>
            <slot separator>
                <button with=grabber mode="instant" c:slider-grabber>
                </button>
            </slot>
        </range>
    })
}

When I use the command 'Expand macro recursively at caret' on a macro widget, nothing happens. After that, I commented out the content in the macro styles, and it worked.

For example:

#[widget]
#[extends(RangeWidget)]
#[styles(
    // slider .slider-grabber {
    //   margin: 0px;
    //   min-width: 16px;
    //   min-height: 16px;
    //   width: 16px;
    //   height: 16px;
    // }
)]
fn slider(ctx: &mut WidgetContext) {
coderfreii commented 2 months ago

And the cargo expand command produces the correct output.

            fn split_components(
                &self,
                components: Self::Components,
            ) -> (Self::BuildComponents, Self::OtherComponents) {
                let () = components;
                ((), ())
            }
            fn default_styles(&self) -> &str {
                "slider .slider-grabber {\n  margin: 0px;\n  min-width: 16px;\n  min-height: 16px;\n  width: 16px;\n  height: 16px;\n}\n\n"
            }
Veykril commented 2 months ago

I imagine we might be tokenizing things wrongly (the 16px looks suspicious to me as that is a special kind of literal)

evbo commented 2 months ago

here's another one that won't re-expand after any line change; you must manually "restart" rust-analyzer every time to get rid of red squiggly highlighting:

macro_rules! make_types {
    ( $l:ident, ($($n:ident $c:literal $d:expr),*)) => {
        $( pub const $n: &str = $c; )*

        lazy_static! {
            pub static ref $l: Vec<(&'static str, DataType)> = vec![
                $( ($n, $d), )*
            ];
        }
    };
}

it also strangely highlights this use as "unresolved import" until I restart after every code change: use lazy_static::lazy_static;