rust-lang / rust-lang/rust

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

Open
#137,765 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-dyn-trait A-lang-item F-rustc_attrs I-unsound T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 https://github.com/rust-lang/rust/pull/137669#discussion_r1973326456 @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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the discussion in #137669 and the linked Compiler Explorer example to understand the reported miscompile around #[rustc_nounwind] on trait methods. Trace the current behavior for the trait declaration and its implementations, then establish the intended rule and add coverage showing that calls and implementations follow it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.