Skip to content

Emit dummy open drop for unsafe binder #141431

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

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions compiler/rustc_mir_transform/src/elaborate_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,23 @@ where
}
ty::Slice(ety) => self.drop_loop_trio_for_slice(*ety),

ty::UnsafeBinder(_) => {
// Unsafe binders may elaborate drops if their inner type isn't copy.
// This is enforced in typeck, so this should never happen.
self.tcx().dcx().span_delayed_bug(
self.source_info.span,
"open drop for unsafe binder shouldn't be encountered",
);
self.elaborator.patch().new_block(BasicBlockData {
statements: vec![],
terminator: Some(Terminator {
source_info: self.source_info,
kind: TerminatorKind::Unreachable,
}),
is_cleanup: self.unwind.is_cleanup(),
})
}

_ => span_bug!(self.source_info.span, "open drop from non-ADT `{:?}`", ty),
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/unsafe/move-out-of-non-copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ compile-flags: -Zvalidate-mir

// Regression test for <https://github.com/rust-lang/rust/issues/141394>.

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

use std::unsafe_binder::unwrap_binder;

fn id<T>(x: unsafe<> T) -> T {
//~^ ERROR the trait bound `T: Copy` is not satisfied
unsafe { unwrap_binder!(x) }
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/unsafe/move-out-of-non-copy.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0277]: the trait bound `T: Copy` is not satisfied
--> $DIR/move-out-of-non-copy.rs:10:13
|
LL | fn id<T>(x: unsafe<> T) -> T {
| ^^^^^^^^^^ the trait `Copy` is not implemented for `T`
|
help: consider restricting type parameter `T` with trait `Copy`
|
LL | fn id<T: std::marker::Copy>(x: unsafe<> T) -> T {
| +++++++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading