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

Type errors in macros have poor spans #38771

Open
Mark-Simulacrum opened this issue Jan 2, 2017 · 4 comments
Open

Type errors in macros have poor spans #38771

Mark-Simulacrum opened this issue Jan 2, 2017 · 4 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-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

@Mark-Simulacrum
Copy link
Member

For example, in this code, it's hard to tell which part of the inputs to the macro has the wrong type. This is obviously a reduced example, but I've seen the same problem in real code.

I suspect this might be a duplicate issue, but couldn't find anything...

fn main() {
    assert_eq!(10u64, 10usize);
}

provides:

rustc 1.16.0-nightly (4ecc85beb 2016-12-28)
error[E0308]: mismatched types
 --> <anon>:2:5
  |
2 |     assert_eq!(10u64, 10usize);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u64, found usize
  |
  = note: this error originates in a macro outside of the current crate

error: aborting due to previous error
@KalitaAlexey
Copy link
Contributor

Is anyone working on it?

@Mark-Simulacrum Mark-Simulacrum added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-diagnostics Area: Messages for errors, warnings, and lints labels May 20, 2017
@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Jul 26, 2017
@estebank
Copy link
Contributor

On nightly using -Z external-macro-backtrace this looks like this:

error[E0308]: mismatched types
 --> <assert_eq macros>:5:22
  |
5 | if ! ( * left_val == * right_val ) {
  |                      ^^^^^^^^^^^ expected u64, found usize
file2.rs:2:5: 2:32 note: in this expansion of assert_eq! (defined in <assert_eq macros>)

error: aborting due to previous error

and once #46605 lands it'll look like this:

error[E0308]: mismatched types
 --> <assert_eq macros>:5:22
  |
5 | if ! ( * left_val == * right_val ) {
  |                      ^^^^^^^^^^^ expected u64, found usize
 ::: file.rs
  |
2 |     assert_eq!(10u64, 10usize);
  |     --------------------------- in this expansion of `assert_eq!`

error: aborting due to previous error

@estebank
Copy link
Contributor

estebank commented Feb 1, 2018

We should probably leave this ticket open for us to stabilize external-macro-backtrace.

This reminds me, -Zteach should probably imply a simplified version of -Zexternal-macro-backtrace.

@estebank
Copy link
Contributor

I think the "only" work left to do here is to add a way to annotate macro inner spans as coming from a macro argument. This annotation has to be very viral in order to work in this case. Looking at the current output for external-macro-backtrace:

error[E0308]: mismatched types
  --> <assert_eq macros>:5:22
   |
1  | / ( $ left : expr , $ right : expr ) => (
2  | | {
3  | | match ( & $ left , & $ right ) {
4  | | ( left_val , right_val ) => {
5  | | if ! ( * left_val == * right_val ) {
   | |                      ^^^^^^^^^^^ expected u64, found usize
...  |
20 | |  right: `{:?}`: {}"# ,
21 | | left_val , right_val , format_args ! ( $ ( $ arg ) + ) ) } } } } ) ;
   | |____________________________________________________________________- in this expansion of `assert_eq!`
   |
  ::: test.rs:2:5
   |
2  |       assert_eq!(10u64, 10usize);
   |       --------------------------- in this macro invocation

the following spans should have the following internal_macro_backtrace:

1  | ( $ left : expr , $ right : expr ) => (
   |     ^^^^ 1          ^^^^^ 2
2  | {
3  | match ( & $ left , & $ right ) {
   |         --------  ---------- 4 through 2
   |         |
   |         3 through 1
4  | ( left_val , right_val ) => {
   |   --------  ---------- 6 through 4
   |   |
   |   5 through 3
5  | if ! ( * left_val == * right_val ) {
   |        ^^^^^^^^^^    ^^^^^^^^^^^ 8 though 6
   |        |
   |        7 through 5

Once we have a way to travel back to an individual argument, we could see that the error comes from 8, which is tied all the way to 2, which corresponds to the second match argument in the macro call and emit the following without the backtrace:

error[E0308]: mismatched types
 --> <anon>:2:5
  |
2 |     assert_eq!(10u64, 10usize);
  |                       ^^^^^^^ expected u64, found usize
  |
  = note: this error originates in a macro outside of the current crate

or

error[E0308]: mismatched types
 --> <anon>:2:5
  |
2 |     assert_eq!(10u64, 10usize);
  |     ------------------^^^^^^^- in this macro call
  |                       |
  |                       expected u64, found usize
  |
  = note: this error originates in a macro outside of the current crate

@crlf0710 crlf0710 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 11, 2020
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-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

4 participants