Skip to content

Broken pattern span for identifier parameters in macros #32072

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
panicbit opened this issue Mar 6, 2016 · 3 comments
Closed

Broken pattern span for identifier parameters in macros #32072

panicbit opened this issue Mar 6, 2016 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@panicbit
Copy link
Contributor

panicbit commented Mar 6, 2016

This kind of macro pattern seems to mess up the span:

macro_rules! bad {
    ($s:ident whatever) => {
        {
            let $s = 0;
            *&mut $s = 0;
        }
    }
}

fn main() {
    bad!(foo whatever);
}

Playpen

Compiling it results in a messed up error message:

<anon>:11:14: 11:22 error: cannot borrow immutable local variable `foo` as mutable
<anon>:11     bad!(foo whatever);
                       ^~~~~~~~
<anon>:11:5: 11:24 note: in this expansion of bad! (defined in <anon>)
<anon>:11:14: 11:22 help: to make the local variable mutable, use `mut` as shown:
<anon>:       bad!(foo mut whatever);
<anon>:11:5: 11:24 note: in this expansion of bad! (defined in <anon>)
error: aborting due to previous error
@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Mar 11, 2016
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum
Copy link
Member

This is almost certainly not what we want today.. Or at least, it looks really odd.

error[E0596]: cannot borrow immutable local variable `foo` as mutable
  --> test.rs:11:10
   |
5  | |             *&mut $s = 0;
   | |____________________^ cannot borrow mutably
...
11 |       bad!(foo whatever);
   |  _____-----^-------------
   | |     |
   | |     in this macro invocation

error: aborting due to previous error(s)

@Mark-Simulacrum Mark-Simulacrum added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jun 5, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 24, 2017
@estebank
Copy link
Contributor

estebank commented Feb 1, 2018

CC #23480.

@estebank
Copy link
Contributor

estebank commented Feb 1, 2018

The current output is beyond bad because of the new suggestion:

error[E0596]: cannot borrow immutable local variable `foo` as mutable
  --> src/main.rs:5:19
   |
4  |                let $s = 0;
   |   _________________-
5  |  |             *&mut $s = 0;
   |  |___________________^
6  | ||         }
7  | ||     }
8  | || }
9  | ||
10 | || fn main() {
11 | ||     bad!(foo whatever);
   | ||     -------^-----------
   | ||_____|______|
   | |______|______consider changing this to `mut $s = 0;
            *&mut $s = 0;
        }
    }
}

fn main() {
    bad!(foo`
   |        |      cannot borrow mutably
   |        in this macro invocation

Fixed in #47942.

kennytm added a commit to kennytm/rust that referenced this issue Feb 2, 2018
…s Minimize weird spans involving macro context Sometimes the parser attempts to synthesize spans from within a macro context with the span for the captured argument, leading to non-sensical spans with very bad output. Given that an incorrect span is worse than a partially incomplete span, when detecting this situation return only one of the spans without merging them. Fix rust-lang#32072, rust-lang#47778. CC rust-lang#23480.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. 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

4 participants