Skip to content

Commit 8ec94d3

Browse files
committed
Update dangling pointer tests
1 parent fdef65b commit 8ec94d3

File tree

5 files changed

+50
-20
lines changed

5 files changed

+50
-20
lines changed

compiler/rustc_lint/src/dangling.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,14 @@ fn owns_allocation(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool {
211211
|| inner.ty_adt_def().is_some_and(|def| tcx.is_lang_item(def.did(), LangItem::CStr))
212212
|| owns_allocation(tcx, inner)
213213
} else if let Some(def) = ty.ty_adt_def() {
214-
for lang_item in [LangItem::String, LangItem::MaybeUninit] {
214+
for lang_item in [LangItem::String, LangItem::MaybeUninit, LangItem::UnsafeCell] {
215215
if tcx.is_lang_item(def.did(), lang_item) {
216216
return true;
217217
}
218218
}
219-
tcx.get_diagnostic_name(def.did())
220-
.is_some_and(|name| matches!(name, sym::cstring_type | sym::Vec | sym::Cell))
219+
tcx.get_diagnostic_name(def.did()).is_some_and(|name| {
220+
matches!(name, sym::cstring_type | sym::Vec | sym::Cell | sym::sync_unsafe_cell)
221+
})
221222
} else {
222223
false
223224
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,7 @@ symbols! {
19301930
surface_async_drop_in_place,
19311931
sym,
19321932
sync,
1933+
sync_unsafe_cell,
19331934
synthetic,
19341935
t32,
19351936
target,

library/core/src/cell.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,7 @@ impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T>
22732273
/// See [`UnsafeCell`] for details.
22742274
#[unstable(feature = "sync_unsafe_cell", issue = "95439")]
22752275
#[repr(transparent)]
2276+
#[rustc_diagnostic_item = "sync_unsafe_cell"]
22762277
#[rustc_pub_transparent]
22772278
pub struct SyncUnsafeCell<T: ?Sized> {
22782279
value: UnsafeCell<T>,

tests/ui/lint/dangling-pointers-from-temporaries/types.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![deny(dangling_pointers_from_temporaries)]
2+
#![feature(sync_unsafe_cell)]
23

3-
use std::cell::Cell;
4+
use std::cell::{Cell, SyncUnsafeCell, UnsafeCell};
45
use std::ffi::{CStr, CString};
56
use std::mem::MaybeUninit;
67

@@ -47,6 +48,10 @@ fn main() {
4748
//~^ ERROR a dangling pointer will be produced because the temporary `MaybeUninit<u8>` will be dropped
4849
declval::<Vec<AsPtrFake>>().as_ptr();
4950
//~^ ERROR a dangling pointer will be produced because the temporary `Vec<AsPtrFake>` will be dropped
51+
declval::<UnsafeCell<u8>>().get();
52+
//~^ ERROR a dangling pointer will be produced because the temporary `UnsafeCell<u8>` will be dropped
53+
declval::<SyncUnsafeCell<u8>>().get();
54+
//~^ ERROR a dangling pointer will be produced because the temporary `SyncUnsafeCell<u8>` will be dropped
5055
declval::<Box<AsPtrFake>>().as_ptr();
5156
declval::<AsPtrFake>().as_ptr();
5257
}

tests/ui/lint/dangling-pointers-from-temporaries/types.stderr

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: a dangling pointer will be produced because the temporary `CString` will be dropped
2-
--> $DIR/types.rs:20:26
2+
--> $DIR/types.rs:21:26
33
|
44
LL | declval::<CString>().as_ptr();
55
| -------------------- ^^^^^^ this pointer will immediately be invalid
@@ -15,7 +15,7 @@ LL | #![deny(dangling_pointers_from_temporaries)]
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

1717
error: a dangling pointer will be produced because the temporary `String` will be dropped
18-
--> $DIR/types.rs:22:25
18+
--> $DIR/types.rs:23:25
1919
|
2020
LL | declval::<String>().as_ptr();
2121
| ------------------- ^^^^^^ this pointer will immediately be invalid
@@ -26,7 +26,7 @@ LL | declval::<String>().as_ptr();
2626
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2727

2828
error: a dangling pointer will be produced because the temporary `Vec<u8>` will be dropped
29-
--> $DIR/types.rs:24:26
29+
--> $DIR/types.rs:25:26
3030
|
3131
LL | declval::<Vec<u8>>().as_ptr();
3232
| -------------------- ^^^^^^ this pointer will immediately be invalid
@@ -37,7 +37,7 @@ LL | declval::<Vec<u8>>().as_ptr();
3737
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
3838

3939
error: a dangling pointer will be produced because the temporary `Box<CString>` will be dropped
40-
--> $DIR/types.rs:26:31
40+
--> $DIR/types.rs:27:31
4141
|
4242
LL | declval::<Box<CString>>().as_ptr();
4343
| ------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -48,7 +48,7 @@ LL | declval::<Box<CString>>().as_ptr();
4848
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
4949

5050
error: a dangling pointer will be produced because the temporary `Box<[u8]>` will be dropped
51-
--> $DIR/types.rs:28:28
51+
--> $DIR/types.rs:29:28
5252
|
5353
LL | declval::<Box<[u8]>>().as_ptr();
5454
| ---------------------- ^^^^^^ this pointer will immediately be invalid
@@ -59,7 +59,7 @@ LL | declval::<Box<[u8]>>().as_ptr();
5959
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
6060

6161
error: a dangling pointer will be produced because the temporary `Box<str>` will be dropped
62-
--> $DIR/types.rs:30:27
62+
--> $DIR/types.rs:31:27
6363
|
6464
LL | declval::<Box<str>>().as_ptr();
6565
| --------------------- ^^^^^^ this pointer will immediately be invalid
@@ -70,7 +70,7 @@ LL | declval::<Box<str>>().as_ptr();
7070
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
7171

7272
error: a dangling pointer will be produced because the temporary `Box<CStr>` will be dropped
73-
--> $DIR/types.rs:32:28
73+
--> $DIR/types.rs:33:28
7474
|
7575
LL | declval::<Box<CStr>>().as_ptr();
7676
| ---------------------- ^^^^^^ this pointer will immediately be invalid
@@ -81,7 +81,7 @@ LL | declval::<Box<CStr>>().as_ptr();
8181
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
8282

8383
error: a dangling pointer will be produced because the temporary `[u8; 10]` will be dropped
84-
--> $DIR/types.rs:34:27
84+
--> $DIR/types.rs:35:27
8585
|
8686
LL | declval::<[u8; 10]>().as_ptr();
8787
| --------------------- ^^^^^^ this pointer will immediately be invalid
@@ -92,7 +92,7 @@ LL | declval::<[u8; 10]>().as_ptr();
9292
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
9393

9494
error: a dangling pointer will be produced because the temporary `Box<[u8; 10]>` will be dropped
95-
--> $DIR/types.rs:36:32
95+
--> $DIR/types.rs:37:32
9696
|
9797
LL | declval::<Box<[u8; 10]>>().as_ptr();
9898
| -------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -103,7 +103,7 @@ LL | declval::<Box<[u8; 10]>>().as_ptr();
103103
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
104104

105105
error: a dangling pointer will be produced because the temporary `Box<Vec<u8>>` will be dropped
106-
--> $DIR/types.rs:38:31
106+
--> $DIR/types.rs:39:31
107107
|
108108
LL | declval::<Box<Vec<u8>>>().as_ptr();
109109
| ------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -114,7 +114,7 @@ LL | declval::<Box<Vec<u8>>>().as_ptr();
114114
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
115115

116116
error: a dangling pointer will be produced because the temporary `Box<String>` will be dropped
117-
--> $DIR/types.rs:40:30
117+
--> $DIR/types.rs:41:30
118118
|
119119
LL | declval::<Box<String>>().as_ptr();
120120
| ------------------------ ^^^^^^ this pointer will immediately be invalid
@@ -125,7 +125,7 @@ LL | declval::<Box<String>>().as_ptr();
125125
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
126126

127127
error: a dangling pointer will be produced because the temporary `Box<Box<Box<Box<[u8]>>>>` will be dropped
128-
--> $DIR/types.rs:42:43
128+
--> $DIR/types.rs:43:43
129129
|
130130
LL | declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr();
131131
| ------------------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -136,7 +136,7 @@ LL | declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr();
136136
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
137137

138138
error: a dangling pointer will be produced because the temporary `Cell<u8>` will be dropped
139-
--> $DIR/types.rs:44:27
139+
--> $DIR/types.rs:45:27
140140
|
141141
LL | declval::<Cell<u8>>().as_ptr();
142142
| --------------------- ^^^^^^ this pointer will immediately be invalid
@@ -147,7 +147,7 @@ LL | declval::<Cell<u8>>().as_ptr();
147147
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
148148

149149
error: a dangling pointer will be produced because the temporary `MaybeUninit<u8>` will be dropped
150-
--> $DIR/types.rs:46:34
150+
--> $DIR/types.rs:47:34
151151
|
152152
LL | declval::<MaybeUninit<u8>>().as_ptr();
153153
| ---------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -158,7 +158,7 @@ LL | declval::<MaybeUninit<u8>>().as_ptr();
158158
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
159159

160160
error: a dangling pointer will be produced because the temporary `Vec<AsPtrFake>` will be dropped
161-
--> $DIR/types.rs:48:33
161+
--> $DIR/types.rs:49:33
162162
|
163163
LL | declval::<Vec<AsPtrFake>>().as_ptr();
164164
| --------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -168,5 +168,27 @@ LL | declval::<Vec<AsPtrFake>>().as_ptr();
168168
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<AsPtrFake>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
169169
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
170170

171-
error: aborting due to 15 previous errors
171+
error: a dangling pointer will be produced because the temporary `UnsafeCell<u8>` will be dropped
172+
--> $DIR/types.rs:51:33
173+
|
174+
LL | declval::<UnsafeCell<u8>>().get();
175+
| --------------------------- ^^^ this pointer will immediately be invalid
176+
| |
177+
| this `UnsafeCell<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
178+
|
179+
= note: pointers do not have a lifetime; when calling `get` the `UnsafeCell<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
180+
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
181+
182+
error: a dangling pointer will be produced because the temporary `SyncUnsafeCell<u8>` will be dropped
183+
--> $DIR/types.rs:53:37
184+
|
185+
LL | declval::<SyncUnsafeCell<u8>>().get();
186+
| ------------------------------- ^^^ this pointer will immediately be invalid
187+
| |
188+
| this `SyncUnsafeCell<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
189+
|
190+
= note: pointers do not have a lifetime; when calling `get` the `SyncUnsafeCell<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
191+
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
192+
193+
error: aborting due to 17 previous errors
172194

0 commit comments

Comments
 (0)