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

If an error is not affected by a macro, don't print macro notes #30506

Closed
nrc opened this issue Dec 21, 2015 · 4 comments
Closed

If an error is not affected by a macro, don't print macro notes #30506

nrc opened this issue Dec 21, 2015 · 4 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

Comments

@nrc
Copy link
Member

nrc commented Dec 21, 2015

e.g.,

foo!(fasdfas);

If there is an error about fasdfas it should just use the span for fasdfas, and not print anything about foo!.

cc @DanielJCampbell

@nrc nrc added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 21, 2015
@durka
Copy link
Contributor

durka commented Dec 21, 2015

Hmm what do you mean by "an error about fasdfas"? Definitely the macro backtrace adds a lot of noise to the error message, but isn't the macro free to reinterpret fasdfas in various ways so how is the compiler supposed to know whether foo! is relevant or not?

@nrc
Copy link
Member Author

nrc commented Dec 21, 2015

The compiler sees things somewhat backwards - it sees the post-expansion AST and an expansion trace. Errors in generated code, can come from three places - straight from the macro, straight from the input, or some combination of the two. For the first and third cases if there is an error, we can't do much better than giving the whole instantiation (e.g., foo!(fasdfas)) and hopefully some part of the macro definition (in some cases, perhaps only the whole macro definition, I'm not sure). However, in the middle case, the expansion trace shows that the macro is just copying and pasting it's input into somewhere in the macro. The compiler should be able to tell easily if that is the case from the expansion trace (and @DanielJCampbell has been doing some work improving these traces in any case), and then skip informing the user about the macro stuff, since it doesn't matter too much (we might still note that a macro was involved and reference it, but show less detail than we do at the moment).

edited for clarity

@nrc
Copy link
Member Author

nrc commented Dec 21, 2015

Actually, we seem to be doing much better than we used - we don't have the annoying note: expansion site notes any more.

I still think we should consider dropping any mention of the macro - after all if there is a problem with a sub-expression in a function call, we don't mention the function.

Examples:

macro_rules! foo{
    ($x: expr) => {$x}
}

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

would give just

<anon>:6:10: 6:13 error: unresolved name `bar` [E0425]
<anon>:6     foo!(bar());
                  ^~~
<anon>:6:10: 6:13 help: see the detailed explanation for E0425

losing (because it is not useful)

<anon>:6:5: 6:17 note: in this expansion of foo! (defined in <anon>)

but

macro_rules! foo{
    ($x: expr) => {$x.bar()}
}

fn main() {
    foo!(42);
}

would stay as it is:

<anon>:2:23: 2:28 error: no method named `bar` found for type `_` in the current scope
<anon>:2     ($x: expr) => {$x.bar()}
                               ^~~~~
<anon>:6:5: 6:14 note: in this expansion of foo! (defined in <anon>)

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@jseyfried jseyfried self-assigned this Mar 17, 2017
@jseyfried jseyfried added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Mar 17, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 26, 2017
…=nrc

macros: improve `Span`'s expansion information

This PR improves `Span`'s expansion information. More specifically:
 - It refactors AST node span construction to preserve expansion information.
   - Today, we only use the underlying tokens' `BytePos`s, throwing away the `ExpnId`s.
   - This improves the accuracy of AST nodes' expansion information, fixing rust-lang#30506.
 - It refactors `span.expn_id: ExpnId` to `span.ctxt: SyntaxContext` and removes `ExpnId`.
   - This gives all tokens as much hygiene information as `Ident`s.
   - This is groundwork for procedural macros 2.0 `TokenStream` API.
   - This is also groundwork for declarative macros 2.0, which will need this hygiene information for some non-`Ident` tokens.
 - It simplifies processing of spans' expansion information throughout the compiler.
 - It fixes rust-lang#40649.

r? @nrc
bors added a commit that referenced this issue Mar 28, 2017
macros: improve `Span`'s expansion information

This PR improves `Span`'s expansion information. More specifically:
 - It refactors AST node span construction to preserve expansion information.
   - Today, we only use the underlying tokens' `BytePos`s, throwing away the `ExpnId`s.
   - This improves the accuracy of AST nodes' expansion information, fixing #30506.
 - It refactors `span.expn_id: ExpnId` to `span.ctxt: SyntaxContext` and removes `ExpnId`.
   - This gives all tokens as much hygiene information as `Ident`s.
   - This is groundwork for procedural macros 2.0 `TokenStream` API.
   - This is also groundwork for declarative macros 2.0, which will need this hygiene information for some non-`Ident` tokens.
 - It simplifies processing of spans' expansion information throughout the compiler.
 - It fixes #40649.

r? @nrc
bors added a commit that referenced this issue Mar 30, 2017
macros: improve `Span`'s expansion information

This PR improves `Span`'s expansion information. More specifically:
 - It refactors AST node span construction to preserve expansion information.
   - Today, we only use the underlying tokens' `BytePos`s, throwing away the `ExpnId`s.
   - This improves the accuracy of AST nodes' expansion information, fixing #30506.
 - It refactors `span.expn_id: ExpnId` to `span.ctxt: SyntaxContext` and removes `ExpnId`.
   - This gives all tokens as much hygiene information as `Ident`s.
   - This is groundwork for procedural macros 2.0 `TokenStream` API.
   - This is also groundwork for declarative macros 2.0, which will need this hygiene information for some non-`Ident` tokens.
 - It simplifies processing of spans' expansion information throughout the compiler.
 - It fixes #40649.
 - It fixes #39450 and fixes part of #23480.

r? @nrc
@jseyfried
Copy link
Contributor

Fixed in #40597.

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, ..)
Projects
None yet
Development

No branches or pull requests

4 participants