Rollup merge of #108066 - compiler-errors:better-labels-for-bad-impl-trait, r=petrochenkov

Better names for illegal impl trait positions

Just some wording tweaks, no behavior changes.
This commit is contained in:
Matthias Krüger 2023-02-15 21:30:58 +01:00 committed by GitHub
commit 8259755069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 67 additions and 42 deletions

View File

@ -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) => {

View File

@ -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)
});
@ -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))
}
@ -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,
@ -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);
@ -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())
}
@ -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(
@ -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())),
@ -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,
@ -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),
})
}

View File

@ -253,7 +253,6 @@ enum ImplTraitContext {
enum ImplTraitPosition {
Path,
Variable,
Type,
Trait,
AsyncBlock,
Bound,
@ -270,6 +269,13 @@ enum ImplTraitPosition {
FnTraitReturn,
TraitReturn,
ImplReturn,
GenericDefault,
ConstTy,
StaticTy,
AssocTy,
FieldTy,
Cast,
ImplSelf,
}
impl std::fmt::Display for ImplTraitPosition {
@ -277,7 +283,6 @@ impl std::fmt::Display for ImplTraitPosition {
let name = match self {
ImplTraitPosition::Path => "path",
ImplTraitPosition::Variable => "variable binding",
ImplTraitPosition::Type => "type",
ImplTraitPosition::Trait => "trait",
ImplTraitPosition::AsyncBlock => "async block",
ImplTraitPosition::Bound => "bound",
@ -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}")
@ -2166,7 +2178,10 @@ 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,
};
@ -2174,7 +2189,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
(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)),

View File

@ -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 {

View File

@ -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;

View File

@ -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 = {

View File

@ -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>;

View File

@ -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| {

View File

@ -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),
@ -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
@ -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) {}