Skip to content

Commit

Permalink
Rollup merge of #108066 - compiler-errors:better-labels-for-bad-impl-…
Browse files Browse the repository at this point in the history
…trait, r=petrochenkov

Better names for illegal impl trait positions

Just some wording tweaks, no behavior changes.
  • Loading branch information
matthiaskrgr committed Feb 15, 2023
2 parents 897f56e + 3f80017 commit 8259755
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 42 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::Cast(expr, ty) => {
let expr = self.lower_expr(expr);
let ty =
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
hir::ExprKind::Cast(expr, ty)
}
ExprKind::Type(expr, ty) => {
let expr = self.lower_expr(expr);
let ty =
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
hir::ExprKind::Type(expr, ty)
}
ExprKind::AddrOf(k, m, ohs) => {
Expand Down
33 changes: 20 additions & 13 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
});

let lowered_ty = this
.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let lowered_ty = this.lower_ty(
ty,
&ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
);

(trait_ref, lowered_ty)
});
Expand Down Expand Up @@ -458,7 +460,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: Span,
body: Option<&Expr>,
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
(ty, self.lower_const_body(span, body))
}

Expand Down Expand Up @@ -608,8 +610,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
}
ForeignItemKind::Static(t, m, _) => {
let ty =
self.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty = self
.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
hir::ForeignItemKind::Static(ty, *m)
}
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
Expand Down Expand Up @@ -679,11 +681,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself,
path,
ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
&ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy),
);
self.arena.alloc(t)
} else {
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy))
};
let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs);
Expand All @@ -708,7 +710,8 @@ impl<'hir> LoweringContext<'_, 'hir> {

let (generics, kind, has_default) = match &i.kind {
AssocItemKind::Const(_, ty, default) => {
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty =
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
}
Expand Down Expand Up @@ -746,7 +749,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| {
let ty = ty.as_ref().map(|x| {
this.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
this.lower_ty(
x,
&ImplTraitContext::Disallowed(ImplTraitPosition::AssocTy),
)
});
hir::TraitItemKind::Type(
this.lower_param_bounds(
Expand Down Expand Up @@ -805,7 +811,8 @@ impl<'hir> LoweringContext<'_, 'hir> {

let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => {
let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty =
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
(
hir::Generics::empty(),
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
Expand Down Expand Up @@ -1441,7 +1448,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir_id: self.next_id(),
bound_generic_params: self.lower_generic_params(bound_generic_params),
bounded_ty: self
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
.lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
self.lower_param_bound(
bound,
Expand All @@ -1465,9 +1472,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
WherePredicate::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty, span }) => {
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
lhs_ty: self
.lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
.lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
rhs_ty: self
.lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
.lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
span: self.lower_span(*span),
})
}
Expand Down
26 changes: 22 additions & 4 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ enum ImplTraitContext {
enum ImplTraitPosition {
Path,
Variable,
Type,
Trait,
AsyncBlock,
Bound,
Expand All @@ -270,14 +269,20 @@ enum ImplTraitPosition {
FnTraitReturn,
TraitReturn,
ImplReturn,
GenericDefault,
ConstTy,
StaticTy,
AssocTy,
FieldTy,
Cast,
ImplSelf,
}

impl std::fmt::Display for ImplTraitPosition {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
ImplTraitPosition::Path => "path",
ImplTraitPosition::Variable => "variable binding",
ImplTraitPosition::Type => "type",
ImplTraitPosition::Trait => "trait",
ImplTraitPosition::AsyncBlock => "async block",
ImplTraitPosition::Bound => "bound",
Expand All @@ -294,6 +299,13 @@ impl std::fmt::Display for ImplTraitPosition {
ImplTraitPosition::FnTraitReturn => "`Fn` trait return",
ImplTraitPosition::TraitReturn => "trait method return",
ImplTraitPosition::ImplReturn => "`impl` method return",
ImplTraitPosition::GenericDefault => "generic parameter default",
ImplTraitPosition::ConstTy => "const type",
ImplTraitPosition::StaticTy => "static type",
ImplTraitPosition::AssocTy => "associated type",
ImplTraitPosition::FieldTy => "field type",
ImplTraitPosition::Cast => "cast type",
ImplTraitPosition::ImplSelf => "impl header",
};

write!(f, "{name}")
Expand Down Expand Up @@ -2166,15 +2178,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericParamKind::Type { default, .. } => {
let kind = hir::GenericParamKind::Type {
default: default.as_ref().map(|x| {
self.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
self.lower_ty(
x,
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
)
}),
synthetic: false,
};

(hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
}
GenericParamKind::Const { ty, kw_span: _, default } => {
let ty = self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty = self.lower_ty(
&ty,
&ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
);
let default = default.as_ref().map(|def| self.lower_anon_const(def));
(
hir::ParamName::Plain(self.lower_ident(param.ident)),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-consts/issue-105330.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LL | fn main<A: TraitWAssocConst<A=32>>() {
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in impl header
--> $DIR/issue-105330.rs:6:27
|
LL | impl TraitWAssocConst for impl Demo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ LL | let _: impl Tr1<As1: Copy> = S1;
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information
= help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable

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

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
--> $DIR/feature-gate-associated_type_bounds.rs:64:15
|
LL | static _sdef: impl Tr1<As1: Copy> = S1;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issues/issue-58956.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
--> $DIR/issue-58956.rs:7:11
|
LL | const _A: impl Lam = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16
|
LL | struct Foo<T = impl Copy>(T);
| ^^^^^^^^^

error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic parameter default
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:4:20
|
LL | type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issues/issue-86642.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in const type
--> $DIR/issue-86642.rs:1:11
|
LL | static x: impl Fn(&str) -> Result<&str, ()> = move |source| {
Expand Down
32 changes: 16 additions & 16 deletions tests/ui/impl-trait/where-allowed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,31 @@ error[E0562]: `impl Trait` only allowed in function and inherent method return t
LL | fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() }
| ^^^^^^^^^^

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 8259755

Please sign in to comment.