-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: Plan to make core and std's panic identical. #3007
Conversation
[Citation needed] I think the first thing that needs to be here is a crater run. |
I'm not sure if a crater run would be enough here. I expect a lot of uses of Edit: Here's already a small preview of what crater will find: https://grep.app/search?q=panic%21%5C%28%5Cs%2A%5B%5E%5Cs%22%29%5D®exp=true&filter[lang][0]=Rust |
e1641a6
to
698fb76
Compare
Nothing in the search looks like an incompatible use of a format string, in particular ( |
That's because that regex specifically looks for panic!() invocations that do not start with a string literal. Searching for invocations of panic with only one argument that's both a string literal and contains braces is a lot harder. Here's an incomplete try (also note that grep.app searches through only a relatively small set of repositories): https://grep.app/search?q=panic%21%5C%28%5Cs%2A%22%5B%5E%22%5D%2A%7B%5B%5E%22%5D%2A%22%5Cs%2A%5C%29®exp=true&filter[lang][0]=Rust Cases like that could clearly use a warning. But making them an error would unecessarily break them. |
Ok, what I want to convey is that keeping compiler simple and minimizing edition differences is more important that preventing a couple of obscure crates from breaking. |
Still throwing We're still going to need an edition gate and a lint for |
Note that the idea of shifting One argument I found compelling - besides all the consistency / simplification arguments - is that I particularly agree with
Even as an experienced Rust developer, I still often do this when I'm thinking more about what I'm debugging than the debug code I'm hacking in. I often worry Rust beginners debugging their first program will not easily understand what they did wrong when they see error messages reading along the lines of |
All for removing differences between core and std, and for advancing progress toward capturing format string arguments. For the sake of completeness, one alternative that I've not yet seen proposed would be to introduce a new alternative to panic! with the correct semantics. Obviously this would be a much more drastic step, but risks no breakage whatsoever. |
|
||
Specifically: | ||
|
||
- Only for Rust 2021, we apply the breaking changes as in the previous section. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will this interact with macro expansion? For example, if I have the following code:
// crate2018 (2018 edition):
macro_rules! custom_panic {
($($arg:tt),*) => {
panic!($($arg),*)
}
}
fn foo() {
custom_panic!("Weird format: {}");
}
// crate2021 (2021 edition)
fn bar() {
crate2018::custom_panic!("Weird format: {}");
}
what should the result be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question. We could either look at the edition of the span of panic
or at the edition of the span of "Weird format: {}"
.
I think the former would make most sense. Then the macro doesn't change its behaviour until it is updated to Rust 2021 itself.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds reasonable to me. Note that this will be a 'best effort' approach to preventing breakage. With a somewhat contrived macro, this approach can be made to fail:
macro_rules! custom_invoke {
($name:ident) => {
$name!("My panic: {}")
}
}
fn main() {
custom_invoke!(panic)
}
That is, a higher-order macro could use the ident panic
from a 2021 edition crate, causing the new behavior to be used.
While this particular macro seems very unrealistic, there are some places in rustc that actually use higher-order macros. Unfortunately, I think we need to accept risk of breakage if this RFC is accepted. However, this would require a crate to be bumped to the 2021 edition, so we will never break a 2018-edition-only crate graph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd still make sense right? You're now passing the macro the 2021 panic, not the 2018 panic. With this style of macro, it's the responsibility of the caller to provide it with the name of a macro that works. Just like custom_invoke!(println)
also wouldn't compile. So (like you said) the breakage in this example is in the 2021 crate that calls the macro, not in the 2018 one that defines it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With proc macros, it may not be as clear that crate2021
is to blame. Using Span::resolved_at
, you could get a panic
ident that appears to come from a 2021 crate, even if the literal panic
never appears in a 2021 crate.
To be clear, I woud be shocked if this ever came up in practice. However, I think it would be worth adding an note to the RFC that this is idended to mitigate cross-edition breakage, not prevent it entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Aaron1011 isn't this true for all edition changes though? If I write a proc-macro that attaches the wrong spans, then it may stop working when a crate using that macro upgrades to a new edition.
Perhaps there should be an exception to the stability rules for bugs that cause code to be tagged with the wrong edition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. However, there have been a bunch of proc-macro API stabilizations since the 2018 edition, so I don't think it's really come up before.
If progress is made on implementing future-incompat-report, we could enable it for the lint referenced by this RFC. |
cc @rust-lang/lang rust-lang/rust#67984 (comment)
Here it is. :) |
I'm nominating for discussion in the @rust-lang/lang triage meeting, although I think this may also be considered a @rust-lang/libs RFC. |
- Instead of the last step, we could also simply break `assert!(expr, non_string_literal)` in all editions. | ||
This usage is probably way less common than `panic!(non_string_literal)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to do a crater run for this failed before it even started, because that change didn't even get past the @bors try
run. It caused a compilation error while compiling this part of clap
:
Apparently this behaviour of assert!()
is relied upon.
Co-authored-by: Ashley Mannix <[email protected]>
Thanks for working on this @m-ou-se! We discussed this in the recent Libs meeting and felt like this was a reasonable path forward to making our panicking macros behave consistently. I also quite liked the problem-solution organization style 🙂 This work is somewhat time-critical to catch the 2021 edition, so to get eyes and input from the wider team let's kick off a FCP! @rfcbot fcp merge |
Team member @KodrAus has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot reviewed We discussed this in our @rust-lang/lang backlog bonanza meeting and also felt quite good about it. |
@BurntSushi @pnkfelix @withoutboats Ping. ✨ Special limited time offer: 50% off your next checkmark if you click today!* ✨ *Terms and conditions apply. Offer valid for up to 1 (one) checkmark per customer. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC will be merged soon. |
Implement Rust 2021 panic This implements the Rust 2021 versions of `panic!()`. See rust-lang#80162 and rust-lang/rfcs#3007. It does so by replacing `{std, core}::panic!()` by a bulitin macro that expands to either `$crate::panic::panic_2015!(..)` or `$crate::panic::panic_2021!(..)` depending on the edition of the caller. This does not yet make std's panic an alias for core's panic on Rust 2021 as the RFC proposes. That will be a separate change: rust-lang@c5273bd That change is blocked on figuring out what to do with rust-lang#80846 first.
Implement Rust 2021 panic This implements the Rust 2021 versions of `panic!()`. See rust-lang/rust#80162 and rust-lang/rfcs#3007. It does so by replacing `{std, core}::panic!()` by a bulitin macro that expands to either `$crate::panic::panic_2015!(..)` or `$crate::panic::panic_2021!(..)` depending on the edition of the caller. This does not yet make std's panic an alias for core's panic on Rust 2021 as the RFC proposes. That will be a separate change: rust-lang/rust@c5273bd That change is blocked on figuring out what to do with rust-lang/rust#80846 first.
Rendered