Add more information to impl Trait deny error

This commit is contained in:
Michael Goulet 2022-01-11 19:00:34 -08:00
parent 73a7423e77
commit f04f732503
34 changed files with 487 additions and 210 deletions

View File

@ -97,7 +97,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let ty = l
.ty
.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 hir_id = self.lower_node_id(l.id);
let pat = self.lower_pat(&l.pat);
@ -127,7 +127,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let ty = local
.ty
.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.mark_span_with_reason(DesugaringKind::LetElse, span, None);
let init = self.lower_expr(init);

View File

@ -1,3 +1,5 @@
use crate::{FnDeclKind, ImplTraitPosition};
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
use rustc_ast::attr;
@ -53,7 +55,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ParamMode::Optional,
0,
ParenthesizedGenericArgs::Err,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
));
let args = self.lower_exprs(args);
hir::ExprKind::MethodCall(hir_seg, args, self.lower_span(span))
@ -74,12 +76,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ExprKind::Cast(ref expr, ref ty) => {
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)
}
ExprKind::Type(ref expr, ref ty) => {
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)
}
ExprKind::AddrOf(k, m, ref ohs) => {
@ -203,7 +207,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
hir::ExprKind::Path(qpath)
}
@ -239,7 +243,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&se.qself,
&se.path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
)),
self.arena
.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>,
) -> hir::ExprKind<'hir> {
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)),
};
@ -827,7 +833,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
});
// 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(
capture_clause,
@ -919,7 +925,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// 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
// 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(
capture_clause,
@ -1064,7 +1070,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
// Destructure like a tuple struct.
let tuple_struct_pat =
@ -1089,7 +1095,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&se.qself,
&se.path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
let fields_omitted = match &se.rest {
StructRest::Base(e) => {

View File

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

View File

@ -256,19 +256,28 @@ enum ImplTraitContext<'b, 'a> {
/// Position in which `impl Trait` is disallowed.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ImplTraitPosition {
/// Disallowed in `let` / `const` / `static` bindings.
Binding,
/// All other positions.
Other,
Path,
Variable,
Type,
Trait,
AsyncBlock,
Bound,
Generic,
ExternFnParam,
ClosureParam,
PointerParam,
FnTraitParam,
TraitParam,
ImplParam,
ExternFnReturn,
ClosureReturn,
PointerReturn,
FnTraitReturn,
TraitReturn,
ImplReturn,
}
impl<'a> ImplTraitContext<'_, 'a> {
#[inline]
fn disallowed() -> Self {
ImplTraitContext::Disallowed(ImplTraitPosition::Other)
}
fn reborrow<'this>(&'this mut self) -> ImplTraitContext<'this, 'a> {
use self::ImplTraitContext::*;
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",
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>(
sess: &'a Session,
krate: &'a Crate,
@ -1232,11 +1289,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir::TyKind::BareFn(this.arena.alloc(hir::BareFnTy {
generic_params: this.lower_generic_params(
&f.generic_params,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
),
unsafety: this.lower_unsafety(f.unsafety),
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),
}))
})
@ -1357,14 +1414,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}),
))
}
ImplTraitContext::Disallowed(_) => {
ImplTraitContext::Disallowed(position) => {
let mut err = struct_span_err!(
self.sess,
t.span,
E0562,
"`impl Trait` not allowed outside of {}",
"function and method return types",
"`impl Trait` not allowed outside of \
function and inherent method return types",
);
err.note(&format!("found `impl Trait` in {}", position));
err.emit();
hir::TyKind::Err
}
@ -1528,16 +1586,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
decl: &FnDecl,
mut in_band_ty_params: Option<(LocalDefId, &mut Vec<hir::GenericParam<'hir>>)>,
impl_trait_return_allow: bool,
kind: FnDeclKind,
make_ret_async: Option<NodeId>,
) -> &'hir hir::FnDecl<'hir> {
debug!(
"lower_fn_decl(\
fn_decl: {:?}, \
in_band_ty_params: {:?}, \
impl_trait_return_allow: {}, \
kind: {:?}, \
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() {
// In `async fn`, argument-position elided lifetimes
@ -1567,7 +1625,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ImplTraitContext::Universal(ibty, this.current_hir_id_owner),
)
} 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 +1652,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
match decl.output {
FnRetTy::Ty(ref ty) => {
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 {
fn_def_id: 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))
}
@ -1915,7 +1994,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericParamKind::Type { ref default, .. } => {
let kind = hir::GenericParamKind::Type {
default: default.as_ref().map(|x| {
self.lower_ty(x, ImplTraitContext::Disallowed(ImplTraitPosition::Other))
self.lower_ty(x, ImplTraitContext::Disallowed(ImplTraitPosition::Type))
}),
synthetic: false,
};
@ -1923,9 +2002,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
(hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
}
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
let ty = self
.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
this.lower_ty(&ty, ImplTraitContext::disallowed())
let ty =
self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
this.lower_ty(&ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type))
});
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 rustc_ast::ptr::P;
@ -33,7 +35,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
break hir::PatKind::TupleStruct(qpath, pats, ddpos);
@ -49,7 +51,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
break hir::PatKind::Path(qpath);
}
@ -59,7 +61,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
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::{GenericArgsCtor, ParenthesizedGenericArgs};
@ -184,7 +186,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
param_mode,
0,
ParenthesizedGenericArgs::Err,
ImplTraitContext::disallowed(),
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
)
})),
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).
self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::PassThrough, |this| {
let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
let inputs = this.arena.alloc_from_iter(
inputs.iter().map(|ty| this.lower_ty_direct(ty, ImplTraitContext::disallowed())),
);
let inputs = this.arena.alloc_from_iter(inputs.iter().map(|ty| {
this.lower_ty_direct(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam),
)
}));
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, &[])),
};
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;
//~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562]
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types [E0562]
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// const _cdef_dyn: &dyn Tr1<As1: Copy> = &S1;
static _sdef: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562]
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types [E0562]
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// static _sdef_dyn: &dyn Tr1<As1: Copy> = &S1;
fn main() {
let _: impl Tr1<As1: Copy> = S1;
//~^ ERROR associated type bounds are unstable
//~| ERROR `impl Trait` not allowed outside of function and method return types [E0562]
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types [E0562]
// FIXME: uncomment when `impl_trait_in_bindings` feature is fixed.
// let _: &dyn Tr1<As1: Copy> = &S1;
}

View File

@ -115,23 +115,29 @@ 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` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/feature-gate-associated_type_bounds.rs:58:14
|
LL | const _cdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^
|
= note: found `impl Trait` in type
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/feature-gate-associated_type_bounds.rs:64:15
|
LL | static _sdef: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^
|
= note: found `impl Trait` in type
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/feature-gate-associated_type_bounds.rs:71:12
|
LL | let _: impl Tr1<As1: Copy> = S1;
| ^^^^^^^^^^^^^^^^^^^
|
= note: found `impl Trait` in variable
error[E0277]: the trait bound `<<Self as _Tr3>::A as Iterator>::Item: Copy` is not satisfied
--> $DIR/feature-gate-associated_type_bounds.rs:15:28

View File

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

View File

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

View File

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

View File

@ -1,8 +1,10 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/issue-54840.rs:5:13
|
LL | let j: &impl Add = &i;
| ^^^^^^^^
|
= note: found `impl Trait` in variable
error: aborting due to previous error

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,8 +1,10 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/issue-70971.rs:2:14
|
LL | let x : (impl Copy,) = (true,);
| ^^^^^^^^^
|
= note: found `impl Trait` in variable
error: aborting due to previous error

View File

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

View File

@ -9,11 +9,13 @@ LL | let f: impl core::future::Future<Output = u8> = async { 1 };
= help: set `edition = "2021"` in `Cargo.toml`
= 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` not allowed outside of function and inherent method return types
--> $DIR/issue-79099.rs:3:16
|
LL | let f: impl core::future::Future<Output = u8> = async { 1 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: found `impl Trait` in variable
error: aborting due to 2 previous errors

View File

@ -1,8 +1,8 @@
struct Foo<T = impl Copy>(T);
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
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` not allowed outside of function and inherent method return types
// should not cause ICE
fn x() -> Foo {

View File

@ -1,14 +1,18 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/issue-83929-impl-trait-in-generic-default.rs:1:16
|
LL | struct Foo<T = impl Copy>(T);
| ^^^^^^^^^
|
= note: found `impl Trait` in type
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $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>;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: found `impl Trait` in type
error: aborting due to 2 previous errors

View File

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

View File

@ -1,8 +1,10 @@
error[E0562]: `impl Trait` not allowed outside of function and method return types
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
--> $DIR/issue-84919.rs:5:13
|
LL | let _x: impl Trait = ();
| ^^^^^^^^^^
|
= note: found `impl Trait` in variable
error: aborting due to previous error

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,61 +13,61 @@ fn in_adt_in_parameters(_: Vec<impl Debug>) { panic!() }
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
//~^^ ERROR nested `impl Trait` is not allowed
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
//~| ERROR nested `impl Trait` is not allowed
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Disallowed
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` not allowed outside of function and inherent method return types
// Allowed
@ -80,22 +80,22 @@ fn in_impl_Trait_in_return() -> impl IntoIterator<Item = impl IntoIterator> {
// Disallowed
struct InBraceStructField { x: impl Debug }
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
struct InAdtInBraceStructField { x: Vec<impl Debug> }
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
struct InTupleStructField(impl Debug);
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
enum InEnum {
InBraceVariant { x: impl Debug },
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
InTupleVariant(impl Debug),
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Allowed
@ -106,7 +106,7 @@ trait InTraitDefnParameters {
// Disallowed
trait InTraitDefnReturn {
fn in_return() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Allowed and disallowed in trait impls
@ -123,7 +123,7 @@ impl DummyTrait for () {
// Allowed
fn in_trait_impl_return() -> impl Debug { () }
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Allowed
@ -136,10 +136,10 @@ impl DummyType {
// Disallowed
extern "C" {
fn in_foreign_parameters(_: impl Debug);
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
fn in_foreign_return() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Allowed
@ -155,97 +155,97 @@ type InTypeAlias<R> = impl Debug;
//~^ ERROR `impl Trait` in type aliases is unstable
type InReturnInTypeAlias<R> = fn() -> impl Debug;
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
//~| ERROR `impl Trait` in type aliases is unstable
// Disallowed in impl headers
impl PartialEq<impl Debug> for () {
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Disallowed in impl headers
impl PartialEq<()> for impl Debug {
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Disallowed in inherent impls
impl impl Debug {
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Disallowed in inherent impls
struct InInherentImplAdt<T> { t: T }
impl InInherentImplAdt<impl Debug> {
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}
// Disallowed in where clauses
fn in_fn_where_clause()
where impl Debug: Debug
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
{
}
// Disallowed in where clauses
fn in_adt_in_fn_where_clause()
where Vec<impl Debug>: Debug
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
{
}
// Disallowed
fn in_trait_parameter_in_fn_where_clause<T>()
where T: PartialEq<impl Debug>
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
{
}
// Disallowed
fn in_Fn_parameter_in_fn_where_clause<T>()
where T: Fn(impl Debug)
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
{
}
// Disallowed
fn in_Fn_return_in_fn_where_clause<T>()
where T: Fn() -> impl Debug
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
{
}
// Disallowed
struct InStructGenericParamDefault<T = impl Debug>(T);
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
enum InEnumGenericParamDefault<T = impl Debug> { Variant(T) }
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
trait InTraitGenericParamDefault<T = impl Debug> {}
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
type InTypeAliasGenericParamDefault<T = impl Debug> = T;
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
// Disallowed
impl <T = impl Debug> T {}
//~^ 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
//~| ERROR `impl Trait` not allowed outside of function and method return types
//~| ERROR `impl Trait` not allowed outside of function and inherent method return types
//~| ERROR no nominal type found
// Disallowed
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
//~| 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` not allowed outside of function and inherent method return types
fn main() {
let _in_local_variable: impl Fn() = || {};
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
let _in_return_in_local_variable = || -> impl Fn() { || {} };
//~^ ERROR `impl Trait` not allowed outside of function and method return types
//~^ ERROR `impl Trait` not allowed outside of function and inherent method return types
}

View File

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

View File

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

View File

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

View File

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