Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
glandium opened this issue Mar 7, 2018 · 3 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

glandium commented Mar 7, 2018

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
@Centril Centril added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Mar 7, 2018
@estebank
Copy link
Contributor

estebank commented Mar 7, 2018

@glandium, could you confirm wether this is the output in the nightly compiler too? It is probably the case, but just want to verify. (CC #47942)

@glandium
Copy link
Contributor Author

glandium commented Mar 7, 2018

The code I had the issue happen first uses unstable features, so was compiled with the nightly compiler. The reduced testcase above might have been compiled with the stable compiler.

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 21, 2018
@Mark-Simulacrum
Copy link
Member

We seem to have sort of fixed this, though perhaps accidentally. The new output at least appears self-consistent, though that might be due to changes in how the lints looks rather than actual fixes. Either way, test case at least doesn't reproduce.

   Compiling foo v0.1.0 (/Users/mark/Edit/testing/foo)
warning: variant `a` should have an upper camel case name
  --> src/bar.rs:4:11
   |
4  |           a
   |           ^ help: convert the identifier to upper camel case: `A`
   |
  ::: src/lib.rs:10:1
   |
10 | bar!(foo!);
   | ----------- in this macro invocation
   |
   = note: `#[warn(non_camel_case_types)]` on by default

warning: variant `bbbbb` should have an upper camel case name
  --> src/bar.rs:5:11
   |
5  |           bbbbb
   |           ^^^^^ help: convert the identifier to upper camel case: `Bbbbb`
   |
  ::: src/lib.rs:10:1
   |
10 | bar!(foo!);
   | ----------- in this macro invocation

warning: variant `cccccccccccccccccc` should have an upper camel case name
  --> src/bar.rs:6:11
   |
6  |           cccccccccccccccccc
   |           ^^^^^^^^^^^^^^^^^^ help: convert the identifier to upper camel case: `Cccccccccccccccccc`
   |
  ::: src/lib.rs:10:1
   |
10 | bar!(foo!);
   | ----------- in this macro invocation

    Finished dev [unoptimized + debuginfo] target(s) in 0.25s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants