Rollup merge of #92806 - compiler-errors:better-impl-trait-deny, r=estebank

Add more information to `impl Trait` error

Fixes #92458

Let me know if I went overboard here, or if the suggestions could use some refinement.

r? `@estebank`
Feel free to reassign to someone else
This commit is contained in:
Matthias Krüger 2022-02-18 23:23:04 +01:00 committed by GitHub
commit 5c08c39121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 366 additions and 216 deletions

View File

@ -97,7 +97,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let ty = l let ty = l
.ty .ty
.as_ref() .as_ref()
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding))); .map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
let init = l.kind.init().map(|init| self.lower_expr(init)); let init = l.kind.init().map(|init| self.lower_expr(init));
let hir_id = self.lower_node_id(l.id); let hir_id = self.lower_node_id(l.id);
let pat = self.lower_pat(&l.pat); let pat = self.lower_pat(&l.pat);
@ -127,7 +127,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let ty = local let ty = local
.ty .ty
.as_ref() .as_ref()
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Binding))); .map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
let span = self.lower_span(local.span); let span = self.lower_span(local.span);
let span = self.mark_span_with_reason(DesugaringKind::LetElse, span, None); let span = self.mark_span_with_reason(DesugaringKind::LetElse, span, None);
let init = self.lower_expr(init); let init = self.lower_expr(init);

View File

@ -1,3 +1,5 @@
use crate::{FnDeclKind, ImplTraitPosition};
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs}; use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
use rustc_ast::attr; use rustc_ast::attr;
@ -53,7 +55,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ParamMode::Optional, ParamMode::Optional,
0, 0,
ParenthesizedGenericArgs::Err, ParenthesizedGenericArgs::Err,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
)); ));
let args = self.lower_exprs(args); let args = self.lower_exprs(args);
hir::ExprKind::MethodCall(hir_seg, args, self.lower_span(span)) hir::ExprKind::MethodCall(hir_seg, args, self.lower_span(span))
@ -74,12 +76,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
} }
ExprKind::Cast(ref expr, ref ty) => { ExprKind::Cast(ref expr, ref ty) => {
let expr = self.lower_expr(expr); let expr = self.lower_expr(expr);
let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); let ty =
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ExprKind::Cast(expr, ty) hir::ExprKind::Cast(expr, ty)
} }
ExprKind::Type(ref expr, ref ty) => { ExprKind::Type(ref expr, ref ty) => {
let expr = self.lower_expr(expr); let expr = self.lower_expr(expr);
let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); let ty =
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ExprKind::Type(expr, ty) hir::ExprKind::Type(expr, ty)
} }
ExprKind::AddrOf(k, m, ref ohs) => { ExprKind::AddrOf(k, m, ref ohs) => {
@ -203,7 +207,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
hir::ExprKind::Path(qpath) hir::ExprKind::Path(qpath)
} }
@ -239,7 +243,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&se.qself, &se.qself,
&se.path, &se.path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
)), )),
self.arena self.arena
.alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))), .alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
@ -538,7 +542,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
) -> hir::ExprKind<'hir> { ) -> hir::ExprKind<'hir> {
let output = match ret_ty { let output = match ret_ty {
Some(ty) => hir::FnRetTy::Return(self.lower_ty(&ty, ImplTraitContext::disallowed())), Some(ty) => hir::FnRetTy::Return(
self.lower_ty(&ty, ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)),
),
None => hir::FnRetTy::DefaultReturn(self.lower_span(span)), None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
}; };
@ -827,7 +833,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}); });
// Lower outside new scope to preserve `is_in_loop_condition`. // Lower outside new scope to preserve `is_in_loop_condition`.
let fn_decl = self.lower_fn_decl(decl, None, false, None); let fn_decl = self.lower_fn_decl(decl, None, FnDeclKind::Closure, None);
hir::ExprKind::Closure( hir::ExprKind::Closure(
capture_clause, capture_clause,
@ -919,7 +925,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// We need to lower the declaration outside the new scope, because we // We need to lower the declaration outside the new scope, because we
// have to conserve the state of being inside a loop condition for the // have to conserve the state of being inside a loop condition for the
// closure argument types. // closure argument types.
let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None); let fn_decl = self.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None);
hir::ExprKind::Closure( hir::ExprKind::Closure(
capture_clause, capture_clause,
@ -1064,7 +1070,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
// Destructure like a tuple struct. // Destructure like a tuple struct.
let tuple_struct_pat = let tuple_struct_pat =
@ -1089,7 +1095,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&se.qself, &se.qself,
&se.path, &se.path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
let fields_omitted = match &se.rest { let fields_omitted = match &se.rest {
StructRest::Base(e) => { StructRest::Base(e) => {

View File

@ -1,6 +1,6 @@
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode}; use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
use super::{ImplTraitContext, ImplTraitPosition}; use super::{ImplTraitContext, ImplTraitPosition};
use crate::Arena; use crate::{Arena, FnDeclKind};
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
@ -246,7 +246,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
AnonymousLifetimeMode::PassThrough, AnonymousLifetimeMode::PassThrough,
|this, idty| { |this, idty| {
let ret_id = asyncness.opt_return_id(); let ret_id = asyncness.opt_return_id();
this.lower_fn_decl(&decl, Some((fn_def_id, idty)), true, ret_id) this.lower_fn_decl(
&decl,
Some((fn_def_id, idty)),
FnDeclKind::Fn,
ret_id,
)
}, },
); );
let sig = hir::FnSig { let sig = hir::FnSig {
@ -287,12 +292,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
capturable_lifetimes: &mut FxHashSet::default(), capturable_lifetimes: &mut FxHashSet::default(),
}, },
); );
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let generics = self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
);
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
} }
ItemKind::TyAlias(box TyAlias { ref generics, ty: None, .. }) => { ItemKind::TyAlias(box TyAlias { ref generics, ty: None, .. }) => {
let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err)); let ty = self.arena.alloc(self.ty(span, hir::TyKind::Err));
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let generics = self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
);
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
} }
ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemKind::Enum( ItemKind::Enum(ref enum_definition, ref generics) => hir::ItemKind::Enum(
@ -301,20 +312,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
enum_definition.variants.iter().map(|x| self.lower_variant(x)), enum_definition.variants.iter().map(|x| self.lower_variant(x)),
), ),
}, },
self.lower_generics(generics, ImplTraitContext::disallowed()), self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
), ),
ItemKind::Struct(ref struct_def, ref generics) => { ItemKind::Struct(ref struct_def, ref generics) => {
let struct_def = self.lower_variant_data(hir_id, struct_def); let struct_def = self.lower_variant_data(hir_id, struct_def);
hir::ItemKind::Struct( hir::ItemKind::Struct(
struct_def, struct_def,
self.lower_generics(generics, ImplTraitContext::disallowed()), self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
) )
} }
ItemKind::Union(ref vdata, ref generics) => { ItemKind::Union(ref vdata, ref generics) => {
let vdata = self.lower_variant_data(hir_id, vdata); let vdata = self.lower_variant_data(hir_id, vdata);
hir::ItemKind::Union( hir::ItemKind::Union(
vdata, vdata,
self.lower_generics(generics, ImplTraitContext::disallowed()), self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
) )
} }
ItemKind::Impl(box Impl { ItemKind::Impl(box Impl {
@ -347,10 +367,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
AnonymousLifetimeMode::CreateParameter, AnonymousLifetimeMode::CreateParameter,
|this, _| { |this, _| {
let trait_ref = trait_ref.as_ref().map(|trait_ref| { let trait_ref = trait_ref.as_ref().map(|trait_ref| {
this.lower_trait_ref(trait_ref, ImplTraitContext::disallowed()) this.lower_trait_ref(
trait_ref,
ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
)
}); });
let lowered_ty = this.lower_ty(ty, ImplTraitContext::disallowed()); let lowered_ty = this
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
(trait_ref, lowered_ty) (trait_ref, lowered_ty)
}, },
@ -390,21 +414,33 @@ impl<'hir> LoweringContext<'_, 'hir> {
ref bounds, ref bounds,
ref items, ref items,
}) => { }) => {
let bounds = self.lower_param_bounds(bounds, ImplTraitContext::disallowed()); let bounds = self.lower_param_bounds(
bounds,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
);
let items = self let items = self
.arena .arena
.alloc_from_iter(items.iter().map(|item| self.lower_trait_item_ref(item))); .alloc_from_iter(items.iter().map(|item| self.lower_trait_item_ref(item)));
hir::ItemKind::Trait( hir::ItemKind::Trait(
is_auto, is_auto,
self.lower_unsafety(unsafety), self.lower_unsafety(unsafety),
self.lower_generics(generics, ImplTraitContext::disallowed()), self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
bounds, bounds,
items, items,
) )
} }
ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemKind::TraitAlias( ItemKind::TraitAlias(ref generics, ref bounds) => hir::ItemKind::TraitAlias(
self.lower_generics(generics, ImplTraitContext::disallowed()), self.lower_generics(
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
self.lower_param_bounds(
bounds,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
), ),
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => { ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
let body = P(self.lower_mac_args(body)); let body = P(self.lower_mac_args(body));
@ -423,7 +459,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: Span, span: Span,
body: Option<&Expr>, body: Option<&Expr>,
) -> (&'hir hir::Ty<'hir>, hir::BodyId) { ) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Binding)); let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
(ty, self.lower_const_body(span, body)) (ty, self.lower_const_body(span, body))
} }
@ -667,7 +703,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|this, _| { |this, _| {
( (
// Disallow `impl Trait` in foreign items. // Disallow `impl Trait` in foreign items.
this.lower_fn_decl(fdec, None, false, None), this.lower_fn_decl(fdec, None, FnDeclKind::ExternFn, None),
this.lower_fn_params_to_names(fdec), this.lower_fn_params_to_names(fdec),
) )
}, },
@ -676,7 +712,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics) hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
} }
ForeignItemKind::Static(ref t, m, _) => { ForeignItemKind::Static(ref t, m, _) => {
let ty = self.lower_ty(t, ImplTraitContext::disallowed()); let ty =
self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ForeignItemKind::Static(ty, m) hir::ForeignItemKind::Static(ty, m)
} }
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type, ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
@ -744,11 +781,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself, qself,
path, path,
ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124) ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
self.arena.alloc(t) self.arena.alloc(t)
} else { } else {
self.lower_ty(&f.ty, ImplTraitContext::disallowed()) self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type))
}; };
let hir_id = self.lower_node_id(f.id); let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs); self.lower_attrs(hir_id, &f.attrs);
@ -771,14 +808,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (generics, kind) = match i.kind { let (generics, kind) = match i.kind {
AssocItemKind::Const(_, ref ty, ref default) => { AssocItemKind::Const(_, ref ty, ref default) => {
let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))); let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body)) (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
} }
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => { AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
let names = self.lower_fn_params_to_names(&sig.decl); let names = self.lower_fn_params_to_names(&sig.decl);
let (generics, sig) = let (generics, sig) = self.lower_method_sig(
self.lower_method_sig(generics, sig, trait_item_def_id, false, None); generics,
sig,
trait_item_def_id,
FnDeclKind::Trait,
None,
);
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names))) (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
} }
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => { AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
@ -789,16 +831,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
generics, generics,
sig, sig,
trait_item_def_id, trait_item_def_id,
false, FnDeclKind::Trait,
asyncness.opt_return_id(), asyncness.opt_return_id(),
); );
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id))) (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
} }
AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => { AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
let ty = ty.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed())); let ty = ty.as_ref().map(|x| {
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); self.lower_ty(x, ImplTraitContext::Disallowed(ImplTraitPosition::Type))
});
let generics = self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
);
let kind = hir::TraitItemKind::Type( let kind = hir::TraitItemKind::Type(
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), self.lower_param_bounds(
bounds,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
ty, ty,
); );
@ -850,7 +900,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (generics, kind) = match &i.kind { let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => { AssocItemKind::Const(_, ty, expr) => {
let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
( (
hir::Generics::empty(), hir::Generics::empty(),
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())), hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
@ -861,19 +911,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
let asyncness = sig.header.asyncness; let asyncness = sig.header.asyncness;
let body_id = let body_id =
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref()); self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
let impl_trait_return_allow = !self.is_in_trait_impl;
let (generics, sig) = self.lower_method_sig( let (generics, sig) = self.lower_method_sig(
generics, generics,
sig, sig,
impl_item_def_id, impl_item_def_id,
impl_trait_return_allow, if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
asyncness.opt_return_id(), asyncness.opt_return_id(),
); );
(generics, hir::ImplItemKind::Fn(sig, body_id)) (generics, hir::ImplItemKind::Fn(sig, body_id))
} }
AssocItemKind::TyAlias(box TyAlias { generics, ty, .. }) => { AssocItemKind::TyAlias(box TyAlias { generics, ty, .. }) => {
let generics = self.lower_generics(generics, ImplTraitContext::disallowed()); let generics = self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
);
let kind = match ty { let kind = match ty {
None => { None => {
let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err)); let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err));
@ -1248,7 +1300,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
generics: &Generics, generics: &Generics,
sig: &FnSig, sig: &FnSig,
fn_def_id: LocalDefId, fn_def_id: LocalDefId,
impl_trait_return_allow: bool, kind: FnDeclKind,
is_async: Option<NodeId>, is_async: Option<NodeId>,
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) { ) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
let header = self.lower_fn_header(sig.header); let header = self.lower_fn_header(sig.header);
@ -1256,14 +1308,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
generics, generics,
fn_def_id, fn_def_id,
AnonymousLifetimeMode::PassThrough, AnonymousLifetimeMode::PassThrough,
|this, idty| { |this, idty| this.lower_fn_decl(&sig.decl, Some((fn_def_id, idty)), kind, is_async),
this.lower_fn_decl(
&sig.decl,
Some((fn_def_id, idty)),
impl_trait_return_allow,
is_async,
)
},
); );
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) }) (generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
} }
@ -1409,11 +1454,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
span, span,
}) => self.with_in_scope_lifetime_defs(&bound_generic_params, |this| { }) => self.with_in_scope_lifetime_defs(&bound_generic_params, |this| {
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: this bound_generic_params: this.lower_generic_params(
.lower_generic_params(bound_generic_params, ImplTraitContext::disallowed()), bound_generic_params,
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()), ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
bounded_ty: this.lower_ty(
bounded_ty,
ImplTraitContext::Disallowed(ImplTraitPosition::Type),
),
bounds: this.arena.alloc_from_iter(bounds.iter().map(|bound| { bounds: this.arena.alloc_from_iter(bounds.iter().map(|bound| {
this.lower_param_bound(bound, ImplTraitContext::disallowed()) this.lower_param_bound(
bound,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
)
})), })),
span: this.lower_span(span), span: this.lower_span(span),
}) })
@ -1425,13 +1478,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
}) => hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate { }) => hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
span: self.lower_span(span), span: self.lower_span(span),
lifetime: self.lower_lifetime(lifetime), lifetime: self.lower_lifetime(lifetime),
bounds: self.lower_param_bounds(bounds, ImplTraitContext::disallowed()), bounds: self.lower_param_bounds(
bounds,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
),
}), }),
WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => { WherePredicate::EqPredicate(WhereEqPredicate { id, ref lhs_ty, ref rhs_ty, span }) => {
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
hir_id: self.lower_node_id(id), hir_id: self.lower_node_id(id),
lhs_ty: self.lower_ty(lhs_ty, ImplTraitContext::disallowed()), lhs_ty: self
rhs_ty: self.lower_ty(rhs_ty, ImplTraitContext::disallowed()), .lower_ty(lhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
rhs_ty: self
.lower_ty(rhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
span: self.lower_span(span), span: self.lower_span(span),
}) })
} }

View File

@ -256,19 +256,28 @@ enum ImplTraitContext<'b, 'a> {
/// Position in which `impl Trait` is disallowed. /// Position in which `impl Trait` is disallowed.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ImplTraitPosition { enum ImplTraitPosition {
/// Disallowed in `let` / `const` / `static` bindings. Path,
Binding, Variable,
Type,
/// All other positions. Trait,
Other, AsyncBlock,
Bound,
Generic,
ExternFnParam,
ClosureParam,
PointerParam,
FnTraitParam,
TraitParam,
ImplParam,
ExternFnReturn,
ClosureReturn,
PointerReturn,
FnTraitReturn,
TraitReturn,
ImplReturn,
} }
impl<'a> ImplTraitContext<'_, 'a> { impl<'a> ImplTraitContext<'_, 'a> {
#[inline]
fn disallowed() -> Self {
ImplTraitContext::Disallowed(ImplTraitPosition::Other)
}
fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> { fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> {
use self::ImplTraitContext::*; use self::ImplTraitContext::*;
match self { match self {
@ -284,6 +293,54 @@ impl<'a> ImplTraitContext<'_, 'a> {
} }
} }
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",
ImplTraitPosition::Generic => "generic",
ImplTraitPosition::ExternFnParam => "`extern fn` param",
ImplTraitPosition::ClosureParam => "closure param",
ImplTraitPosition::PointerParam => "`fn` pointer param",
ImplTraitPosition::FnTraitParam => "`Fn` trait param",
ImplTraitPosition::TraitParam => "trait method param",
ImplTraitPosition::ImplParam => "`impl` method param",
ImplTraitPosition::ExternFnReturn => "`extern fn` return",
ImplTraitPosition::ClosureReturn => "closure return",
ImplTraitPosition::PointerReturn => "`fn` pointer return",
ImplTraitPosition::FnTraitReturn => "`Fn` trait return",
ImplTraitPosition::TraitReturn => "trait method return",
ImplTraitPosition::ImplReturn => "`impl` method return",
};
write!(f, "{}", name)
}
}
#[derive(Debug)]
enum FnDeclKind {
Fn,
Inherent,
ExternFn,
Closure,
Pointer,
Trait,
Impl,
}
impl FnDeclKind {
fn impl_trait_return_allowed(&self) -> bool {
match self {
FnDeclKind::Fn | FnDeclKind::Inherent => true,
_ => false,
}
}
}
pub fn lower_crate<'a, 'hir>( pub fn lower_crate<'a, 'hir>(
sess: &'a Session, sess: &'a Session,
krate: &'a Crate, krate: &'a Crate,
@ -1232,11 +1289,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy { hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy {
generic_params: this.lower_generic_params( generic_params: this.lower_generic_params(
&f.generic_params, &f.generic_params,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
), ),
unsafety: this.lower_unsafety(f.unsafety), unsafety: this.lower_unsafety(f.unsafety),
abi: this.lower_extern(f.ext), abi: this.lower_extern(f.ext),
decl: this.lower_fn_decl(&f.decl, None, false, None), decl: this.lower_fn_decl(&f.decl, None, FnDeclKind::Pointer, None),
param_names: this.lower_fn_params_to_names(&f.decl), param_names: this.lower_fn_params_to_names(&f.decl),
})) }))
}) })
@ -1357,13 +1414,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}), }),
)) ))
} }
ImplTraitContext::Disallowed(_) => { ImplTraitContext::Disallowed(position) => {
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.sess, self.sess,
t.span, t.span,
E0562, E0562,
"`impl Trait` not allowed outside of {}", "`impl Trait` only allowed in function and inherent method return types, not in {}",
"function and method return types", position
); );
err.emit(); err.emit();
hir::TyKind::Err hir::TyKind::Err
@ -1528,16 +1585,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self, &mut self,
decl: &FnDecl, decl: &FnDecl,
mut in_band_ty_params: Option<(LocalDefId, &mut Vec<hir::GenericParam<'hir>>)>, mut in_band_ty_params: Option<(LocalDefId, &mut Vec<hir::GenericParam<'hir>>)>,
impl_trait_return_allow: bool, kind: FnDeclKind,
make_ret_async: Option<NodeId>, make_ret_async: Option<NodeId>,
) -> &'hir hir::FnDecl<'hir> { ) -> &'hir hir::FnDecl<'hir> {
debug!( debug!(
"lower_fn_decl(\ "lower_fn_decl(\
fn_decl: {:?}, \ fn_decl: {:?}, \
in_band_ty_params: {:?}, \ in_band_ty_params: {:?}, \
impl_trait_return_allow: {}, \ kind: {:?}, \
make_ret_async: {:?})", make_ret_async: {:?})",
decl, in_band_ty_params, impl_trait_return_allow, make_ret_async, decl, in_band_ty_params, kind, make_ret_async,
); );
let lt_mode = if make_ret_async.is_some() { let lt_mode = if make_ret_async.is_some() {
// In `async fn`, argument-position elided lifetimes // In `async fn`, argument-position elided lifetimes
@ -1567,7 +1624,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ImplTraitContext::Universal(ibty, this.current_hir_id_owner), ImplTraitContext::Universal(ibty, this.current_hir_id_owner),
) )
} else { } else {
this.lower_ty_direct(&param.ty, ImplTraitContext::disallowed()) this.lower_ty_direct(
&param.ty,
ImplTraitContext::Disallowed(match kind {
FnDeclKind::Fn | FnDeclKind::Inherent => {
unreachable!("fn should allow in-band lifetimes")
}
FnDeclKind::ExternFn => ImplTraitPosition::ExternFnParam,
FnDeclKind::Closure => ImplTraitPosition::ClosureParam,
FnDeclKind::Pointer => ImplTraitPosition::PointerParam,
FnDeclKind::Trait => ImplTraitPosition::TraitParam,
FnDeclKind::Impl => ImplTraitPosition::ImplParam,
}),
)
} }
})) }))
}); });
@ -1582,13 +1651,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
match decl.output { match decl.output {
FnRetTy::Ty(ref ty) => { FnRetTy::Ty(ref ty) => {
let context = match in_band_ty_params { let context = match in_band_ty_params {
Some((def_id, _)) if impl_trait_return_allow => { Some((def_id, _)) if kind.impl_trait_return_allowed() => {
ImplTraitContext::ReturnPositionOpaqueTy { ImplTraitContext::ReturnPositionOpaqueTy {
fn_def_id: def_id, fn_def_id: def_id,
origin: hir::OpaqueTyOrigin::FnReturn(def_id), origin: hir::OpaqueTyOrigin::FnReturn(def_id),
} }
} }
_ => ImplTraitContext::disallowed(), _ => ImplTraitContext::Disallowed(match kind {
FnDeclKind::Fn | FnDeclKind::Inherent => {
unreachable!("fn should allow in-band lifetimes")
}
FnDeclKind::ExternFn => ImplTraitPosition::ExternFnReturn,
FnDeclKind::Closure => ImplTraitPosition::ClosureReturn,
FnDeclKind::Pointer => ImplTraitPosition::PointerReturn,
FnDeclKind::Trait => ImplTraitPosition::TraitReturn,
FnDeclKind::Impl => ImplTraitPosition::ImplReturn,
}),
}; };
hir::FnRetTy::Return(self.lower_ty(ty, context)) hir::FnRetTy::Return(self.lower_ty(ty, context))
} }
@ -1946,7 +2024,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericParamKind::Type { ref default, .. } => { GenericParamKind::Type { ref default, .. } => {
let kind = hir::GenericParamKind::Type { let kind = hir::GenericParamKind::Type {
default: default.as_ref().map(|x| { default: default.as_ref().map(|x| {
self.lower_ty(x, ImplTraitContext::Disallowed(ImplTraitPosition::Other)) self.lower_ty(x, ImplTraitContext::Disallowed(ImplTraitPosition::Type))
}), }),
synthetic: false, synthetic: false,
}; };
@ -1954,9 +2032,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
(hir::ParamName::Plain(self.lower_ident(param.ident)), kind) (hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
} }
GenericParamKind::Const { ref ty, kw_span: _, ref default } => { GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
let ty = self let ty =
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| { self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
this.lower_ty(&ty, ImplTraitContext::disallowed()) this.lower_ty(&ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type))
}); });
let default = default.as_ref().map(|def| self.lower_anon_const(def)); let default = default.as_ref().map(|def| self.lower_anon_const(def));
( (

View File

@ -1,3 +1,5 @@
use crate::ImplTraitPosition;
use super::{ImplTraitContext, LoweringContext, ParamMode}; use super::{ImplTraitContext, LoweringContext, ParamMode};
use rustc_ast::ptr::P; use rustc_ast::ptr::P;
@ -33,7 +35,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct"); let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
break hir::PatKind::TupleStruct(qpath, pats, ddpos); break hir::PatKind::TupleStruct(qpath, pats, ddpos);
@ -49,7 +51,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
break hir::PatKind::Path(qpath); break hir::PatKind::Path(qpath);
} }
@ -59,7 +61,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| hir::PatField { let fs = self.arena.alloc_from_iter(fields.iter().map(|f| hir::PatField {

View File

@ -1,3 +1,5 @@
use crate::ImplTraitPosition;
use super::{AnonymousLifetimeMode, ImplTraitContext, LoweringContext, ParamMode}; use super::{AnonymousLifetimeMode, ImplTraitContext, LoweringContext, ParamMode};
use super::{GenericArgsCtor, ParenthesizedGenericArgs}; use super::{GenericArgsCtor, ParenthesizedGenericArgs};
@ -184,7 +186,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
param_mode, param_mode,
0, 0,
ParenthesizedGenericArgs::Err, ParenthesizedGenericArgs::Err,
ImplTraitContext::disallowed(), ImplTraitContext::Disallowed(ImplTraitPosition::Path),
) )
})), })),
span: self.lower_span(p.span), span: self.lower_span(p.span),
@ -392,11 +394,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// we generally don't permit such things (see #51008). // we generally don't permit such things (see #51008).
self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| { self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
let ParenthesizedArgs { span, inputs, inputs_span, output } = data; let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
let inputs = this.arena.alloc_from_iter( let inputs = this.arena.alloc_from_iter(inputs.iter().map(|ty| {
inputs.iter().map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed())), this.lower_ty_direct(
); ty,
ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam),
)
}));
let output_ty = match output { let output_ty = match output {
FnRetTy::Ty(ty) => this.lower_ty(&ty, ImplTraitContext::disallowed()), FnRetTy::Ty(ty) => this
.lower_ty(&ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn)),
FnRetTy::Default(_) => this.arena.alloc(this.ty_tup(*span, &[])), FnRetTy::Default(_) => this.arena.alloc(this.ty_tup(*span, &[])),
}; };
let args = smallvec![GenericArg::Type(this.ty_tup(*inputs_span, inputs))]; let args = smallvec![GenericArg::Type(this.ty_tup(*inputs_span, inputs))];

View File

@ -57,20 +57,20 @@ fn _rpit_dyn() -> Box<dyn Tr1<As1: Copy>> { Box::new(S1) }
const _cdef: impl Tr1<As1: Copy> = S1; const _cdef: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable //~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562] //~| ERROR `impl Trait` only allowed in function and inherent method return types
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed. // FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// const _cdef_dyn: &dyn Tr1<As1: Copy> = &S1; // const _cdef_dyn: &dyn Tr1<As1: Copy> = &S1;
static _sdef: impl Tr1<As1: Copy> = S1; static _sdef: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable //~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562] //~| ERROR `impl Trait` only allowed in function and inherent method return types
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed. // FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// static _sdef_dyn: &dyn Tr1<As1: Copy> = &S1; // static _sdef_dyn: &dyn Tr1<As1: Copy> = &S1;
fn main() { fn main() {
let _: impl Tr1<As1: Copy> = S1; let _: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable //~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562] //~| ERROR `impl Trait` only allowed in function and inherent method return types
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed. // FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// let _: &dyn Tr1<As1: Copy> = &S1; // let _: &dyn Tr1<As1: Copy> = &S1;
} }

View File

@ -115,19 +115,19 @@ LL | let _: impl Tr1<As1: Copy> = S1;
= note: see issue #52662 <https://github.com/rust-lang/rust/issues/52662> for more information = 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 = help: add `#![feature(associated_type_bounds)]` to the crate attributes to enable
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/feature-gate-associated_type_bounds.rs:58:14 --> $DIR/feature-gate-associated_type_bounds.rs:58:14
| |
LL | const _cdef: impl Tr1<As1: Copy> = S1; LL | const _cdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/feature-gate-associated_type_bounds.rs:64:15 --> $DIR/feature-gate-associated_type_bounds.rs:64:15
| |
LL | static _sdef: impl Tr1<As1: Copy> = S1; LL | static _sdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/feature-gate-associated_type_bounds.rs:71:12 --> $DIR/feature-gate-associated_type_bounds.rs:71:12
| |
LL | let _: impl Tr1<As1: Copy> = S1; LL | let _: impl Tr1<As1: Copy> = S1;

View File

@ -2,6 +2,6 @@ use std::fmt::Debug;
fn main() { fn main() {
let x: Option<impl Debug> = Some(44_u32); let x: Option<impl Debug> = Some(44_u32);
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
println!("{:?}", x); println!("{:?}", x);
} }

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-54600.rs:4:19 --> $DIR/issue-54600.rs:4:19
| |
LL | let x: Option<impl Debug> = Some(44_u32); LL | let x: Option<impl Debug> = Some(44_u32);

View File

@ -3,5 +3,5 @@ use std::ops::Add;
fn main() { fn main() {
let i: i32 = 0; let i: i32 = 0;
let j: &impl Add = &i; let j: &impl Add = &i;
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
} }

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-54840.rs:5:13 --> $DIR/issue-54840.rs:5:13
| |
LL | let j: &impl Add = &i; LL | let j: &impl Add = &i;

View File

@ -8,5 +8,5 @@ fn mk_gen() -> impl Generator<Return=!, Yield=()> {
fn main() { fn main() {
let gens: [impl Generator<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ]; let gens: [impl Generator<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ];
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
} }

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-58504.rs:10:16 --> $DIR/issue-58504.rs:10:16
| |
LL | let gens: [impl Generator<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ]; LL | let gens: [impl Generator<Return=!, Yield=()>;2] = [ mk_gen(), mk_gen() ];

View File

@ -5,9 +5,9 @@ impl Lam for B {}
pub struct Wrap<T>(T); pub struct Wrap<T>(T);
const _A: impl Lam = { const _A: impl Lam = {
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
let x: Wrap<impl Lam> = Wrap(B); let x: Wrap<impl Lam> = Wrap(B);
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
x.0 x.0
}; };

View File

@ -1,10 +1,10 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/issue-58956.rs:7:11 --> $DIR/issue-58956.rs:7:11
| |
LL | const _A: impl Lam = { LL | const _A: impl Lam = {
| ^^^^^^^^ | ^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-58956.rs:9:17 --> $DIR/issue-58956.rs:9:17
| |
LL | let x: Wrap<impl Lam> = Wrap(B); LL | let x: Wrap<impl Lam> = Wrap(B);

View File

@ -1,4 +1,4 @@
fn main() { fn main() {
let x : (impl Copy,) = (true,); let x : (impl Copy,) = (true,);
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
} }

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-70971.rs:2:14 --> $DIR/issue-70971.rs:2:14
| |
LL | let x : (impl Copy,) = (true,); LL | let x : (impl Copy,) = (true,);

View File

@ -1,7 +1,7 @@
struct Bug { struct Bug {
V1: [(); { V1: [(); {
let f: impl core::future::Future<Output = u8> = async { 1 }; let f: impl core::future::Future<Output = u8> = async { 1 };
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
//~| expected identifier //~| expected identifier
1 1
}], }],

View File

@ -9,7 +9,7 @@ LL | let f: impl core::future::Future<Output = u8> = async { 1 };
= help: set `edition = "2021"` in `Cargo.toml` = help: set `edition = "2021"` in `Cargo.toml`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide = note: for more on editions, read https://doc.rust-lang.org/edition-guide
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-79099.rs:3:16 --> $DIR/issue-79099.rs:3:16
| |
LL | let f: impl core::future::Future<Output = u8> = async { 1 }; LL | let f: impl core::future::Future<Output = u8> = async { 1 };

View File

@ -1,8 +1,8 @@
struct Foo<T = impl Copy>(T); struct Foo<T = impl Copy>(T);
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
type Result<T, E = impl std::error::Error> = std::result::Result<T, E>; type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// should not cause ICE // should not cause ICE
fn x() -> Foo { fn x() -> Foo {

View File

@ -1,10 +1,10 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16 --> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16
| |
LL | struct Foo<T = impl Copy>(T); LL | struct Foo<T = impl Copy>(T);
| ^^^^^^^^^ | ^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:4:20 --> $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>; LL | type Result<T, E = impl std::error::Error> = std::result::Result<T, E>;

View File

@ -3,7 +3,7 @@ impl Trait for () {}
fn foo<'a: 'a>() { fn foo<'a: 'a>() {
let _x: impl Trait = (); let _x: impl Trait = ();
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
} }
fn main() {} fn main() {}

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-84919.rs:5:13 --> $DIR/issue-84919.rs:5:13
| |
LL | let _x: impl Trait = (); LL | let _x: impl Trait = ();

View File

@ -1,5 +1,5 @@
static x: impl Fn(&str) -> Result<&str, ()> = move |source| { static x: impl Fn(&str) -> Result<&str, ()> = move |source| {
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
let res = (move |source| Ok(source))(source); let res = (move |source| Ok(source))(source);
let res = res.or((move |source| Ok(source))(source)); let res = res.or((move |source| Ok(source))(source));
res res

View File

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

View File

@ -14,5 +14,5 @@ impl<F> Struct<F> {
fn main() { fn main() {
let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(()); let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(());
//~^ `impl Trait` not allowed outside of function and method return types //~^ `impl Trait` only allowed in function and inherent method return types
} }

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/issue-87295.rs:16:31 --> $DIR/issue-87295.rs:16:31
| |
LL | let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(()); LL | let _do_not_waste: Struct<impl Trait<Output = i32>> = Struct::new(());

View File

@ -7,7 +7,7 @@ fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {} fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
//~^ ERROR nested `impl Trait` is not allowed //~^ ERROR nested `impl Trait` is not allowed
//~^^ `impl Trait` not allowed //~| `impl Trait` only allowed in function and inherent method return types
fn bad_in_arg_position(_: impl Into<impl Debug>) { } fn bad_in_arg_position(_: impl Into<impl Debug>) { }
//~^ ERROR nested `impl Trait` is not allowed //~^ ERROR nested `impl Trait` is not allowed
@ -23,7 +23,7 @@ fn allowed_in_assoc_type() -> impl Iterator<Item=impl Fn()> {
} }
fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> { fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {
//~^ `impl Trait` not allowed //~^ `impl Trait` only allowed in function and inherent method return types
|| 5 || 5
} }

View File

@ -34,13 +34,13 @@ LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| | nested `impl Trait` here | | nested `impl Trait` here
| outer `impl Trait` | outer `impl Trait`
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer return
--> $DIR/nested_impl_trait.rs:8:32 --> $DIR/nested_impl_trait.rs:8:32
| |
LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {} LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/nested_impl_trait.rs:25:42 --> $DIR/nested_impl_trait.rs:25:42
| |
LL | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> { LL | fn allowed_in_ret_type() -> impl Fn() -> impl Into<u32> {

View File

@ -13,61 +13,61 @@ fn in_adt_in_parameters(_: Vec<impl Debug>) { panic!() }
// Disallowed // Disallowed
fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() } fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() } fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() } fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() } fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() } fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() } fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() } fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() } fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() } fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
//~^^ ERROR nested `impl Trait` is not allowed //~^^ ERROR nested `impl Trait` is not allowed
// Disallowed // Disallowed
fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() } fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() } fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
//~| ERROR nested `impl Trait` is not allowed //~| ERROR nested `impl Trait` is not allowed
// Disallowed // Disallowed
fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() } fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_Fn_parameter_in_generics<F: Fn(impl Debug)> (_: F) { panic!() } fn in_Fn_parameter_in_generics<F: Fn(impl Debug)> (_: F) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() } fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Allowed // Allowed
@ -80,22 +80,22 @@ fn in_impl_Trait_in_return() -> impl IntoIterator<Item = impl IntoIterator> {
// Disallowed // Disallowed
struct InBraceStructField { x: impl Debug } struct InBraceStructField { x: impl Debug }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
struct InAdtInBraceStructField { x: Vec<impl Debug> } struct InAdtInBraceStructField { x: Vec<impl Debug> }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
struct InTupleStructField(impl Debug); struct InTupleStructField(impl Debug);
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
enum InEnum { enum InEnum {
InBraceVariant { x: impl Debug }, InBraceVariant { x: impl Debug },
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
InTupleVariant(impl Debug), InTupleVariant(impl Debug),
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Allowed // Allowed
@ -106,7 +106,7 @@ trait InTraitDefnParameters {
// Disallowed // Disallowed
trait InTraitDefnReturn { trait InTraitDefnReturn {
fn in_return() -> impl Debug; fn in_return() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Allowed and disallowed in trait impls // Allowed and disallowed in trait impls
@ -123,7 +123,7 @@ impl DummyTrait for () {
// Allowed // Allowed
fn in_trait_impl_return() -> impl Debug { () } fn in_trait_impl_return() -> impl Debug { () }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Allowed // Allowed
@ -136,10 +136,10 @@ impl DummyType {
// Disallowed // Disallowed
extern "C" { extern "C" {
fn in_foreign_parameters(_: impl Debug); fn in_foreign_parameters(_: impl Debug);
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
fn in_foreign_return() -> impl Debug; fn in_foreign_return() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Allowed // Allowed
@ -155,97 +155,97 @@ type InTypeAlias<R> = impl Debug;
//~^ ERROR `impl Trait` in type aliases is unstable //~^ ERROR `impl Trait` in type aliases is unstable
type InReturnInTypeAlias<R> = fn() -> impl Debug; type InReturnInTypeAlias<R> = fn() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
//~| ERROR `impl Trait` in type aliases is unstable //~| ERROR `impl Trait` in type aliases is unstable
// Disallowed in impl headers // Disallowed in impl headers
impl PartialEq<impl Debug> for () { impl PartialEq<impl Debug> for () {
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Disallowed in impl headers // Disallowed in impl headers
impl PartialEq<()> for impl Debug { impl PartialEq<()> for impl Debug {
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Disallowed in inherent impls // Disallowed in inherent impls
impl impl Debug { impl impl Debug {
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Disallowed in inherent impls // Disallowed in inherent impls
struct InInherentImplAdt<T> { t: T } struct InInherentImplAdt<T> { t: T }
impl InInherentImplAdt<impl Debug> { impl InInherentImplAdt<impl Debug> {
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }
// Disallowed in where clauses // Disallowed in where clauses
fn in_fn_where_clause() fn in_fn_where_clause()
where impl Debug: Debug where impl Debug: Debug
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
{ {
} }
// Disallowed in where clauses // Disallowed in where clauses
fn in_adt_in_fn_where_clause() fn in_adt_in_fn_where_clause()
where Vec<impl Debug>: Debug where Vec<impl Debug>: Debug
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
{ {
} }
// Disallowed // Disallowed
fn in_trait_parameter_in_fn_where_clause<T>() fn in_trait_parameter_in_fn_where_clause<T>()
where T: PartialEq<impl Debug> where T: PartialEq<impl Debug>
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
{ {
} }
// Disallowed // Disallowed
fn in_Fn_parameter_in_fn_where_clause<T>() fn in_Fn_parameter_in_fn_where_clause<T>()
where T: Fn(impl Debug) where T: Fn(impl Debug)
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
{ {
} }
// Disallowed // Disallowed
fn in_Fn_return_in_fn_where_clause<T>() fn in_Fn_return_in_fn_where_clause<T>()
where T: Fn() -> impl Debug where T: Fn() -> impl Debug
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
{ {
} }
// Disallowed // Disallowed
struct InStructGenericParamDefault<T = impl Debug>(T); struct InStructGenericParamDefault<T = impl Debug>(T);
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) } enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) }
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
trait InTraitGenericParamDefault<T = impl Debug> {} trait InTraitGenericParamDefault<T = impl Debug> {}
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
type InTypeAliasGenericParamDefault<T = impl Debug> = T; type InTypeAliasGenericParamDefault<T = impl Debug> = T;
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
// Disallowed // Disallowed
impl <T = impl Debug> T {} impl <T = impl Debug> T {}
//~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions //~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
//~| WARNING this was previously accepted by the compiler but is being phased out //~| WARNING this was previously accepted by the compiler but is being phased out
//~| ERROR `impl Trait` not allowed outside of function and method return types //~| ERROR `impl Trait` only allowed in function and inherent method return types
//~| ERROR no nominal type found //~| ERROR no nominal type found
// Disallowed // Disallowed
fn in_method_generic_param_default<T = impl Debug>(_: T) {} fn in_method_generic_param_default<T = impl Debug>(_: T) {}
//~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions //~^ ERROR defaults for type parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
//~| WARNING this was previously accepted by the compiler but is being phased out //~| WARNING this was previously accepted by the compiler but is being phased out
//~| ERROR `impl Trait` not allowed outside of function and method return types //~| ERROR `impl Trait` only allowed in function and inherent method return types
fn main() { fn main() {
let _in_local_variable: impl Fn() = || {}; let _in_local_variable: impl Fn() = || {};
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
let _in_return_in_local_variable = || -> impl Fn() { || {} }; let _in_return_in_local_variable = || -> impl Fn() { || {} };
//~^ ERROR `impl Trait` not allowed outside of function and method return types //~^ ERROR `impl Trait` only allowed in function and inherent method return types
} }

View File

@ -43,247 +43,247 @@ LL | type InReturnInTypeAlias<R> = fn() -> impl Debug;
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer param
--> $DIR/where-allowed.rs:15:40 --> $DIR/where-allowed.rs:15:40
| |
LL | fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() } LL | fn in_fn_parameter_in_parameters(_: fn(impl Debug)) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer return
--> $DIR/where-allowed.rs:19:42 --> $DIR/where-allowed.rs:19:42
| |
LL | fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() } LL | fn in_fn_return_in_parameters(_: fn() -> impl Debug) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer param
--> $DIR/where-allowed.rs:23:38 --> $DIR/where-allowed.rs:23:38
| |
LL | fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() } LL | fn in_fn_parameter_in_return() -> fn(impl Debug) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer return
--> $DIR/where-allowed.rs:27:40 --> $DIR/where-allowed.rs:27:40
| |
LL | fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() } LL | fn in_fn_return_in_return() -> fn() -> impl Debug { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait param
--> $DIR/where-allowed.rs:31:49 --> $DIR/where-allowed.rs:31:49
| |
LL | fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() } LL | fn in_dyn_Fn_parameter_in_parameters(_: &dyn Fn(impl Debug)) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/where-allowed.rs:35:51 --> $DIR/where-allowed.rs:35:51
| |
LL | fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() } LL | fn in_dyn_Fn_return_in_parameters(_: &dyn Fn() -> impl Debug) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait param
--> $DIR/where-allowed.rs:39:55 --> $DIR/where-allowed.rs:39:55
| |
LL | fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() } LL | fn in_dyn_Fn_parameter_in_return() -> &'static dyn Fn(impl Debug) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/where-allowed.rs:43:57 --> $DIR/where-allowed.rs:43:57
| |
LL | fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() } LL | fn in_dyn_Fn_return_in_return() -> &'static dyn Fn() -> impl Debug { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait param
--> $DIR/where-allowed.rs:47:51 --> $DIR/where-allowed.rs:47:51
| |
LL | fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() } LL | fn in_impl_Fn_parameter_in_parameters(_: &impl Fn(impl Debug)) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/where-allowed.rs:52:53 --> $DIR/where-allowed.rs:52:53
| |
LL | fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() } LL | fn in_impl_Fn_return_in_parameters(_: &impl Fn() -> impl Debug) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait param
--> $DIR/where-allowed.rs:56:57 --> $DIR/where-allowed.rs:56:57
| |
LL | fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() } LL | fn in_impl_Fn_parameter_in_return() -> &'static impl Fn(impl Debug) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/where-allowed.rs:61:59 --> $DIR/where-allowed.rs:61:59
| |
LL | fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() } LL | fn in_impl_Fn_return_in_return() -> &'static impl Fn() -> impl Debug { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait param
--> $DIR/where-allowed.rs:65:38 --> $DIR/where-allowed.rs:65:38
| |
LL | fn in_Fn_parameter_in_generics<F: Fn(impl Debug)> (_: F) { panic!() } LL | fn in_Fn_parameter_in_generics<F: Fn(impl Debug)> (_: F) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/where-allowed.rs:69:40 --> $DIR/where-allowed.rs:69:40
| |
LL | fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() } LL | fn in_Fn_return_in_generics<F: Fn() -> impl Debug> (_: F) { panic!() }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:82:32 --> $DIR/where-allowed.rs:82:32
| |
LL | struct InBraceStructField { x: impl Debug } LL | struct InBraceStructField { x: impl Debug }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in path
--> $DIR/where-allowed.rs:86:41 --> $DIR/where-allowed.rs:86:41
| |
LL | struct InAdtInBraceStructField { x: Vec<impl Debug> } LL | struct InAdtInBraceStructField { x: Vec<impl Debug> }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:90:27 --> $DIR/where-allowed.rs:90:27
| |
LL | struct InTupleStructField(impl Debug); LL | struct InTupleStructField(impl Debug);
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:95:25 --> $DIR/where-allowed.rs:95:25
| |
LL | InBraceVariant { x: impl Debug }, LL | InBraceVariant { x: impl Debug },
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:97:20 --> $DIR/where-allowed.rs:97:20
| |
LL | InTupleVariant(impl Debug), LL | InTupleVariant(impl Debug),
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait method return
--> $DIR/where-allowed.rs:108:23 --> $DIR/where-allowed.rs:108:23
| |
LL | fn in_return() -> impl Debug; LL | fn in_return() -> impl Debug;
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `impl` method return
--> $DIR/where-allowed.rs:125:34 --> $DIR/where-allowed.rs:125:34
| |
LL | fn in_trait_impl_return() -> impl Debug { () } LL | fn in_trait_impl_return() -> impl Debug { () }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `extern fn` param
--> $DIR/where-allowed.rs:138:33 --> $DIR/where-allowed.rs:138:33
| |
LL | fn in_foreign_parameters(_: impl Debug); LL | fn in_foreign_parameters(_: impl Debug);
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `extern fn` return
--> $DIR/where-allowed.rs:141:31 --> $DIR/where-allowed.rs:141:31
| |
LL | fn in_foreign_return() -> impl Debug; LL | fn in_foreign_return() -> impl Debug;
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer return
--> $DIR/where-allowed.rs:157:39 --> $DIR/where-allowed.rs:157:39
| |
LL | type InReturnInTypeAlias<R> = fn() -> impl Debug; LL | type InReturnInTypeAlias<R> = fn() -> impl Debug;
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait
--> $DIR/where-allowed.rs:162:16 --> $DIR/where-allowed.rs:162:16
| |
LL | impl PartialEq<impl Debug> for () { LL | impl PartialEq<impl Debug> for () {
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:167:24 --> $DIR/where-allowed.rs:167:24
| |
LL | impl PartialEq<()> for impl Debug { LL | impl PartialEq<()> for impl Debug {
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:172:6 --> $DIR/where-allowed.rs:172:6
| |
LL | impl impl Debug { LL | impl impl Debug {
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:178:24 --> $DIR/where-allowed.rs:178:24
| |
LL | impl InInherentImplAdt<impl Debug> { LL | impl InInherentImplAdt<impl Debug> {
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:184:11 --> $DIR/where-allowed.rs:184:11
| |
LL | where impl Debug: Debug LL | where impl Debug: Debug
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:191:15 --> $DIR/where-allowed.rs:191:15
| |
LL | where Vec<impl Debug>: Debug LL | where Vec<impl Debug>: Debug
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in bound
--> $DIR/where-allowed.rs:198:24 --> $DIR/where-allowed.rs:198:24
| |
LL | where T: PartialEq<impl Debug> LL | where T: PartialEq<impl Debug>
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait param
--> $DIR/where-allowed.rs:205:17 --> $DIR/where-allowed.rs:205:17
| |
LL | where T: Fn(impl Debug) LL | where T: Fn(impl Debug)
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `Fn` trait return
--> $DIR/where-allowed.rs:212:22 --> $DIR/where-allowed.rs:212:22
| |
LL | where T: Fn() -> impl Debug LL | where T: Fn() -> impl Debug
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:218:40 --> $DIR/where-allowed.rs:218:40
| |
LL | struct InStructGenericParamDefault<T = impl Debug>(T); LL | struct InStructGenericParamDefault<T = impl Debug>(T);
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:222:36 --> $DIR/where-allowed.rs:222:36
| |
LL | enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) } LL | enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) }
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:226:38 --> $DIR/where-allowed.rs:226:38
| |
LL | trait InTraitGenericParamDefault<T = impl Debug> {} LL | trait InTraitGenericParamDefault<T = impl Debug> {}
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:230:41 --> $DIR/where-allowed.rs:230:41
| |
LL | type InTypeAliasGenericParamDefault<T = impl Debug> = T; LL | type InTypeAliasGenericParamDefault<T = impl Debug> = T;
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:234:11 --> $DIR/where-allowed.rs:234:11
| |
LL | impl <T = impl Debug> T {} LL | impl <T = impl Debug> T {}
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in type
--> $DIR/where-allowed.rs:241:40 --> $DIR/where-allowed.rs:241:40
| |
LL | fn in_method_generic_param_default<T = impl Debug>(_: T) {} LL | fn in_method_generic_param_default<T = impl Debug>(_: T) {}
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding
--> $DIR/where-allowed.rs:247:29 --> $DIR/where-allowed.rs:247:29
| |
LL | let _in_local_variable: impl Fn() = || {}; LL | let _in_local_variable: impl Fn() = || {};
| ^^^^^^^^^ | ^^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in closure return
--> $DIR/where-allowed.rs:249:46 --> $DIR/where-allowed.rs:249:46
| |
LL | let _in_return_in_local_variable = || -> impl Fn() { || {} }; LL | let _in_return_in_local_variable = || -> impl Fn() { || {} };

View File

@ -7,22 +7,22 @@ trait Iterable {
} }
struct Container<T: Iterable<Item = impl Foo>> { struct Container<T: Iterable<Item = impl Foo>> {
//~^ ERROR `impl Trait` not allowed //~^ ERROR `impl Trait` only allowed in function and inherent method return types
field: T field: T
} }
enum Enum<T: Iterable<Item = impl Foo>> { enum Enum<T: Iterable<Item = impl Foo>> {
//~^ ERROR `impl Trait` not allowed //~^ ERROR `impl Trait` only allowed in function and inherent method return types
A(T), A(T),
} }
union Union<T: Iterable<Item = impl Foo> + Copy> { union Union<T: Iterable<Item = impl Foo> + Copy> {
//~^ ERROR `impl Trait` not allowed //~^ ERROR `impl Trait` only allowed in function and inherent method return types
x: T, x: T,
} }
type Type<T: Iterable<Item = impl Foo>> = T; type Type<T: Iterable<Item = impl Foo>> = T;
//~^ ERROR `impl Trait` not allowed //~^ ERROR `impl Trait` only allowed in function and inherent method return types
fn main() { fn main() {
} }

View File

@ -1,22 +1,22 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic
--> $DIR/issue-47715.rs:9:37 --> $DIR/issue-47715.rs:9:37
| |
LL | struct Container<T: Iterable<Item = impl Foo>> { LL | struct Container<T: Iterable<Item = impl Foo>> {
| ^^^^^^^^ | ^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic
--> $DIR/issue-47715.rs:14:30 --> $DIR/issue-47715.rs:14:30
| |
LL | enum Enum<T: Iterable<Item = impl Foo>> { LL | enum Enum<T: Iterable<Item = impl Foo>> {
| ^^^^^^^^ | ^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic
--> $DIR/issue-47715.rs:19:32 --> $DIR/issue-47715.rs:19:32
| |
LL | union Union<T: Iterable<Item = impl Foo> + Copy> { LL | union Union<T: Iterable<Item = impl Foo> + Copy> {
| ^^^^^^^^ | ^^^^^^^^
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in generic
--> $DIR/issue-47715.rs:24:30 --> $DIR/issue-47715.rs:24:30
| |
LL | type Type<T: Iterable<Item = impl Foo>> = T; LL | type Type<T: Iterable<Item = impl Foo>> = T;

View File

@ -4,7 +4,7 @@
// FIXME: this is ruled out for now but should work // FIXME: this is ruled out for now but should work
type Foo = fn() -> impl Send; type Foo = fn() -> impl Send;
//~^ ERROR: `impl Trait` not allowed outside of function and method return types //~^ ERROR: `impl Trait` only allowed in function and inherent method return types
fn make_foo() -> Foo { fn make_foo() -> Foo {
|| 15 || 15

View File

@ -1,4 +1,4 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `fn` pointer return
--> $DIR/type-alias-impl-trait-fn-type.rs:6:20 --> $DIR/type-alias-impl-trait-fn-type.rs:6:20
| |
LL | type Foo = fn() -> impl Send; LL | type Foo = fn() -> impl Send;