-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Comments
Unfortunately, we will lose the span for these errors. (See rust-lang/rust#44535)
cc @wesleywiser |
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. |
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. |
I think are a few possibilities. Perhaps we can add a new macro, perhaps 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! |
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 |
|
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 The line information for the |
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);
}
} |
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". |
I noticed that this question from @Stebalien was left unaddressed in the
compile_error!
RFC: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 reportno 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
The text was updated successfully, but these errors were encountered: