Skip to content

Commit e707f1a

Browse files
committed
Improve note for the invalid_reference_casting lint
Add link to the book interior mutability chapter, https://doc.rust-lang.org/book/ch15-05-interior-mutability.html.
1 parent b4d09f3 commit e707f1a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ lint_invalid_reference_casting_assign_to_ref = assigning to `&T` is undefined be
323323
lint_invalid_reference_casting_borrow_as_mut = casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
324324
.label = casting happend here
325325
326+
lint_invalid_reference_casting_note_book = for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
327+
326328
lint_lintpass_by_hand = implementing `LintPass` by hand
327329
.help = try using `declare_lint_pass!` or `impl_lint_pass!` instead
328330

compiler/rustc_lint/src/lints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,13 @@ pub enum InvalidFromUtf8Diag {
764764
#[derive(LintDiagnostic)]
765765
pub enum InvalidReferenceCastingDiag {
766766
#[diag(lint_invalid_reference_casting_borrow_as_mut)]
767+
#[note(lint_invalid_reference_casting_note_book)]
767768
BorrowAsMut {
768769
#[label]
769770
orig_cast: Option<Span>,
770771
},
771772
#[diag(lint_invalid_reference_casting_assign_to_ref)]
773+
#[note(lint_invalid_reference_casting_note_book)]
772774
AssignToRef {
773775
#[label]
774776
orig_cast: Option<Span>,

tests/ui/const-generics/issues/issue-100313.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
44
LL | *(B as *const bool as *mut bool) = false;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
78
= note: `#[deny(invalid_reference_casting)]` on by default
89

910
error[E0080]: evaluation of constant value failed

tests/ui/lint/reference_casting.stderr

+49
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,80 @@ error: casting `&T` to `&mut T` is undefined behavior, even if the reference is
44
LL | let _num = &mut *(num as *const i32 as *mut i32);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
78
= note: `#[deny(invalid_reference_casting)]` on by default
89

910
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
1011
--> $DIR/reference_casting.rs:21:16
1112
|
1213
LL | let _num = &mut *(num as *const i32).cast_mut();
1314
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
1417

1518
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
1619
--> $DIR/reference_casting.rs:23:16
1720
|
1821
LL | let _num = &mut *std::ptr::from_ref(num).cast_mut();
1922
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
2025

2126
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
2227
--> $DIR/reference_casting.rs:25:16
2328
|
2429
LL | let _num = &mut *std::ptr::from_ref({ num }).cast_mut();
2530
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
2633

2734
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
2835
--> $DIR/reference_casting.rs:27:16
2936
|
3037
LL | let _num = &mut *{ std::ptr::from_ref(num) }.cast_mut();
3138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
3241

3342
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
3443
--> $DIR/reference_casting.rs:29:16
3544
|
3645
LL | let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
3746
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
3849

3950
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
4051
--> $DIR/reference_casting.rs:31:16
4152
|
4253
LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut();
4354
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
|
56+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
4457

4558
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
4659
--> $DIR/reference_casting.rs:33:16
4760
|
4861
LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut().cast_const().cast_mut();
4962
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
5065

5166
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
5267
--> $DIR/reference_casting.rs:35:16
5368
|
5469
LL | let _num = &mut *(std::ptr::from_ref(static_u8()) as *mut i32);
5570
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
|
72+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
5673

5774
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
5875
--> $DIR/reference_casting.rs:37:16
5976
|
6077
LL | let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
6178
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
|
80+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
6281

6382
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
6483
--> $DIR/reference_casting.rs:41:16
@@ -67,6 +86,8 @@ LL | let deferred = num as *const i32 as *mut i32;
6786
| ----------------------------- casting happend here
6887
LL | let _num = &mut *deferred;
6988
| ^^^^^^^^^^^^^^
89+
|
90+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
7091

7192
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
7293
--> $DIR/reference_casting.rs:44:16
@@ -75,60 +96,80 @@ LL | let deferred = (std::ptr::from_ref(num) as *const i32 as *const i32).ca
7596
| ---------------------------------------------------------------------------- casting happend here
7697
LL | let _num = &mut *deferred;
7798
| ^^^^^^^^^^^^^^
99+
|
100+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
78101

79102
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
80103
--> $DIR/reference_casting.rs:46:16
81104
|
82105
LL | let _num = &mut *(num as *const _ as usize as *mut i32);
83106
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107+
|
108+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
84109

85110
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
86111
--> $DIR/reference_casting.rs:50:9
87112
|
88113
LL | &mut *((this as *const _) as *mut _)
89114
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115+
|
116+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
90117

91118
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
92119
--> $DIR/reference_casting.rs:60:5
93120
|
94121
LL | *(a as *const _ as *mut _) = String::from("Replaced");
95122
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123+
|
124+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
96125

97126
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
98127
--> $DIR/reference_casting.rs:62:5
99128
|
100129
LL | *(a as *const _ as *mut String) += " world";
101130
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131+
|
132+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
102133

103134
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
104135
--> $DIR/reference_casting.rs:64:5
105136
|
106137
LL | *std::ptr::from_ref(num).cast_mut() += 1;
107138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139+
|
140+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
108141

109142
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
110143
--> $DIR/reference_casting.rs:66:5
111144
|
112145
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
113146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147+
|
148+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
114149

115150
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
116151
--> $DIR/reference_casting.rs:68:5
117152
|
118153
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
119154
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155+
|
156+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
120157

121158
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
122159
--> $DIR/reference_casting.rs:70:5
123160
|
124161
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
125162
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163+
|
164+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
126165

127166
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
128167
--> $DIR/reference_casting.rs:72:5
129168
|
130169
LL | *std::mem::transmute::<_, *mut i32>(num) += 1;
131170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
171+
|
172+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
132173

133174
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
134175
--> $DIR/reference_casting.rs:76:5
@@ -137,24 +178,32 @@ LL | let value = num as *const i32 as *mut i32;
137178
| ----------------------------- casting happend here
138179
LL | *value = 1;
139180
| ^^^^^^^^^^
181+
|
182+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
140183

141184
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
142185
--> $DIR/reference_casting.rs:78:5
143186
|
144187
LL | *(num as *const i32).cast::<i32>().cast_mut() = 2;
145188
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189+
|
190+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
146191

147192
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
148193
--> $DIR/reference_casting.rs:80:5
149194
|
150195
LL | *(num as *const _ as usize as *mut i32) = 2;
151196
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
197+
|
198+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
152199

153200
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
154201
--> $DIR/reference_casting.rs:84:9
155202
|
156203
LL | *(this as *const _ as *mut _) = a;
157204
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
205+
|
206+
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
158207

159208
error: aborting due to 25 previous errors
160209

0 commit comments

Comments
 (0)