Skip to content

Line and column numbers for linting warnings are off when recursing macros across files #48804

Closed
@glandium

Description

@glandium

The core problem is that when a linting warning is due to a piece coming from a macro in another crate, things get off.
Following is a minimalist reproducible testcase:

$ cargo new --lib foo
$ cd foo
$ cat <<'EOF' > src/bar.rs
#[macro_export]
macro_rules! bar {
    ($macro:ident !) => ( $macro! {
          a
          bbbbb
          cccccccccccccccccc
        }
    )
}
EOF
$ cat <<'EOF' > src/lib.rs
#[macro_use]
mod bar;

macro_rules! foo {
    ($($name:ident)+) => {
        pub enum Foo {$($name,)+ }
    };
}

bar!(foo!);
EOF

The compiler output is:

warning: variant `a` should have a camel case name such as `A`
  --> src/lib.rs:6:25
   |
4  | | macro_rules! foo {
   | |___________^
5  |       ($($name:ident)+) => {
6  |           pub enum Foo {$($name,)+ }
   |  _________________________^
...
10 |   bar!(foo!);
   |   ----------- in this macro invocation
   |
   = note: #[warn(non_camel_case_types)] on by default

warning: variant `bbbbb` should have a camel case name such as `Bbbbb`
  --> src/lib.rs:6:25
   |
5  | |     ($($name:ident)+) => {
   | |_______________^
6  |           pub enum Foo {$($name,)+ }
   |  _________________________^
...
10 |   bar!(foo!);
   |   ----------- in this macro invocation

warning: variant `cccccccccccccccccc` should have a camel case name such as `Cccccccccccccccccc`
  --> src/lib.rs:6:25
   |
6  |         pub enum Foo {$($name,)+ }
   |                         ^^^^
...
10 | bar!(foo!);
   | ----------- in this macro invocation

Several things of note:

  • Line numbers are progressing, but depending on the contents of the bar macro, they sometimes don't: even when all items are on different lines, sometimes the reported lines for some variants are the same as other variants.
  • Columns vary depending on the length of the items in the bar macro, but don't seem to actually match.
  • When putting everything in the same file, the errors look fine:
#[macro_use]
mod bar {
    #[macro_export]
    macro_rules! bar {
	($macro:ident !) => ( $macro! {
	      a
	      bbbbb
	      cccccccccccccccccc
	    }
	)
    }
}

macro_rules! foo {
    ($($name:ident)+) => {
        pub enum Foo {$($name,)+ }
    };
}

bar!(foo!);
warning: variant `a` should have a camel case name such as `A`
  --> src/lib.rs:6:8
   |
6  |             a
   |  ___________^
7  | |           bbbbb
8  | |           cccccccccccccccccc
9  | |         }
...  |
15 | |     ($($name:ident)+) => {
16 | |         pub enum Foo {$($name,)+ }
   | |_____________________________^
...
20 |   bar!(foo!);
   |   ----------- in this macro invocation
   |
   = note: #[warn(non_camel_case_types)] on by default

warning: variant `bbbbb` should have a camel case name such as `Bbbbb`
  --> src/lib.rs:7:8
   |
7  |             bbbbb
   |  ___________^
8  | |           cccccccccccccccccc
9  | |         }
10 | |     )
...  |
15 | |     ($($name:ident)+) => {
16 | |         pub enum Foo {$($name,)+ }
   | |_____________________________^
...
20 |   bar!(foo!);
   |   ----------- in this macro invocation

warning: variant `cccccccccccccccccc` should have a camel case name such as `Cccccccccccccccccc`
  --> src/lib.rs:8:8
   |
8  |             cccccccccccccccccc
   |  ___________^
9  | |         }
10 | |     )
11 | |     }
...  |
15 | |     ($($name:ident)+) => {
16 | |         pub enum Foo {$($name,)+ }
   | |_____________________________^
...
20 |   bar!(foo!);
   |   ----------- in this macro invocation

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions