Skip to content

Commit 624ce6f

Browse files
committed
Improve AST validation strategies
1 parent 2514c9d commit 624ce6f

File tree

6 files changed

+194
-118
lines changed

6 files changed

+194
-118
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'a> AstValidator<'a> {
221221
}
222222
TyKind::AnonStruct(ref fields, ..) | TyKind::AnonUnion(ref fields, ..) => {
223223
// self.with_banned_assoc_ty_bound(|this| {
224-
walk_list!(self, visit_struct_field_def, fields)
224+
walk_list!(self, visit_field_def, fields)
225225
// });
226226
}
227227
_ => visit::walk_ty(self, t),
@@ -231,7 +231,7 @@ impl<'a> AstValidator<'a> {
231231
fn visit_struct_field_def(&mut self, field: &'a FieldDef) {
232232
if let Some(ident) = field.ident &&
233233
ident.name == kw::Underscore {
234-
self.check_unnamed_field_ty(&field.ty, field.span);
234+
self.check_unnamed_field_ty(&field.ty, ident.span);
235235
self.visit_vis(&field.vis);
236236
self.visit_ident(ident);
237237
self.visit_ty_common(&field.ty);
@@ -279,6 +279,8 @@ impl<'a> AstValidator<'a> {
279279
}
280280
}
281281

282+
#[allow(rustc::untranslatable_diagnostic)]
283+
#[allow(rustc::diagnostic_outside_of_impl)]
282284
fn check_unnamed_field_ty(&self, ty: &Ty, span: Span) {
283285
if matches!(
284286
&ty.kind,
@@ -296,6 +298,8 @@ impl<'a> AstValidator<'a> {
296298
self.err_handler().struct_span_err(span, msg).span_label(ty.span, label).emit();
297299
}
298300

301+
#[allow(rustc::untranslatable_diagnostic)]
302+
#[allow(rustc::diagnostic_outside_of_impl)]
299303
fn deny_anon_struct_or_union(&self, ty: &Ty) {
300304
let struct_or_union = match &ty.kind {
301305
TyKind::AnonStruct(..) => "struct",
@@ -311,15 +315,17 @@ impl<'a> AstValidator<'a> {
311315
.emit();
312316
}
313317

318+
#[allow(rustc::untranslatable_diagnostic)]
319+
#[allow(rustc::diagnostic_outside_of_impl)]
314320
fn deny_unnamed_field(&self, field: &FieldDef) {
315321
if let Some(ident) = field.ident &&
316322
ident.name == kw::Underscore {
317323
self.err_handler()
318324
.struct_span_err(
319325
field.span,
320-
"anonymous fields are not allowed outside of structs or unions",
326+
"unnamed fields are not allowed outside of structs or unions",
321327
)
322-
.span_label(ident.span, "anonymous field declared here")
328+
.span_label(ident.span, "unnamed field declared here")
323329
.emit();
324330
}
325331
}
@@ -849,7 +855,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
849855

850856
fn visit_ty(&mut self, ty: &'a Ty) {
851857
self.visit_ty_common(ty);
852-
tracing::info!(?ty);
853858
self.deny_anon_struct_or_union(ty);
854859
self.walk_ty(ty)
855860
}

compiler/rustc_ast_passes/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#![feature(iter_is_partitioned)]
1010
#![feature(let_chains)]
1111
#![recursion_limit = "256"]
12-
// FIXME(unnamed_field): unncomment these two lints
13-
// #![deny(rustc::untranslatable_diagnostic)]
14-
// #![deny(rustc::diagnostic_outside_of_impl)]
12+
#![deny(rustc::untranslatable_diagnostic)]
13+
#![deny(rustc::diagnostic_outside_of_impl)]
1514

1615
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
1716
use rustc_fluent_macro::fluent_messages;

tests/ui/union/unnamed-fields/restrict_anonymous_structs.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,40 @@ fn f2(a: struct { field: u8 } ) {} //~ ERROR anonymous structs are not allowed o
99

1010

1111
struct F {
12-
field: struct { field: u8 } //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
12+
field: struct { field: u8 }, //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
1313
//~^ ERROR anonymous structs are unimplemented
14+
_: struct { field: u8 },
15+
//~^ ERROR anonymous structs are unimplemented
16+
}
17+
18+
struct G {
19+
_: (u8, u8), //~ ERROR unnamed fields can only have struct or union types
1420
}
1521

16-
union G {
17-
field1: struct { field: u8 } //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
22+
union H {
23+
field: struct { field: u8 }, //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
24+
//~^ ERROR anonymous structs are unimplemented
25+
_: struct { field: u8 },
1826
//~^ ERROR anonymous structs are unimplemented
1927
}
2028

21-
struct I(struct { field: u8 }, u8); //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
29+
union I {
30+
_: (u8, u8), //~ ERROR unnamed fields can only have struct or union types
31+
}
32+
33+
struct J(struct { field: u8 }, u8); //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
2234
//~^ ERROR anonymous structs are unimplemented
2335

24-
enum J {
25-
K(struct { field: u8 }), //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
36+
enum K {
37+
L(struct { field: u8 }), //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
2638
//~^ ERROR anonymous structs are unimplemented
27-
L {
28-
_ : struct { field: u8 } //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
29-
//~^ ERROR anonymous fields are not allowed outside of structs or unions
39+
M {
40+
_ : struct { field: u8 }, //~ ERROR anonymous structs are not allowed outside of unnamed struct or union fields
41+
//~^ ERROR unnamed fields are not allowed outside of structs or unions
3042
//~| ERROR anonymous structs are unimplemented
3143
},
32-
M {
33-
_ : u8 //~ ERROR anonymous fields are not allowed outside of structs or unions
44+
N {
45+
_ : u8, //~ ERROR unnamed fields are not allowed outside of structs or unions
3446
}
3547
}
3648

tests/ui/union/unnamed-fields/restrict_anonymous_structs.stderr

+69-45
Original file line numberDiff line numberDiff line change
@@ -13,93 +13,105 @@ LL | fn f2(a: struct { field: u8 } ) {}
1313
error: anonymous structs are not allowed outside of unnamed struct or union fields
1414
--> $DIR/restrict_anonymous_structs.rs:12:12
1515
|
16-
LL | field: struct { field: u8 }
16+
LL | field: struct { field: u8 },
1717
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
1818

19+
error: unnamed fields can only have struct or union types
20+
--> $DIR/restrict_anonymous_structs.rs:19:5
21+
|
22+
LL | _: (u8, u8),
23+
| ^ -------- not a struct or union
24+
1925
error: anonymous structs are not allowed outside of unnamed struct or union fields
20-
--> $DIR/restrict_anonymous_structs.rs:17:13
26+
--> $DIR/restrict_anonymous_structs.rs:23:12
2127
|
22-
LL | field1: struct { field: u8 }
23-
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
28+
LL | field: struct { field: u8 },
29+
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
30+
31+
error: unnamed fields can only have struct or union types
32+
--> $DIR/restrict_anonymous_structs.rs:30:5
33+
|
34+
LL | _: (u8, u8),
35+
| ^ -------- not a struct or union
2436

2537
error: anonymous structs are not allowed outside of unnamed struct or union fields
26-
--> $DIR/restrict_anonymous_structs.rs:21:10
38+
--> $DIR/restrict_anonymous_structs.rs:33:10
2739
|
28-
LL | struct I(struct { field: u8 }, u8);
40+
LL | struct J(struct { field: u8 }, u8);
2941
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
3042

3143
error: anonymous structs are not allowed outside of unnamed struct or union fields
32-
--> $DIR/restrict_anonymous_structs.rs:25:7
44+
--> $DIR/restrict_anonymous_structs.rs:37:7
3345
|
34-
LL | K(struct { field: u8 }),
46+
LL | L(struct { field: u8 }),
3547
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
3648

37-
error: anonymous fields are not allowed outside of structs or unions
38-
--> $DIR/restrict_anonymous_structs.rs:28:9
49+
error: unnamed fields are not allowed outside of structs or unions
50+
--> $DIR/restrict_anonymous_structs.rs:40:9
3951
|
40-
LL | _ : struct { field: u8 }
52+
LL | _ : struct { field: u8 },
4153
| -^^^^^^^^^^^^^^^^^^^^^^^
4254
| |
43-
| anonymous field declared here
55+
| unnamed field declared here
4456

4557
error: anonymous structs are not allowed outside of unnamed struct or union fields
46-
--> $DIR/restrict_anonymous_structs.rs:28:13
58+
--> $DIR/restrict_anonymous_structs.rs:40:13
4759
|
48-
LL | _ : struct { field: u8 }
60+
LL | _ : struct { field: u8 },
4961
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
5062

51-
error: anonymous fields are not allowed outside of structs or unions
52-
--> $DIR/restrict_anonymous_structs.rs:33:9
63+
error: unnamed fields are not allowed outside of structs or unions
64+
--> $DIR/restrict_anonymous_structs.rs:45:9
5365
|
54-
LL | _ : u8
66+
LL | _ : u8,
5567
| -^^^^^
5668
| |
57-
| anonymous field declared here
69+
| unnamed field declared here
5870

5971
error: anonymous structs are not allowed outside of unnamed struct or union fields
60-
--> $DIR/restrict_anonymous_structs.rs:37:10
72+
--> $DIR/restrict_anonymous_structs.rs:49:10
6173
|
6274
LL | const L: struct { field: u8 } = 0;
6375
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
6476

6577
error: anonymous structs are not allowed outside of unnamed struct or union fields
66-
--> $DIR/restrict_anonymous_structs.rs:40:11
78+
--> $DIR/restrict_anonymous_structs.rs:52:11
6779
|
6880
LL | static M: struct { field: u8 } = 0;
6981
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
7082

7183
error: anonymous structs are not allowed outside of unnamed struct or union fields
72-
--> $DIR/restrict_anonymous_structs.rs:43:10
84+
--> $DIR/restrict_anonymous_structs.rs:55:10
7385
|
7486
LL | type N = struct { field: u8 };
7587
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
7688

7789
error: anonymous structs are not allowed outside of unnamed struct or union fields
78-
--> $DIR/restrict_anonymous_structs.rs:46:6
90+
--> $DIR/restrict_anonymous_structs.rs:58:6
7991
|
8092
LL | impl struct { field: u8 } {}
8193
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
8294

8395
error: anonymous structs are not allowed outside of unnamed struct or union fields
84-
--> $DIR/restrict_anonymous_structs.rs:51:14
96+
--> $DIR/restrict_anonymous_structs.rs:63:14
8597
|
8698
LL | impl Foo for struct { field: u8 } {}
8799
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
88100

89101
error: anonymous structs are not allowed outside of unnamed struct or union fields
90-
--> $DIR/restrict_anonymous_structs.rs:55:13
102+
--> $DIR/restrict_anonymous_structs.rs:67:13
91103
|
92104
LL | let p: [struct { field: u8 }; 1];
93105
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
94106

95107
error: anonymous structs are not allowed outside of unnamed struct or union fields
96-
--> $DIR/restrict_anonymous_structs.rs:58:13
108+
--> $DIR/restrict_anonymous_structs.rs:70:13
97109
|
98110
LL | let q: (struct { field: u8 }, u8);
99111
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
100112

101113
error: anonymous structs are not allowed outside of unnamed struct or union fields
102-
--> $DIR/restrict_anonymous_structs.rs:61:19
114+
--> $DIR/restrict_anonymous_structs.rs:73:19
103115
|
104116
LL | let c = || -> struct { field: u8 } {};
105117
| ^^^^^^^^^^^^^^^^^^^^ anonymous struct declared here
@@ -119,80 +131,92 @@ LL | fn f2(a: struct { field: u8 } ) {}
119131
error: anonymous structs are unimplemented
120132
--> $DIR/restrict_anonymous_structs.rs:12:12
121133
|
122-
LL | field: struct { field: u8 }
134+
LL | field: struct { field: u8 },
123135
| ^^^^^^^^^^^^^^^^^^^^
124136

125137
error: anonymous structs are unimplemented
126-
--> $DIR/restrict_anonymous_structs.rs:17:13
138+
--> $DIR/restrict_anonymous_structs.rs:14:8
127139
|
128-
LL | field1: struct { field: u8 }
129-
| ^^^^^^^^^^^^^^^^^^^^
140+
LL | _: struct { field: u8 },
141+
| ^^^^^^^^^^^^^^^^^^^^
142+
143+
error: anonymous structs are unimplemented
144+
--> $DIR/restrict_anonymous_structs.rs:23:12
145+
|
146+
LL | field: struct { field: u8 },
147+
| ^^^^^^^^^^^^^^^^^^^^
148+
149+
error: anonymous structs are unimplemented
150+
--> $DIR/restrict_anonymous_structs.rs:25:8
151+
|
152+
LL | _: struct { field: u8 },
153+
| ^^^^^^^^^^^^^^^^^^^^
130154

131155
error: anonymous structs are unimplemented
132-
--> $DIR/restrict_anonymous_structs.rs:21:10
156+
--> $DIR/restrict_anonymous_structs.rs:33:10
133157
|
134-
LL | struct I(struct { field: u8 }, u8);
158+
LL | struct J(struct { field: u8 }, u8);
135159
| ^^^^^^^^^^^^^^^^^^^^
136160

137161
error: anonymous structs are unimplemented
138-
--> $DIR/restrict_anonymous_structs.rs:25:7
162+
--> $DIR/restrict_anonymous_structs.rs:37:7
139163
|
140-
LL | K(struct { field: u8 }),
164+
LL | L(struct { field: u8 }),
141165
| ^^^^^^^^^^^^^^^^^^^^
142166

143167
error: anonymous structs are unimplemented
144-
--> $DIR/restrict_anonymous_structs.rs:28:13
168+
--> $DIR/restrict_anonymous_structs.rs:40:13
145169
|
146-
LL | _ : struct { field: u8 }
170+
LL | _ : struct { field: u8 },
147171
| ^^^^^^^^^^^^^^^^^^^^
148172

149173
error: anonymous structs are unimplemented
150-
--> $DIR/restrict_anonymous_structs.rs:37:10
174+
--> $DIR/restrict_anonymous_structs.rs:49:10
151175
|
152176
LL | const L: struct { field: u8 } = 0;
153177
| ^^^^^^^^^^^^^^^^^^^^
154178

155179
error: anonymous structs are unimplemented
156-
--> $DIR/restrict_anonymous_structs.rs:40:11
180+
--> $DIR/restrict_anonymous_structs.rs:52:11
157181
|
158182
LL | static M: struct { field: u8 } = 0;
159183
| ^^^^^^^^^^^^^^^^^^^^
160184

161185
error: anonymous structs are unimplemented
162-
--> $DIR/restrict_anonymous_structs.rs:43:10
186+
--> $DIR/restrict_anonymous_structs.rs:55:10
163187
|
164188
LL | type N = struct { field: u8 };
165189
| ^^^^^^^^^^^^^^^^^^^^
166190

167191
error: anonymous structs are unimplemented
168-
--> $DIR/restrict_anonymous_structs.rs:46:6
192+
--> $DIR/restrict_anonymous_structs.rs:58:6
169193
|
170194
LL | impl struct { field: u8 } {}
171195
| ^^^^^^^^^^^^^^^^^^^^
172196

173197
error: anonymous structs are unimplemented
174-
--> $DIR/restrict_anonymous_structs.rs:51:14
198+
--> $DIR/restrict_anonymous_structs.rs:63:14
175199
|
176200
LL | impl Foo for struct { field: u8 } {}
177201
| ^^^^^^^^^^^^^^^^^^^^
178202

179203
error: anonymous structs are unimplemented
180-
--> $DIR/restrict_anonymous_structs.rs:55:13
204+
--> $DIR/restrict_anonymous_structs.rs:67:13
181205
|
182206
LL | let p: [struct { field: u8 }; 1];
183207
| ^^^^^^^^^^^^^^^^^^^^
184208

185209
error: anonymous structs are unimplemented
186-
--> $DIR/restrict_anonymous_structs.rs:58:13
210+
--> $DIR/restrict_anonymous_structs.rs:70:13
187211
|
188212
LL | let q: (struct { field: u8 }, u8);
189213
| ^^^^^^^^^^^^^^^^^^^^
190214

191215
error: anonymous structs are unimplemented
192-
--> $DIR/restrict_anonymous_structs.rs:61:19
216+
--> $DIR/restrict_anonymous_structs.rs:73:19
193217
|
194218
LL | let c = || -> struct { field: u8 } {};
195219
| ^^^^^^^^^^^^^^^^^^^^
196220

197-
error: aborting due to 32 previous errors
221+
error: aborting due to 36 previous errors
198222

0 commit comments

Comments
 (0)