Skip to content

Semantics::expand_macro_call returns None on some macro calls from version 0.0.274 onwards #19873

Closed
@redsun82

Description

@redsun82

In our experimental CodeQL rust analysis we piggy-back on rust-analyzer analysis library, and specifically for macro calls we rely on ra_ap_hir::Semantics::expand_macro_call. While updating from rust-analyzer 0.0.273 to 0.0.274 we suddenly saw in the wild a significant number of cases of that function returning None. I managed to recreate a small reproduction crate of the 4 classes of unexpanded macros I saw so far. For 2 of them I also managed to reproduce the behaviour on the VSCode extension (that is, newer versions give no expansion with the Expand macro recursively at caret command, while an older one those. The crate can be accessed here. Here's the 4 cases I found:

  1. a macro call in an impl block expanding to some items:
    impl S {
        def_x!();  // this doesn't expand since 0.0.274
    }
    (this does still expand in VSCode)
  2. very specifically, a macro call providing a format specifier for a macro that expands to a format! call (the original example was anyhow!, I recreated it with
    macro_rules! my_macro {
        ($head:expr, $($tail:tt)*) => { format!($head, $($tail)*) };
    }
    
    
    fn test() {
        _ = my_macro!(
            concat!("<", "{}", ">"),  // this doesn't expand since 0.0.274
            "hi",
        );
    }
    (VSCode does not expand further than alloc::__export::format_args! either before or after, but I'm guessing that might be on purpose as format_args! is not really a macro. Still I was getting an expansion of concat! there in 0.0.273, but I'm getting none on 0.0.274)
  3. a macro call in a doc attribute like #[doc = include_str!("README.md")]. This one I can confirm that previous versions of VSCode were able to expand, but now I get no expansion.
  4. a macro call in a .rs file that is included via include! rather than via a normal module item (and apparently that has been done in the wild even for non-generated files). Again when going to the included file with VSCode, the macros can be expanded on older plugin versions, but can't any more on newer ones.

I've seen some substantial changes in that area in the code, but I can't put my finger on what could be causing this. For us the most concerning instance is the first one, as it's not that uncommon.

Do you have any guidance on this?

rust-analyzer version: 0.0.274 (rust-analyzer as library from crates.io)
rustc version: 1.86

editor or extension: -

relevant settings: -

repository link (if public, optional): example crate

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions