Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1b8413e

Browse files
compiler-errorscuviper
authored andcommittedJan 10, 2025
Don't do AccessDepth::Drop for types with no drop impl
(cherry picked from commit 4a099b2)
1 parent 273873a commit 1b8413e

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed
 

‎compiler/rustc_borrowck/src/lib.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
11661166
fn check_backward_incompatible_drop(
11671167
&mut self,
11681168
location: Location,
1169-
place_span: (Place<'tcx>, Span),
1169+
(place, place_span): (Place<'tcx>, Span),
11701170
state: &BorrowckDomain,
11711171
) {
1172-
let sd = AccessDepth::Drop;
1172+
let tcx = self.infcx.tcx;
1173+
// If this type does not need `Drop`, then treat it like a `StorageDead`.
1174+
// This is needed because we track the borrows of refs to thread locals,
1175+
// and we'll ICE because we don't track borrows behind shared references.
1176+
let sd = if place.ty(self.body, tcx).ty.needs_drop(tcx, self.body.typing_env(tcx)) {
1177+
AccessDepth::Drop
1178+
} else {
1179+
AccessDepth::Shallow(None)
1180+
};
11731181

11741182
let borrows_in_scope = self.borrows_in_scope(location, state);
11751183

@@ -1179,7 +1187,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
11791187
self,
11801188
self.infcx.tcx,
11811189
self.body,
1182-
(sd, place_span.0),
1190+
(sd, place),
11831191
self.borrow_set,
11841192
|borrow_index| borrows_in_scope.contains(borrow_index),
11851193
|this, _borrow_index, borrow| {
@@ -1190,7 +1198,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
11901198
this.infcx.tcx.emit_node_span_lint(
11911199
TAIL_EXPR_DROP_ORDER,
11921200
CRATE_HIR_ID,
1193-
place_span.1,
1201+
place_span,
11941202
session_diagnostics::TailExprDropOrder { borrowed },
11951203
);
11961204
// We may stop at the first case
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//@ check-pass
2+
3+
#![feature(thread_local)]
4+
#![deny(tail_expr_drop_order)]
5+
6+
use std::marker::PhantomData;
7+
use std::ops::{Deref, DerefMut};
8+
9+
pub struct Global;
10+
11+
#[thread_local]
12+
static REENTRANCY_STATE: State<Global> = State { marker: PhantomData, controller: Global };
13+
14+
pub struct Token(PhantomData<*mut ()>);
15+
16+
pub fn with_mut<T>(f: impl FnOnce(&mut Token) -> T) -> T {
17+
f(&mut REENTRANCY_STATE.borrow_mut())
18+
}
19+
20+
pub struct State<T: ?Sized = Global> {
21+
marker: PhantomData<*mut ()>,
22+
controller: T,
23+
}
24+
25+
impl<T: ?Sized> State<T> {
26+
pub fn borrow_mut(&self) -> TokenMut<'_, T> {
27+
todo!()
28+
}
29+
}
30+
31+
pub struct TokenMut<'a, T: ?Sized = Global> {
32+
state: &'a State<T>,
33+
token: Token,
34+
}
35+
36+
impl<T> Deref for TokenMut<'_, T> {
37+
type Target = Token;
38+
39+
fn deref(&self) -> &Self::Target {
40+
todo!()
41+
}
42+
}
43+
44+
impl<T> DerefMut for TokenMut<'_, T> {
45+
fn deref_mut(&mut self) -> &mut Self::Target {
46+
todo!()
47+
}
48+
}
49+
50+
impl<T: ?Sized> Drop for TokenMut<'_, T> {
51+
fn drop(&mut self) {
52+
todo!()
53+
}
54+
}
55+
56+
fn main() {}

0 commit comments

Comments
 (0)
Failed to load comments.