Skip to content

a type error in a nested macro leads to a confusing compiler error message #45921

Closed
@oconnor663

Description

@oconnor663

Here's a toy program demonstrating the problem:

macro_rules! my_macro {
    () => {
        fn my_func() {
            // An intentional type error here. The compiler doesn't do a good
            // job of saying what line is causing it.
            assert_eq!(0u8, 0u16);
        }
    }
}

my_macro!();

fn main() {
    my_func();
}

Compiling it (current stable rustc, 1.21) gives the following error:

error[E0308]: mismatched types
  --> src/main.rs:11:1
   |
11 | my_macro!();
   | ^^^^^^^^^^^^
   | |
   | expected u8, found u16
   | in this macro invocation
   |
   = note: this error originates in a macro outside of the current crate

All the compiler is telling me is that "something inside the macro generates this type error". In particular, it's not able to tell me that the problem is on line 6. I wouldn't be surprised if this was a known problem somewhere, but I wanted to make sure to report it, since it's probably super confusing for new developers. (For example, I expect you could run into this easily by combining assert! with another common macro like lazy_static!.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions