Skip to content
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

Incorrect trace for Item-level syntax errors in modules included by macro-generated code #66071

Open
SOF3 opened this issue Nov 4, 2019 · 2 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

@SOF3
Copy link
Contributor

SOF3 commented Nov 4, 2019

Using Rust nightly 2019-11-04.

Consider a macro that generates a mod statement:

#[proc_macro]
pub fn generate(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
    quote::quote!(mod foo;).into()
}

Call this in the src/main.rs in another crate:

codegen::generate!();

fn main() {
    foo::bar();
}

In src/foo.rs, we have the following code:

#{inline}
pub fn bar() {
    println!("Hello world");
}

Note the intentional syntax error #{inline} instead of #[inline] in src/foo.rs.

Compiling this setup (reproducible example in
upload.tar.gz)
with cargo check,
we have the following error message:

error: expected `[`, found `{`
 --> src\main.rs:1:1
  |
1 | codegen::generate!();
  | ^^^^^^^^^^^^^^^^^^^^^

error[E0433]: failed to resolve: use of undeclared type or module `foo`
 --> src\main.rs:4:5
  |
4 |     foo::bar();
  |     ^^^ use of undeclared type or module `foo`

The second error is an indirect consequence of the first error.
But where did the "found {" error message come from?

Obviously, there is nothing wrong with the codegen::generate!() macro
(we can verify this by fixing the intentional syntax error in src/foo.rs).

So let's try manually expanding the macro call, i.e. we now replace src/main.rs:1 with

mod foo;

And we have the following correct error message:

error: expected `[`, found `{`
 --> src\foo.rs:1:2
  |
1 | #{inline}
  |  ^ expected `[`

error: aborting due to previous error

So this error is caused by incorrect error span detection.

I am unable to locate the exact set of syntax errors affected,
because this does not happen with every syntax error.

For example, if I replace src/foo.rs with

#[inline] // correct syntax here
pub fn bar() {
    let a = 1 // missing semicolon here
    println!("Hello world");
}

The resultant error message is correct:

error: expected `;`, found ``println``
 --> src\foo.rs:3:14
  |
3 |     let a = 1
  |              ^ help: add `;` here
4 |     println!("Hello world");
  |     ------- unexpected token
@SOF3 SOF3 changed the title Incorrect syntax error location in macro-generated mods Incorrect syntax error location in mods included by macro-generated cofe Nov 4, 2019
@SOF3 SOF3 changed the title Incorrect syntax error location in mods included by macro-generated cofe Incorrect syntax error location in mods included by macro-generated code Nov 4, 2019
@csmoe csmoe added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 4, 2019
@Centril Centril added 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. labels Nov 5, 2019
@Centril
Copy link
Contributor

Centril commented Nov 5, 2019

cc @petrochenkov

@SOF3
Copy link
Contributor Author

SOF3 commented Dec 14, 2019

it appears that this issue can be generally reproduced with Item-level syntax errors (such as missing semicolon behind use, etc.) in the included module.

@SOF3 SOF3 changed the title Incorrect syntax error location in mods included by macro-generated code Incorrect trace for Item-level syntax errors in modules included by macro-generated code Dec 14, 2019
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

3 participants