Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upimplement #[panic_implementation] #50338
Conversation
rust-highfive
assigned
nagisa
Apr 30, 2018
rust-highfive
added
the
S-waiting-on-review
label
Apr 30, 2018
japaric
referenced this pull request
Apr 30, 2018
Closed
Tracking issue for RFC 2070: stable mechanism to specify the behavior of panic! in no-std applications #44489
SimonSapin
reviewed
Apr 30, 2018
|
|
||
| #[cfg(not(stage0))] | ||
| #[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe | ||
| extern "C" { |
This comment has been minimized.
This comment has been minimized.
SimonSapin
Apr 30, 2018
Contributor
Why not extern "Rust"? Would that solve the improper_ctypes warning?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
nagisa
Apr 30, 2018
Contributor
The comment should indicate why it is fine to pass such an object anyway (it never actually passes a FFI boundary and is a Rust-to-Rust call)
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
nagisa
reviewed
Apr 30, 2018
|
The travis failure has to be fixed as well. The failure seems pretty weird for the changes in this PR. |
|
|
||
| #[cfg(not(stage0))] | ||
| #[allow(improper_ctypes)] // PanicInfo contains a trait object which is not FFI safe | ||
| extern "C" { |
This comment has been minimized.
This comment has been minimized.
nagisa
Apr 30, 2018
Contributor
The comment should indicate why it is fine to pass such an object anyway (it never actually passes a FFI boundary and is a Rust-to-Rust call)
This comment has been minimized.
This comment has been minimized.
|
The changes look good otherwise. |
fbstj
reviewed
Apr 30, 2018
| #[cfg(not(stage0))] | ||
| #[cold] #[inline(never)] | ||
| pub fn panic_fmt(fmt: fmt::Arguments, file_line_col: &(&'static str, u32, u32)) -> ! { | ||
| struct NoPayload; |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I fixed those. I seem to have broken a bit the panicking (/ unwinding?) stuff; I'm seeing some proc-macro related tests fail. I won't have time to dig further any time soon but here's the list of failing tests if someone wants to investigate:
|
alexcrichton
reviewed
May 1, 2018
|
|
||
| #[cfg(not(stage0))] | ||
| #[cold] #[inline(never)] | ||
| pub fn panic_payload<M>(msg: M, file_line_col: &(&'static str, u32, u32)) -> ! |
This comment has been minimized.
This comment has been minimized.
alexcrichton
May 1, 2018
Member
I forget, but is this something we want to enable for libcore? I thought that we didn't have a path to actually supporting this yet?
(support in the sense of transmitting the value all the way to the caught value of thread::spawn)
This comment has been minimized.
This comment has been minimized.
japaric
May 16, 2018
Author
Member
I forget, but is this something we want to enable for libcore?
Well, this (adding payload support to core::panic!) is in the accepted RFC. Though we don't have to add it right now; we can stabilize #[panic_implementation] w/o payload support and add payload support later on in a backwards compatible fashion (or at least I think it's possible).
alexcrichton
reviewed
May 1, 2018
| @@ -1148,6 +1148,48 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>, | |||
| } | |||
| } | |||
|
|
|||
| // Check that a function marked as `#[panic_implementation]` has signature `fn(&PanicInfo) -> !` | |||
This comment has been minimized.
This comment has been minimized.
alexcrichton
May 1, 2018
Member
I'm personally more of a fan of generating AST nodes which correspond to the structure we want which avoids reaching into the internals of the typechecker where possible. For example I don't think this disallows something like this:
#[panic_implementation]
fn foo<T>(a: &PanicInfo) -> ! {
}right?
I was thinking that we'd basically expand #[panic_implementation] to #[lang = "panic_impl"] where the lang item internally dispatches to #[panic_implementation] (automatically typechecking it along the way). That may be slightly more difficult to architect, though, but I'm curious what you think
This comment has been minimized.
This comment has been minimized.
nagisa
May 1, 2018
Contributor
The language items are not type-checked at all at the moment, so there is nothing to hand the type-checking over to.
panic_fmt has no benefits over the new language item and IIRC (from when I wrote the instructions) complicates the implementation of this feature unnecessarily.
This comment has been minimized.
This comment has been minimized.
alexcrichton
May 1, 2018
Member
Right yeah, my point is that we generate a language item which internally dispatches to the function annotated #[panic_implementation], which by construction will typecheck the panic implementation and not require typechekcing of lang ites.
This comment has been minimized.
This comment has been minimized.
japaric
May 16, 2018
Author
Member
@alexcrichton you mean something like this?
// this
// (signature is totally wrong)
#[panic_implementation]
fn foo<T>(pi: &i32) {
// body
}
// expands into
#[lang = "panic_impl"]
fn __secret_name_maybe(pi: &PanicInfo) -> ! {
// user input
fn foo<T>(pi: &i32) {
// body
}
let f: fn(&PanicInfo) -> ! = foo;
f()
}That would work typechecking wise, but will it report error messages with meaningful spans? (My experience with proc macros says that most errors will wrongly report the whole #[panic_implementation] item as their span)
This comment has been minimized.
This comment has been minimized.
japaric
May 16, 2018
Author
Member
I don't think this disallows something like this:
fn foo(a: &PanicInfo) -> ! {
I can add a check and test to reject that.
This comment has been minimized.
This comment has been minimized.
alexcrichton
May 17, 2018
Member
@japaric yeah that latter idea is what I was thinking, where basically the compiler manufactures the actual lang item which is hooked up to call the #[panic_implementation]. That way the compiler has full control over ABI and whatnot
This comment has been minimized.
This comment has been minimized.
nagisa
May 28, 2018
Contributor
Sadly in many cases this approach results in kind-of suboptimal errors.
For example
fn foo() {
fn bar<T>(i: i32) -> ! { loop {} }
let foo: fn(i32) -> ! = bar;
}
gives
error[E0282]: type annotations needed
--> src/main.rs:3:29
|
3 | let foo: fn(i32) -> ! = bar;
| ^^^ cannot infer type for `T`
but if expanded within a macro it would be something like this instead:
error[E0282]: type annotations needed
--> src/main.rs:3:29
|
3 | #[panic_implementation]
| fn panic<T>(...) -> ! { loop {} }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `T`
which is just… incomprehensible.
That being said, I have a long time wish to eventually implement universal checking of the language item signatures (similar to one we do for intrinsics), which would resolve this issue universally. Thus, I’d recommend going for the simplest complete approach possible for now.
alexcrichton
reviewed
May 1, 2018
| @@ -180,7 +180,7 @@ fn default_hook(info: &PanicInfo) { | |||
|
|
|||
| let location = info.location().unwrap(); // The current implementation always returns Some | |||
|
|
|||
| let msg = match info.payload().downcast_ref::<&'static str>() { | |||
| let msg = match info.payload().downcast_ref::<&str>() { | |||
This comment has been minimized.
This comment has been minimized.
alexcrichton
May 1, 2018
Member
How come this was necessary? I thought Any-style things could only be with 'static types?
This comment has been minimized.
This comment has been minimized.
|
|
shepmaster
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
May 6, 2018
This comment has been minimized.
This comment has been minimized.
|
Ping from triage, @japaric ! You've got merge conflicts and some review comments pending. |
This comment has been minimized.
This comment has been minimized.
|
Ping from triage @japaric! Will you have time to work on this soon? |
japaric
force-pushed the
japaric:panic-impl
branch
2 times, most recently
from
6cbc8af
to
5830bc2
May 16, 2018
This comment has been minimized.
This comment has been minimized.
|
I have reverted the payload in core::panic! stuff hoping that it makes this PR less controversial. See also #50338 (comment). This has been rebased and passes the test suite when using stage 2. Turns out all the weird errors around proc-macros that I was seeing only occur when testing stage 1 ( |
rust-highfive
assigned
alexcrichton
and unassigned
nagisa
May 16, 2018
This comment has been minimized.
This comment has been minimized.
|
Could some tests also be added for situations like:
|
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
Ping from triage @japaric! The reviewer provided some feedback, do you have time to address it? |
japaric
force-pushed the
japaric:panic-impl
branch
from
5830bc2
to
5d0e2c2
May 29, 2018
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton requested tests have been added. r? |
This comment was marked as resolved.
This comment was marked as resolved.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
pietroalbini
added
S-waiting-on-review
and removed
S-waiting-on-author
labels
May 29, 2018
This comment has been minimized.
This comment has been minimized.
|
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
kennytm
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
Jun 3, 2018
This comment has been minimized.
This comment has been minimized.
|
@bors r=alexcrichton |
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-bors
and removed
S-waiting-on-author
labels
Jun 3, 2018
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jun 3, 2018
This comment has been minimized.
This comment has been minimized.
|
|
japaric commentedApr 30, 2018
This implements the
#[panic_implementation]attribute as instructed in #44489 (comment)I haven't run the full test suite yet but at least all the compile-fail tests pass.
r? @nagisa