Skip to content

Clarifying the behavior of #[rustc_nounwind] at trait methods #137765

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

Open
dianqk opened this issue Feb 28, 2025 · 3 comments
Open

Clarifying the behavior of #[rustc_nounwind] at trait methods #137765

dianqk opened this issue Feb 28, 2025 · 3 comments
Labels
A-lang-item Area: Language items A-trait-objects Area: trait objects, vtable layout F-rustc_attrs Internal rustc attributes gated on the `#[rustc_attrs]` feature gate. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dianqk
Copy link
Member

dianqk commented Feb 28, 2025

Currently, #[rustc_nounwind] is only applied to functions that are explicitly annotated; it's neither required nor applied by default to all implementations. I have no objection to this, but I think we need to clarify what the expected behavior should be.

An alternative is:

If you apply #[rustc_nounwind] on a trait method, it should be fine to assume that all calls of this trait method don't unwind, right? If any of the implementations doesn't use #[rustc_nounwind], that should probably be reported as error. (from #137669 (comment) @bjorn3)

pub trait Trait {
    #[rustc_nounwind]
    fn m(&self, _: (i32, i32, i32));
}

impl Trait for () {
    fn m(&self, _: (i32, i32, i32)) {
        panic!();
    }
}

Based on the current behavior, there are some miscompile.
In the case of https://rust.godbolt.org/z/aTWsEMxWq, all invokes should be unwinding.

From #137669.

@dianqk dianqk added A-lang-item Area: Language items A-trait-objects Area: trait objects, vtable layout F-rustc_attrs Internal rustc attributes gated on the `#[rustc_attrs]` feature gate. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness labels Feb 28, 2025
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 28, 2025
@dianqk dianqk removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 28, 2025
@compiler-errors
Copy link
Member

I think it should be fine to just reject it on (trait) associated fns, do we apply to any today?

@dianqk
Copy link
Member Author

dianqk commented Feb 28, 2025

I think it should be fine to just reject it on (trait) associated fns, do we apply to any today?

I agree. I believe it's not applied anywhere. I think #[rustc_nounwind] should only be used for trivial functions, and the following code is quite odd:

#[rustc_nounwind]
fn foo() {
    panic!()
}

@bjorn3
Copy link
Member

bjorn3 commented Feb 28, 2025

#[rustc_nounwind] is also used for the global allocator and the unstable std::panic::abort_unwind function, both of which run arbitrary user code.

@jieyouxu jieyouxu added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lang-item Area: Language items A-trait-objects Area: trait objects, vtable layout F-rustc_attrs Internal rustc attributes gated on the `#[rustc_attrs]` feature gate. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants