Skip to content

Commit 3f80017

Browse files
Better label for illegal impl trait types
1 parent 9bb6e60 commit 3f80017

File tree

9 files changed

+67
-42
lines changed

9 files changed

+67
-42
lines changed

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
139139
ExprKind::Cast(expr, ty) => {
140140
let expr = self.lower_expr(expr);
141141
let ty =
142-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
142+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
143143
hir::ExprKind::Cast(expr, ty)
144144
}
145145
ExprKind::Type(expr, ty) => {
146146
let expr = self.lower_expr(expr);
147147
let ty =
148-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
148+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
149149
hir::ExprKind::Type(expr, ty)
150150
}
151151
ExprKind::AddrOf(k, m, ohs) => {

Diff for: compiler/rustc_ast_lowering/src/item.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
378378
)
379379
});
380380

381-
let lowered_ty = this
382-
.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
381+
let lowered_ty = this.lower_ty(
382+
ty,
383+
&ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
384+
);
383385

384386
(trait_ref, lowered_ty)
385387
});
@@ -458,7 +460,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
458460
span: Span,
459461
body: Option<&Expr>,
460462
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
461-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
463+
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
462464
(ty, self.lower_const_body(span, body))
463465
}
464466

@@ -608,8 +610,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
608610
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
609611
}
610612
ForeignItemKind::Static(t, m, _) => {
611-
let ty =
612-
self.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
613+
let ty = self
614+
.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
613615
hir::ForeignItemKind::Static(ty, *m)
614616
}
615617
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
@@ -679,11 +681,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
679681
qself,
680682
path,
681683
ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
682-
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
684+
&ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy),
683685
);
684686
self.arena.alloc(t)
685687
} else {
686-
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
688+
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy))
687689
};
688690
let hir_id = self.lower_node_id(f.id);
689691
self.lower_attrs(hir_id, &f.attrs);
@@ -708,7 +710,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
708710

709711
let (generics, kind, has_default) = match &i.kind {
710712
AssocItemKind::Const(_, ty, default) => {
711-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
713+
let ty =
714+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
712715
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
713716
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
714717
}
@@ -746,7 +749,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
746749
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
747750
|this| {
748751
let ty = ty.as_ref().map(|x| {
749-
this.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
752+
this.lower_ty(
753+
x,
754+
&ImplTraitContext::Disallowed(ImplTraitPosition::AssocTy),
755+
)
750756
});
751757
hir::TraitItemKind::Type(
752758
this.lower_param_bounds(
@@ -805,7 +811,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
805811

806812
let (generics, kind) = match &i.kind {
807813
AssocItemKind::Const(_, ty, expr) => {
808-
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
814+
let ty =
815+
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
809816
(
810817
hir::Generics::empty(),
811818
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
@@ -1441,7 +1448,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14411448
hir_id: self.next_id(),
14421449
bound_generic_params: self.lower_generic_params(bound_generic_params),
14431450
bounded_ty: self
1444-
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
1451+
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14451452
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
14461453
self.lower_param_bound(
14471454
bound,
@@ -1465,9 +1472,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14651472
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
14661473
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
14671474
lhs_ty: self
1468-
.lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
1475+
.lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14691476
rhs_ty: self
1470-
.lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
1477+
.lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
14711478
span: self.lower_span(*span),
14721479
})
14731480
}

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ enum ImplTraitContext {
253253
enum ImplTraitPosition {
254254
Path,
255255
Variable,
256-
Type,
257256
Trait,
258257
AsyncBlock,
259258
Bound,
@@ -270,14 +269,20 @@ enum ImplTraitPosition {
270269
FnTraitReturn,
271270
TraitReturn,
272271
ImplReturn,
272+
GenericDefault,
273+
ConstTy,
274+
StaticTy,
275+
AssocTy,
276+
FieldTy,
277+
Cast,
278+
ImplSelf,
273279
}
274280

275281
impl std::fmt::Display for ImplTraitPosition {
276282
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277283
let name = match self {
278284
ImplTraitPosition::Path => "path",
279285
ImplTraitPosition::Variable => "variable binding",
280-
ImplTraitPosition::Type => "type",
281286
ImplTraitPosition::Trait => "trait",
282287
ImplTraitPosition::AsyncBlock => "async block",
283288
ImplTraitPosition::Bound => "bound",
@@ -294,6 +299,13 @@ impl std::fmt::Display for ImplTraitPosition {
294299
ImplTraitPosition::FnTraitReturn => "`Fn` trait return",
295300
ImplTraitPosition::TraitReturn => "trait method return",
296301
ImplTraitPosition::ImplReturn => "`impl` method return",
302+
ImplTraitPosition::GenericDefault => "generic parameter default",
303+
ImplTraitPosition::ConstTy => "const type",
304+
ImplTraitPosition::StaticTy => "static type",
305+
ImplTraitPosition::AssocTy => "associated type",
306+
ImplTraitPosition::FieldTy => "field type",
307+
ImplTraitPosition::Cast => "cast type",
308+
ImplTraitPosition::ImplSelf => "impl header",
297309
};
298310

299311
write!(f, "{name}")
@@ -2166,15 +2178,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21662178
GenericParamKind::Type { default, .. } => {
21672179
let kind = hir::GenericParamKind::Type {
21682180
default: default.as_ref().map(|x| {
2169-
self.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
2181+
self.lower_ty(
2182+
x,
2183+
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
2184+
)
21702185
}),
21712186
synthetic: false,
21722187
};
21732188

21742189
(hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
21752190
}
21762191
GenericParamKind::Const { ty, kw_span: _, default } => {
2177-
let ty = self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
2192+
let ty = self.lower_ty(
2193+
&ty,
2194+
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
2195+
);
21782196
let default = default.as_ref().map(|def| self.lower_anon_const(def));
21792197
(
21802198
hir::ParamName::Plain(self.lower_ident(param.ident)),

Diff for: tests/ui/associated-consts/issue-105330.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ LL | fn main<A: TraitWAssocConst<A=32>>() {
3333
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
3434
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
3535

36-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
36+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in impl header
3737
--> $DIR/issue-105330.rs:6:27
3838
|
3939
LL | impl TraitWAssocConst for impl Demo {

Diff for: tests/ui/feature-gates/feature-gate-associated_type_bounds.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ LL | let _: impl Tr1<As1: Copy> = S1;
115115
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
116116
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable
117117

118-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
118+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
119119
--> $DIR/feature-gate-associated_type_bounds.rs:58:14
120120
|
121121
LL | const _cdef: impl Tr1<As1: Copy> = S1;
122122
| ^^^^^^^^^^^^^^^^^^^
123123

124-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
124+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
125125
--> $DIR/feature-gate-associated_type_bounds.rs:64:15
126126
|
127127
LL | static _sdef: impl Tr1<As1: Copy> = S1;

Diff for: tests/ui/impl-trait/issues/issue-58956.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
1+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
22
--> $DIR/issue-58956.rs:7:11
33
|
44
LL | const _A: impl Lam = {

Diff for: tests/ui/impl-trait/issues/issue-83929-impl-trait-in-generic-default.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
1+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
22
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16
33
|
44
LL | struct Foo<T = impl Copy>(T);
55
| ^^^^^^^^^
66

7-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
7+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
88
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:4:20
99
|
1010
LL | type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;

Diff for: tests/ui/impl-trait/issues/issue-86642.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
1+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
22
--> $DIR/issue-86642.rs:1:11
33
|
44
LL | static x: impl Fn(&str) -> Result<&str, ()> = move |source| {

Diff for: tests/ui/impl-trait/where-allowed.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,31 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
115115
LL | fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() }
116116
| ^^^^^^^^^^
117117

118-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
118+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in field type
119119
--> $DIR/where-allowed.rs:81:32
120120
|
121121
LL | struct InBraceStructField { x: impl Debug }
122122
| ^^^^^^^^^^
123123

124-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in path
124+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in field type
125125
--> $DIR/where-allowed.rs:85:41
126126
|
127127
LL | struct InAdtInBraceStructField { x: Vec<impl Debug> }
128128
| ^^^^^^^^^^
129129

130-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
130+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in field type
131131
--> $DIR/where-allowed.rs:89:27
132132
|
133133
LL | struct InTupleStructField(impl Debug);
134134
| ^^^^^^^^^^
135135

136-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
136+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in field type
137137
--> $DIR/where-allowed.rs:94:25
138138
|
139139
LL | InBraceVariant { x: impl Debug },
140140
| ^^^^^^^^^^
141141

142-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
142+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in field type
143143
--> $DIR/where-allowed.rs:96:20
144144
|
145145
LL | InTupleVariant(impl Debug),
@@ -187,31 +187,31 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
187187
LL | impl PartialEq<impl Debug> for () {
188188
| ^^^^^^^^^^
189189

190-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
190+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in impl header
191191
--> $DIR/where-allowed.rs:166:24
192192
|
193193
LL | impl PartialEq<()> for impl Debug {
194194
| ^^^^^^^^^^
195195

196-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
196+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in impl header
197197
--> $DIR/where-allowed.rs:171:6
198198
|
199199
LL | impl impl Debug {
200200
| ^^^^^^^^^^
201201

202-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
202+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in impl header
203203
--> $DIR/where-allowed.rs:177:24
204204
|
205205
LL | impl InInherentImplAdt<impl Debug> {
206206
| ^^^^^^^^^^
207207

208-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
208+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in bound
209209
--> $DIR/where-allowed.rs:183:11
210210
|
211211
LL | where impl Debug: Debug
212212
| ^^^^^^^^^^
213213

214-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
214+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in bound
215215
--> $DIR/where-allowed.rs:190:15
216216
|
217217
LL | where Vec<impl Debug>: Debug
@@ -235,37 +235,37 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
235235
LL | where T: Fn() -> impl Debug
236236
| ^^^^^^^^^^
237237

238-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
238+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
239239
--> $DIR/where-allowed.rs:217:40
240240
|
241241
LL | struct InStructGenericParamDefault<T = impl Debug>(T);
242242
| ^^^^^^^^^^
243243

244-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
244+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
245245
--> $DIR/where-allowed.rs:221:36
246246
|
247247
LL | enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) }
248248
| ^^^^^^^^^^
249249

250-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
250+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
251251
--> $DIR/where-allowed.rs:225:38
252252
|
253253
LL | trait InTraitGenericParamDefault<T = impl Debug> {}
254254
| ^^^^^^^^^^
255255

256-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
256+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
257257
--> $DIR/where-allowed.rs:229:41
258258
|
259259
LL | type InTypeAliasGenericParamDefault<T = impl Debug> = T;
260260
| ^^^^^^^^^^
261261

262-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
262+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
263263
--> $DIR/where-allowed.rs:233:11
264264
|
265265
LL | impl <T = impl Debug> T {}
266266
| ^^^^^^^^^^
267267

268-
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
268+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
269269
--> $DIR/where-allowed.rs:240:40
270270
|
271271
LL | fn in_method_generic_param_default<T = impl Debug>(_: T) {}

0 commit comments

Comments
 (0)