Skip to content

Commit 09188da

Browse files
committed
dangling pointer from temp cleanup
1 parent e5efc33 commit 09188da

19 files changed

+355
-333
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,17 @@ lint_dangling_pointers_from_locals = {$fn_kind} returns a dangling pointer to dr
197197
.ret_ty = return type is `{$ret_ty}`
198198
.local_var = local variable `{$local_var_name}` is dropped at the end of the {$fn_kind}
199199
.created_at = dangling pointer created here
200-
.note = a dangling pointer is safe, but dereferencing one is undefined behavior
201-
202-
lint_dangling_pointers_from_temporaries = a dangling pointer will be produced because the temporary `{$ty}` will be dropped
203-
.label_ptr = this pointer will immediately be invalid
204-
.label_temporary = this `{$ty}` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
205-
.note = pointers do not have a lifetime; when calling `{$callee}` the `{$ty}` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
206-
.help_bind = you must make sure that the variable you bind the `{$ty}` to lives at least as long as the pointer returned by the call to `{$callee}`
207-
.help_returned = in particular, if this pointer is returned from the current function, binding the `{$ty}` inside the function will not suffice
208-
.help_visit = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
200+
.note_safe = a dangling pointer is safe, but dereferencing one is undefined behavior
201+
.note_more_info = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
202+
203+
lint_dangling_pointers_from_temporaries = expression creates a dangling pointer to dropped temporary `{$ty}`
204+
.label_ptr = dangling pointer created here
205+
.label_temporary = this `{$ty}` is dropped at the end of the statement
206+
.help_bind = bind the `{$ty}` to a variable such that it outlives the pointer returned by `{$callee}`
207+
.note_safe = a dangling pointer is safe, but dereferencing one is undefined behavior
208+
.note_return = returning a pointer to a local variable will always result in a dangling pointer
209+
.note_more_info = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
210+
209211
210212
lint_default_hash_types = prefer `{$preferred}` over `{$used}`, it has better performance
211213
.note = a `use rustc_data_structures::fx::{$preferred}` may be necessary

compiler/rustc_lint/src/lints.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,10 @@ pub(crate) struct IgnoredUnlessCrateSpecified<'a> {
11061106
// dangling.rs
11071107
#[derive(LintDiagnostic)]
11081108
#[diag(lint_dangling_pointers_from_temporaries)]
1109-
#[note]
11101109
#[help(lint_help_bind)]
1111-
#[help(lint_help_returned)]
1112-
#[help(lint_help_visit)]
1110+
#[note(lint_note_safe)]
1111+
#[note(lint_note_return)]
1112+
#[note(lint_note_more_info)]
11131113
// FIXME: put #[primary_span] on `ptr_span` once it does not cause conflicts
11141114
pub(crate) struct DanglingPointersFromTemporaries<'tcx> {
11151115
pub callee: Ident,
@@ -1122,7 +1122,8 @@ pub(crate) struct DanglingPointersFromTemporaries<'tcx> {
11221122

11231123
#[derive(LintDiagnostic)]
11241124
#[diag(lint_dangling_pointers_from_locals)]
1125-
#[note]
1125+
#[note(lint_note_safe)]
1126+
#[note(lint_note_more_info)]
11261127
pub(crate) struct DanglingPointersFromLocals<'tcx> {
11271128
pub ret_ty: Ty<'tcx>,
11281129
#[label(lint_ret_ty)]

tests/ui/lint/dangling-pointers-from-locals.stderr

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LL | &x
99
| ^^
1010
|
1111
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
12+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1213
= note: `#[warn(dangling_pointers_from_locals)]` on by default
1314

1415
warning: function returns a dangling pointer to dropped local variable `x`
@@ -24,6 +25,7 @@ LL | x
2425
| ^
2526
|
2627
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
28+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2729

2830
warning: function returns a dangling pointer to dropped local variable `x`
2931
--> $DIR/dangling-pointers-from-locals.rs:24:12
@@ -38,6 +40,7 @@ LL | return y;
3840
| ^
3941
|
4042
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
43+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
4144

4245
warning: function returns a dangling pointer to dropped local variable `x`
4346
--> $DIR/dangling-pointers-from-locals.rs:30:5
@@ -52,6 +55,7 @@ LL | &x as *const u8
5255
| dangling pointer created here
5356
|
5457
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
58+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
5559

5660
warning: function returns a dangling pointer to dropped local variable `x`
5761
--> $DIR/dangling-pointers-from-locals.rs:37:5
@@ -66,6 +70,7 @@ LL | x as *const u8
6670
| ^^^^^^^^^^^^^^
6771
|
6872
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
73+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
6974

7075
warning: function returns a dangling pointer to dropped local variable `x`
7176
--> $DIR/dangling-pointers-from-locals.rs:43:12
@@ -80,6 +85,7 @@ LL | return &mut x as *mut u8 as *const u8 as *mut u8;
8085
| dangling pointer created here
8186
|
8287
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
88+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
8389

8490
warning: function returns a dangling pointer to dropped local variable `x`
8591
--> $DIR/dangling-pointers-from-locals.rs:49:5
@@ -92,6 +98,7 @@ LL | &{ x }
9298
| ^^^^^^
9399
|
94100
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
101+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
95102

96103
warning: function returns a dangling pointer to dropped local variable `x`
97104
--> $DIR/dangling-pointers-from-locals.rs:57:13
@@ -108,6 +115,7 @@ LL | | }
108115
| |_____________^
109116
|
110117
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
118+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
111119

112120
warning: function returns a dangling pointer to dropped local variable `x`
113121
--> $DIR/dangling-pointers-from-locals.rs:67:12
@@ -120,6 +128,7 @@ LL | return &x;
120128
| ^^
121129
|
122130
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
131+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
123132

124133
warning: function returns a dangling pointer to dropped local variable `x`
125134
--> $DIR/dangling-pointers-from-locals.rs:73:12
@@ -132,6 +141,7 @@ LL | return &mut x;
132141
| ^^^^^^
133142
|
134143
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
144+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
135145

136146
warning: function returns a dangling pointer to dropped local variable `x`
137147
--> $DIR/dangling-pointers-from-locals.rs:80:16
@@ -145,6 +155,7 @@ LL | return &x;
145155
| ^^
146156
|
147157
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
158+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
148159

149160
warning: function returns a dangling pointer to dropped local variable `x`
150161
--> $DIR/dangling-pointers-from-locals.rs:88:5
@@ -157,6 +168,7 @@ LL | &x
157168
| ^^
158169
|
159170
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
171+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
160172

161173
warning: function returns a dangling pointer to dropped local variable `x`
162174
--> $DIR/dangling-pointers-from-locals.rs:94:12
@@ -169,6 +181,7 @@ LL | return &x;
169181
| ^^
170182
|
171183
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
184+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
172185

173186
warning: closure returns a dangling pointer to dropped local variable `x`
174187
--> $DIR/dangling-pointers-from-locals.rs:101:16
@@ -181,6 +194,7 @@ LL | return &x;
181194
| ^^
182195
|
183196
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
197+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
184198

185199
warning: function returns a dangling pointer to dropped local variable `x`
186200
--> $DIR/dangling-pointers-from-locals.rs:113:5
@@ -194,6 +208,7 @@ LL | &x
194208
| ^^
195209
|
196210
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
211+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
197212

198213
warning: function returns a dangling pointer to dropped local variable `a`
199214
--> $DIR/dangling-pointers-from-locals.rs:118:5
@@ -206,6 +221,7 @@ LL | &a
206221
| ^^
207222
|
208223
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
224+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
209225

210226
warning: function returns a dangling pointer to dropped local variable `a`
211227
--> $DIR/dangling-pointers-from-locals.rs:123:5
@@ -218,6 +234,7 @@ LL | &a
218234
| ^^
219235
|
220236
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
237+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
221238

222239
warning: function returns a dangling pointer to dropped local variable `a`
223240
--> $DIR/dangling-pointers-from-locals.rs:128:5
@@ -230,6 +247,7 @@ LL | &a
230247
| ^^
231248
|
232249
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
250+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
233251

234252
warning: function returns a dangling pointer to dropped local variable `a`
235253
--> $DIR/dangling-pointers-from-locals.rs:133:5
@@ -242,6 +260,7 @@ LL | &a
242260
| ^^
243261
|
244262
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
263+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
245264

246265
warning: 19 warnings emitted
247266

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
#[deny(dangling_pointers_from_temporaries)]
88
{
99
dbg!(String::new().as_ptr());
10-
//~^ ERROR a dangling pointer will be produced because the temporary `String` will be dropped
10+
//~^ ERROR dangling pointer
1111
}
1212
S.foo()
1313
}
@@ -18,6 +18,6 @@ impl S {
1818
#[warn(dangling_pointers_from_temporaries)]
1919
fn foo(self) {
2020
dbg!(String::new().as_ptr());
21-
//~^ WARNING a dangling pointer will be produced because the temporary `String` will be dropped
21+
//~^ WARNING dangling pointer
2222
}
2323
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
error: a dangling pointer will be produced because the temporary `String` will be dropped
1+
error: expression creates a dangling pointer to dropped temporary `String`
22
--> $DIR/allow.rs:9:28
33
|
44
LL | dbg!(String::new().as_ptr());
5-
| ------------- ^^^^^^ this pointer will immediately be invalid
5+
| ------------- ^^^^^^ dangling pointer created here
66
| |
7-
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
7+
| this `String` is dropped at the end of the statement
88
|
9-
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10-
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
11-
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
12-
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
9+
= help: bind the `String` to a variable such that it outlives the pointer returned by `as_ptr`
10+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
11+
= note: returning a pointer to a local variable will always result in a dangling pointer
12+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1313
note: the lint level is defined here
1414
--> $DIR/allow.rs:7:12
1515
|
1616
LL | #[deny(dangling_pointers_from_temporaries)]
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
warning: a dangling pointer will be produced because the temporary `String` will be dropped
19+
warning: expression creates a dangling pointer to dropped temporary `String`
2020
--> $DIR/allow.rs:20:28
2121
|
2222
LL | dbg!(String::new().as_ptr());
23-
| ------------- ^^^^^^ this pointer will immediately be invalid
23+
| ------------- ^^^^^^ dangling pointer created here
2424
| |
25-
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
25+
| this `String` is dropped at the end of the statement
2626
|
27-
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28-
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
29-
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
30-
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
27+
= help: bind the `String` to a variable such that it outlives the pointer returned by `as_ptr`
28+
= note: a dangling pointer is safe, but dereferencing one is undefined behavior
29+
= note: returning a pointer to a local variable will always result in a dangling pointer
30+
= note: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
3131
note: the lint level is defined here
3232
--> $DIR/allow.rs:18:12
3333
|

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ fn ok() {
2525
fn not_ok() {
2626
{
2727
let ptr = cstring().as_ptr();
28-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
28+
//~^ ERROR dangling pointer
2929
consume(ptr);
3030
}
3131
consume({
3232
let ptr = cstring().as_ptr();
33-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
33+
//~^ ERROR dangling pointer
3434
ptr
3535
});
3636
consume({
@@ -39,11 +39,11 @@ fn not_ok() {
3939
//^ FIXME: should error
4040
});
4141
let _ptr: *const u8 = cstring().as_ptr().cast();
42-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
42+
//~^ ERROR dangling pointer
4343
let _ptr: *const u8 = { cstring() }.as_ptr().cast();
44-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
44+
//~^ ERROR dangling pointer
4545
let _ptr: *const u8 = { cstring().as_ptr() }.cast();
46-
//~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
46+
//~^ ERROR dangling pointer
4747
}
4848

4949
fn main() {

0 commit comments

Comments
 (0)