Skip to content

unused_lifetimes yeets unsafe binders #141758

Closed
@matthiaskrgr

Description

@matthiaskrgr

Probably the most amusing bug I will encounter today :)

I tried this code:

#![feature(unsafe_binders)]
#![allow(incomplete_features)]
#![warn(unused_lifetimes)]

use std::unsafe_binder::unwrap_binder;

#[derive(Copy, Clone)]
pub struct S([usize; 8]);

// Regression test for <https://github.com/rust-lang/rust/issues/141418>.
pub fn by_value(x: unsafe<'a> S) -> usize {
    unsafe { (|| unwrap_binder!(x).0[0])() }
}

fn main() {}

I expected to see this happen: explanation

Instead, this happened: explanation

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (1bbd62e54 2025-05-29)
binary: rustc
commit-hash: 1bbd62e547ba5cc08ccb44c27def3d33195d2dd5
commit-date: 2025-05-29
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

cargo check:

warning: lifetime parameter `'a` never used
  --> src/main.rs:11:27
   |
11 | pub fn by_value(x: unsafe<'a> S) -> usize {
   |                    -------^^--- help: elide the unused lifetime
   |
note: the lint level is defined here
  --> src/main.rs:3:9
   |
3  | #![warn(unused_lifetimes)]
   |         ^^^^^^^^^^^^^^^^

cargo fix

The following errors were reported:
error: expected type, found `)`
  --> src/main.rs:11:20
   |
11 | pub fn by_value(x: ) -> usize {
   |                    ^ expected type

error[E0425]: cannot find value `x` in this scope
  --> src/main.rs:12:33
   |
8  | pub struct S([usize; 8]);
   | ------------------------- similarly named tuple struct `S` defined here
...
12 |     unsafe { (|| unwrap_binder!(x).0[0])() }
   |                                 ^ help: a tuple struct with a similar name exists: `S`

error: aborting due to 2 previous errors

it removes the entire unsafe<'a> S 😆

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.F-unsafe_binders`#![feature(unsafe_binders)]`L-unused_lifetimesLint: unused_lifetimesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions