Skip to content
New issue

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

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

Already on GitHub? # to your account

error message for macro in wrong context is missing context #34421

Closed
durka opened this issue Jun 23, 2016 · 10 comments · Fixed by #70434
Closed

error message for macro in wrong context is missing context #34421

durka opened this issue Jun 23, 2016 · 10 comments · Fixed by #70434
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@durka
Copy link
Contributor

durka commented Jun 23, 2016

This is a regression from stable to beta and nightly.

This code is wrong (missing semicolons):

macro_rules! make_item {
    ($a:ident) => { struct $a; }
}

fn a() { make_item!(A) }
fn b() { make_item!(B) }

Stable prints:

<anon>:2:21: 2:27 error: expected identifier, found keyword `struct`
<anon>:2     ($a:ident) => { struct $a; }
                             ^~~~~~
<anon>:5:21: 5:22 error: macro expansion ignores token `A` and any following
<anon>:5 fn a() { make_item!(A) }
                             ^
<anon>:5:10: 5:23 note: caused by the macro expansion here; the usage of `make_item!` is likely invalid in expression context
<anon>:5 fn a() { make_item!(A) }
                  ^~~~~~~~~~~~~
<anon>:2:21: 2:27 error: expected identifier, found keyword `struct`
<anon>:2     ($a:ident) => { struct $a; }
                             ^~~~~~
<anon>:6:21: 6:22 error: macro expansion ignores token `B` and any following
<anon>:6 fn b() { make_item!(B) }
                             ^
<anon>:6:10: 6:23 note: caused by the macro expansion here; the usage of `make_item!` is likely invalid in expression context
<anon>:6 fn b() { make_item!(B) }
                  ^~~~~~~~~~~~~
error: aborting due to 4 previous errors

Beta and nightly both print:

error: expected expression, found keyword `struct`
 --> <anon>:2:21
  |>
2 |>     ($a:ident) => { struct $a; }
  |>                     ^^^^^^

Notice that not only is all the useful information gone, but the compiler aborts after the first error instead of finding the rest. It does not matter whether RUST_NEW_ERROR_FORMAT is on or off.

cc @nikomatsakis @jonathandturner

@jseyfried
Copy link
Contributor

cc me

@Mark-Simulacrum
Copy link
Member

So this fell through the cracks, and was a stable to nightly/beta regression. It still is a regression, in a way, but only of diagnostics. @jseyfried Could you take a look?

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label May 6, 2017
@nikomatsakis nikomatsakis added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 8, 2017
@nikomatsakis
Copy link
Contributor

Nominating for prioritization.

@nikomatsakis
Copy link
Contributor

So this part of the (previous) stable message:

macro expansion ignores token A and any following

feels like a bug to me anyhow.

This part:

caused by the macro expansion here; the usage of make_item! is likely invalid in expression context

is sort of good. It'd be nice to preserve a backtrace, at least. And ideally we'd suggest adding a ;.

@nikomatsakis
Copy link
Contributor

Given how much time has past, we're going to call this P-medium for now. However, @jseyfried will try to write up some mentoring instructions (or maybe fix it). He has a plan. =)

triage: P-medium

@rust-highfive rust-highfive added P-medium Medium priority and removed I-nominated labels May 11, 2017
@jseyfried jseyfried self-assigned this May 11, 2017
@jseyfried
Copy link
Contributor

I think the best path forward here is to try parsing an expression macro expansion as a statement before emitting an error. If the macro expansion parses as a statement successfully, we can emit a more helpful error suggesting that the user add a semicolon to the macro invocation.

@jseyfried jseyfried added E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels May 15, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 25, 2017
@Freyskeyd
Copy link
Contributor

I'm interested in this but I need informations to understand what to do :)

@estebank
Copy link
Contributor

estebank commented May 8, 2019

Current output:

error: expected expression, found keyword `struct`
 --> src/lib.rs:2:21
  |
2 |     ($a:ident) => { struct $a; }
  |                     ^^^^^^ expected expression
...
5 | fn a() { make_item!(A) }
  |          ------------- in this macro invocation

@oli-obk oli-obk added P-low Low priority and removed P-medium Medium priority labels May 24, 2019
@oli-obk
Copy link
Contributor

oli-obk commented May 24, 2019

Diagnostics meeting triage: lowering priority as the current error is pretty good, even if not ideal yet.

@Centril
Copy link
Contributor

Centril commented Mar 26, 2020

Current output:


error: expected expression, found keyword `struct`
 --> src/lib.rs:2:21
  |
2 |     ($a:ident) => { struct $a; }
  |                     ^^^^^^ expected expression
...
5 | fn a() { make_item!(A) }
  |          ------------- in this macro invocation
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected expression, found keyword `struct`
 --> src/lib.rs:2:21
  |
2 |     ($a:ident) => { struct $a; }
  |                     ^^^^^^ expected expression
...
6 | fn b() { make_item!(B) }
  |          ------------- in this macro invocation
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

(Notice that we recover -- it's not a fatal error now.)

Centril added a commit to Centril/rust that referenced this issue Mar 27, 2020
suggest `;` on expr `mac!()` which is good as stmt `mac!()`

Fixes rust-lang#34421 by implementing @jseyfried's suggestion in rust-lang#34421 (comment).

r? @petrochenkov
@bors bors closed this as completed in cfe1e33 Mar 28, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants