Skip to content

Commit e107194

Browse files
committed
Ignore AscribeUserType in unsafeck to avoid duplicate diagnostics.
1 parent 4462bb5 commit e107194

File tree

6 files changed

+17
-33
lines changed

6 files changed

+17
-33
lines changed

compiler/rustc_mir_transform/src/check_unsafety.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
100100
| StatementKind::StorageLive(..)
101101
| StatementKind::StorageDead(..)
102102
| StatementKind::Retag { .. }
103-
| StatementKind::AscribeUserType(..)
104103
| StatementKind::PlaceMention(..)
105104
| StatementKind::Coverage(..)
106105
| StatementKind::Intrinsic(..)
107106
| StatementKind::ConstEvalCounter
108107
| StatementKind::Nop => {
109108
// safe (at least as emitted during MIR construction)
110109
}
110+
// `AscribeUserType` just exists to help MIR borrowck. It has no semantics, and
111+
// everything is already reported by `PlaceMention`.
112+
StatementKind::AscribeUserType(..) => return,
111113
}
112114
self.super_statement(statement, location);
113115
}

tests/ui/binding/issue-53114-safety-checks.rs

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ fn let_ascribe_gets_unsafe_field() {
3636
let p = P { a: &2, b: &3 };
3737
let _: _ = &p.b; //~ ERROR reference to packed field
3838
let _: _ = u1.a; //~ ERROR [E0133]
39-
//~^ ERROR [E0133]
4039
let _: _ = &u2.a; //~ ERROR [E0133]
4140

4241
// variation on above with `_` in substructure

tests/ui/binding/issue-53114-safety-checks.stderr

+11-19
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LL | let _: _ = &p.b;
2626
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
2727

2828
error[E0793]: reference to packed field is unaligned
29-
--> $DIR/issue-53114-safety-checks.rs:43:20
29+
--> $DIR/issue-53114-safety-checks.rs:42:20
3030
|
3131
LL | let (_,): _ = (&p.b,);
3232
| ^^^^
@@ -35,7 +35,7 @@ LL | let (_,): _ = (&p.b,);
3535
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
3636

3737
error[E0793]: reference to packed field is unaligned
38-
--> $DIR/issue-53114-safety-checks.rs:52:11
38+
--> $DIR/issue-53114-safety-checks.rs:51:11
3939
|
4040
LL | match &p.b { _ => { } }
4141
| ^^^^
@@ -44,7 +44,7 @@ LL | match &p.b { _ => { } }
4444
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
4545

4646
error[E0793]: reference to packed field is unaligned
47-
--> $DIR/issue-53114-safety-checks.rs:57:12
47+
--> $DIR/issue-53114-safety-checks.rs:56:12
4848
|
4949
LL | match (&p.b,) { (_,) => { } }
5050
| ^^^^
@@ -93,70 +93,62 @@ LL | let _: _ = u1.a;
9393
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
9494

9595
error[E0133]: access to union field is unsafe and requires unsafe function or block
96-
--> $DIR/issue-53114-safety-checks.rs:38:12
97-
|
98-
LL | let _: _ = u1.a;
99-
| ^ access to union field
100-
|
101-
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
102-
103-
error[E0133]: access to union field is unsafe and requires unsafe function or block
104-
--> $DIR/issue-53114-safety-checks.rs:40:16
96+
--> $DIR/issue-53114-safety-checks.rs:39:16
10597
|
10698
LL | let _: _ = &u2.a;
10799
| ^^^^^ access to union field
108100
|
109101
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
110102

111103
error[E0133]: access to union field is unsafe and requires unsafe function or block
112-
--> $DIR/issue-53114-safety-checks.rs:44:20
104+
--> $DIR/issue-53114-safety-checks.rs:43:20
113105
|
114106
LL | let (_,): _ = (u1.a,);
115107
| ^^^^ access to union field
116108
|
117109
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
118110

119111
error[E0133]: access to union field is unsafe and requires unsafe function or block
120-
--> $DIR/issue-53114-safety-checks.rs:45:20
112+
--> $DIR/issue-53114-safety-checks.rs:44:20
121113
|
122114
LL | let (_,): _ = (&u2.a,);
123115
| ^^^^^ access to union field
124116
|
125117
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
126118

127119
error[E0133]: access to union field is unsafe and requires unsafe function or block
128-
--> $DIR/issue-53114-safety-checks.rs:53:11
120+
--> $DIR/issue-53114-safety-checks.rs:52:11
129121
|
130122
LL | match u1.a { _ => { } }
131123
| ^^^^ access to union field
132124
|
133125
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
134126

135127
error[E0133]: access to union field is unsafe and requires unsafe function or block
136-
--> $DIR/issue-53114-safety-checks.rs:54:11
128+
--> $DIR/issue-53114-safety-checks.rs:53:11
137129
|
138130
LL | match &u2.a { _ => { } }
139131
| ^^^^^ access to union field
140132
|
141133
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
142134

143135
error[E0133]: access to union field is unsafe and requires unsafe function or block
144-
--> $DIR/issue-53114-safety-checks.rs:58:12
136+
--> $DIR/issue-53114-safety-checks.rs:57:12
145137
|
146138
LL | match (u1.a,) { (_,) => { } }
147139
| ^^^^ access to union field
148140
|
149141
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
150142

151143
error[E0133]: access to union field is unsafe and requires unsafe function or block
152-
--> $DIR/issue-53114-safety-checks.rs:59:12
144+
--> $DIR/issue-53114-safety-checks.rs:58:12
153145
|
154146
LL | match (&u2.a,) { (_,) => { } }
155147
| ^^^^^ access to union field
156148
|
157149
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
158150

159-
error: aborting due to 19 previous errors
151+
error: aborting due to 18 previous errors
160152

161153
Some errors have detailed explanations: E0133, E0793.
162154
For more information about an error, try `rustc --explain E0133`.

tests/ui/unsafe/unsafe-fn-deref-ptr.mir.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,13 @@ LL | let _: u8 = *p;
1515
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
1616

1717
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
18-
--> $DIR/unsafe-fn-deref-ptr.rs:6:12
19-
|
20-
LL | let _: u8 = *p;
21-
| ^^ dereference of raw pointer
22-
|
23-
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
24-
25-
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
26-
--> $DIR/unsafe-fn-deref-ptr.rs:8:12
18+
--> $DIR/unsafe-fn-deref-ptr.rs:7:12
2719
|
2820
LL | return *p;
2921
| ^^ dereference of raw pointer
3022
|
3123
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
3224

33-
error: aborting due to 4 previous errors
25+
error: aborting due to 3 previous errors
3426

3527
For more information about this error, try `rustc --explain E0133`.

tests/ui/unsafe/unsafe-fn-deref-ptr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
fn f(p: *const u8) -> u8 {
55
let _ = *p; //~ ERROR dereference of raw pointer is unsafe
66
let _: u8 = *p; //~ ERROR dereference of raw pointer is unsafe
7-
//[mir]~^ ERROR dereference of raw pointer is unsafe
87
return *p; //~ ERROR dereference of raw pointer is unsafe
98
}
109

tests/ui/unsafe/unsafe-fn-deref-ptr.thir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | let _: u8 = *p;
1515
= note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
1616

1717
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
18-
--> $DIR/unsafe-fn-deref-ptr.rs:8:12
18+
--> $DIR/unsafe-fn-deref-ptr.rs:7:12
1919
|
2020
LL | return *p;
2121
| ^^ dereference of raw pointer

0 commit comments

Comments
 (0)