-
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
Allow panicking in constants #2345
Conversation
This seems like a good idea to me. Supporting panic from compile-time computation as a compile-time error makes perfect sense, and I'd anticipate many excellent error diagnostics on that basis. I do want to see something along the lines of the last item in the unresolved section. Whether |
I think that making
|
While I do support panicking in const fn, I'm uneasy with this potential direction mentioned in the unresolved question:
This sounds like eventually the entire We need the I don't know of a better solution. Maybe we could add a |
Well.. we could infer constness for private functions, which would make a lot of things easier. We could also consider some future Anyway, this is a very much orthogonal topic and we should probably be disussing it in the internals forum instead of here |
@oli-obk Having all of these @Centril Effect polymorphism, alas, is only a solution to abstracting over things. That is, if you might want to say things like "this |
@glaebhoerl while all of these RFCs increase the possible code that can be evaluated, this one in particular does not add anything new except nicer errors. The only one that allows real new features is the constant if RFC. Everything else was already possible via const fn. The particular concern about marking everything const fn should be part of the const fn stabilization and has already been heavily discussed in the tracking issue rust-lang/rust#24111 |
This wouldn't be too different from |
Also I think that the big thing to point out, whether or not you agree with If you're in favour of const inference then this RFC should be an obvious yes imho. |
Would this change the following from a runtime to a compile-time error, then? fn main() {
Err::<(), ()>(()).unwrap();
} If so, is that a breaking change - if someone does really want to panic at runtime? I like this change, but this aspect seems concerning. It seems to make adding |
@daboross I mean, that example is enough to warrant a crater run, but IMHO if a logic error is caught at compile time instead of runtime, that's just helping the programmer out. |
No. That just becomes a lint. |
@clarcharr it's helping the programmer for actively developed applications and libraries, but not everything has a maintainer. There are situations even today when small backwards-incompatible coherence changes break existing unmaintained libraries (andybarron/app-dirs-rs#28 is my latest example). This would be on a much larger scale, though. If we adopt What if there's some bad @oli-obk Alright, that sounds reasonable to me. Does this mean we make all compile-time panics lints which then forward the panic to runtime? |
It is only a compile time error if you call it in a const context. Since you can't call non const fns in const contexts, you can't get a breaking change this way. Const context being array lengths, static initializers or outright constants |
@oli-obk ahh, that makes a lot more sense, thanks! I guess I was going on the assumption that the compiler would unroll / execute any |
You can't execute |
This is similar to |
Fair enough; Specifying what effects a particular function has could be done more ergonomically with effect aliases (type aliases but for the effect kind), like so: effect foo = const + async + total;
foo fn bar(..) { // modulo syntax bikeshed and making it unambiguous.
} |
Summary of comment thread: As far as I can tell, all commenters seem to be in favor of this RFC as written. There was some discussion about whether to expand the scope of the RFC to also change
I do have one relatively small concern with the text itself, but my concern is pretty slight, so I think I will propose merging this via the rfcbot and then mark my concern formally there (in part to ensure that it does not get completely overlooked). |
@rfcbot fcp merge |
(argh hit the wrong button.) |
@rfcbot fcp merge |
Team member @pnkfelix has proposed to merge this. The next step is review by the rest of the tagged teams: Concerns:
Once a majority of reviewers approve (and none object), 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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
I'm jumping in pretty late here, but I'm not sure how I feel about the compile time versions of As an aside, it would give us the option to allow |
@tkadur note that if you write I think a goal should be to minimize the difference in experience across |
This is a good point - makes sense to me. |
Note that you'll almost never use |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
Huzzah! This RFC is merged! Tracking issue: rust-lang/rust#51999 |
…r=Centril Add support for `const unsafe? extern fn` This works just as you might expect - an `const extern fn` is a `const fn` that is callable from foreign code. Currently, panicking is not allowed in `const`s. When rust-lang/rfcs#2345 (rust-lang#51999) is stabilized, then panicking in an `const extern fn` will produce a compile-time error when invoked at compile time, and an abort when invoked at runtime. Since this is extending the language (we're allowing the `const` keyword in a new context), I believe that this will need an FCP. However, it's a very minor change, so I didn't think that filing an RFC was necessary. This will allow libc (and other FFI crates) to make many functions `const`, without having to give up on making them `extern` as well. Tracking issue: rust-lang#64926.
Add support for `const unsafe? extern fn` This works just as you might expect - an `const extern fn` is a `const fn` that is callable from foreign code. Currently, panicking is not allowed in `const`s. When rust-lang/rfcs#2345 (#51999) is stabilized, then panicking in an `const extern fn` will produce a compile-time error when invoked at compile time, and an abort when invoked at runtime. Since this is extending the language (we're allowing the `const` keyword in a new context), I believe that this will need an FCP. However, it's a very minor change, so I didn't think that filing an RFC was necessary. This will allow libc (and other FFI crates) to make many functions `const`, without having to give up on making them `extern` as well. Tracking issue: #64926.
Capitalization in a header is illegal and causes the bot to panic. I posted in discord and figured it out shortly after. https://docs.rs/http/latest/http/header/struct.HeaderName.html#method.from_static > Panics > This function panics when the static string is a invalid header. > > Until [Allow panicking in constants](rust-lang/rfcs#2345) makes its way into stable, the panic message at > compile-time is going to look cryptic, but should at least point at your header value
Rendered
Tracking issue
DON'T PANIC! "in large, friendly letters"
cc @pnkfelix (#1383)