Skip to content

Is it possible to pass a span to compile_error! #44535

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
chancancode opened this issue Sep 13, 2017 · 10 comments
Closed

Is it possible to pass a span to compile_error! #44535

chancancode opened this issue Sep 13, 2017 · 10 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-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@chancancode
Copy link
Contributor

I noticed that this question from @Stebalien was left unaddressed in the compile_error! RFC:

Is the span information not attached to the tokens passed to a macro? I'd like to be able point out which token in the macro input caused the macro expansion to fail (and why).

Before compile_error! was a thing, I generally report errors to the user (where possible) by passing the offending token to a "sink" macro that has no possible matches, which would cause the compiler to report no rule expected the token <token> with the right span. It is nice that now I can specify a useful message, however we lost the ability to point the error back to the right span. It is unfortunate to have to make this trade-off (either providing the right span without telling you why, or telling you why without reporting where), and in some scenario neither of the two options on their own would be sufficient to understand the error (it could be a very generic error that could happen in a lot of places – think "missing a semicolon" or "unbalanced braces" kind of errors).

cc @sgrif

chancancode added a commit to tildeio/helix that referenced this issue Sep 13, 2017
Unfortunately, we will lose the span for these errors.

(See rust-lang/rust#44535)
@chancancode
Copy link
Contributor Author

cc @wesleywiser

@sgrif
Copy link
Contributor

sgrif commented Sep 15, 2017

I explicitly left spans out of the original RFC, as there is no mechanism in place for dealing with spans anywhere in the macro system today, and I think such a change wouldn't be feasible without macros 2.0.

@chancancode
Copy link
Contributor Author

As @Stebalien has pointed out, the span information is attached to the original tokens. The strategy I used previously (forcing a mismatch) does (and still) usually report the right span. Therefore the reality is that today you have the option to report the right span or a message, but not both.

@chancancode
Copy link
Contributor Author

I think are a few possibilities.

Perhaps we can add a new macro, perhaps mismatch!(token, message) that produces a compile error with the token's span similar to the one you would get (no rules expected token...).

Another possibility is to make concat! somehow carry-forward the span from its input, so that you can produce the same message manually by concat!-ing the oftending token into the message that you pass to compile_error!

@sgrif
Copy link
Contributor

sgrif commented Sep 15, 2017

Those both seem super implicit and magical to me. Either way I think something like that would require an RFC. I wonder if you could get a good 80/20 solution by using line!, column! and file!?

@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Sep 17, 2017
@chancancode
Copy link
Contributor Author

line!, column! and file! won't work since they would point to the original/outermost macro invocation. If we can optionally pass the token to these macros the it would accomplish what we need.

@chancancode
Copy link
Contributor Author

There is a sort-of work around though. Since the compiler doesn't stop at the first error, you can first force a mismatch, then give more details in a compile_error!: http://play.rust-lang.org/?gist=c321a3c88544586c9323da8cc967aa99&version=stable&mode=debug

The line information for the compile_error! is not very useful and potentially confusing, so ideally, we won't want to show that. However, this is probably good enough since the compiler would just point to the outer-most in-crate macro invocation (still not useful, but it is less likely to cause confusion).

@limira
Copy link
Contributor

limira commented Jun 28, 2018

compile_error! is just give a half-usefulness in most cases. We need both why and where! Such as:

321 |          something_cause_error {
               ^^^^^^^^^^^^^^^^^^^^^ a useful message provided via compile_error here!

I like the work around by @chancancode above, but do in reverse:

macro_rules! give_me_foo_or_bar {
    (foo) => {};
    (bar) => {};
    ($x:ident) => {
        compile_error!("This macro only accepts `foo` or `bar`. Find location of error in next error");
        force_mismatch_on_error_token! ($x);
    }
}

@nrc nrc added A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) labels Jul 19, 2018
@wesleywiser
Copy link
Member

@Mark-Simulacrum
Copy link
Member

It does seem like ultimately what we want here is the diagnostics feature of macros stabilized, before that happens I don't expect this'll have too much movement, so I'm going to go ahead and close as it's edging on either "needs RFC" and/or "won't fix".

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-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants