Pass ImplTraitContext as &, there's no need for that to be &mut

This commit is contained in:
Santiago Pastorino 2022-09-14 17:39:52 -03:00
parent 669f2d4550
commit 861055094c
No known key found for this signature in database
GPG Key ID: 8131A24E0C79EFAF
6 changed files with 84 additions and 107 deletions

View File

@ -220,7 +220,7 @@ pub(crate) fn lower_inline_asm(
&sym.qself, &sym.qself,
&sym.path, &sym.path,
ParamMode::Optional, ParamMode::Optional,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
hir::InlineAsmOperand::SymStatic { path, def_id } hir::InlineAsmOperand::SymStatic { path, def_id }
} else { } else {

View File

@ -84,9 +84,10 @@ fn lower_stmts(
} }
fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> { fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {
let ty = l.ty.as_ref().map(|t| { let ty = l
self.lower_ty(t, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Variable)) .ty
}); .as_ref()
.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);

View File

@ -66,7 +66,7 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
seg, seg,
ParamMode::Optional, ParamMode::Optional,
ParenthesizedGenericArgs::Err, ParenthesizedGenericArgs::Err,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
)); ));
let receiver = self.lower_expr(receiver); let receiver = self.lower_expr(receiver);
let args = let args =
@ -89,14 +89,14 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'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 let ty =
.lower_ty(ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type)); 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 let ty =
.lower_ty(ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type)); 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) => {
@ -225,7 +225,7 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
hir::ExprKind::Path(qpath) hir::ExprKind::Path(qpath)
} }
@ -259,7 +259,7 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
&se.qself, &se.qself,
&se.path, &se.path,
ParamMode::Optional, ParamMode::Optional,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &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))),
@ -556,14 +556,12 @@ pub(super) fn make_async_expr(
async_gen_kind: hir::AsyncGeneratorKind, async_gen_kind: hir::AsyncGeneratorKind,
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
) -> hir::ExprKind<'hir> { ) -> hir::ExprKind<'hir> {
let output = let output = match ret_ty {
match ret_ty { Some(ty) => hir::FnRetTy::Return(
Some(ty) => hir::FnRetTy::Return(self.lower_ty( self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)),
&ty, ),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock), None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
)), };
None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
};
// Resume argument type. We let the compiler infer this to simplify the lowering. It is // Resume argument type. We let the compiler infer this to simplify the lowering. It is
// fully constrained by `future::from_generator`. // fully constrained by `future::from_generator`.
@ -1131,7 +1129,7 @@ fn destructure_assign_mut(
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
// Destructure like a tuple struct. // Destructure like a tuple struct.
let tuple_struct_pat = hir::PatKind::TupleStruct( let tuple_struct_pat = hir::PatKind::TupleStruct(
@ -1150,7 +1148,7 @@ fn destructure_assign_mut(
qself, qself,
path, path,
ParamMode::Optional, ParamMode::Optional,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
// Destructure like a unit struct. // Destructure like a unit struct.
let unit_struct_pat = hir::PatKind::Path(qpath); let unit_struct_pat = hir::PatKind::Path(qpath);
@ -1174,7 +1172,7 @@ fn destructure_assign_mut(
&se.qself, &se.qself,
&se.path, &se.path,
ParamMode::Optional, ParamMode::Optional,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
let fields_omitted = match &se.rest { let fields_omitted = match &se.rest {
StructRest::Base(e) => { StructRest::Base(e) => {

View File

@ -313,8 +313,8 @@ fn lower_item_kind(
let (generics, ty) = self.lower_generics( let (generics, ty) = self.lower_generics(
&generics, &generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.lower_ty(ty, &mut ImplTraitContext::TypeAliasesOpaqueTy), |this| this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy),
); );
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
} }
@ -326,7 +326,7 @@ fn lower_item_kind(
let (generics, ty) = self.lower_generics( let (generics, ty) = self.lower_generics(
&generics, &generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.arena.alloc(this.ty(span, hir::TyKind::Err)), |this| this.arena.alloc(this.ty(span, hir::TyKind::Err)),
); );
hir::ItemKind::TyAlias(ty, generics) hir::ItemKind::TyAlias(ty, generics)
@ -335,7 +335,7 @@ fn lower_item_kind(
let (generics, variants) = self.lower_generics( let (generics, variants) = self.lower_generics(
generics, generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| { |this| {
this.arena.alloc_from_iter( this.arena.alloc_from_iter(
enum_definition.variants.iter().map(|x| this.lower_variant(x)), enum_definition.variants.iter().map(|x| this.lower_variant(x)),
@ -348,7 +348,7 @@ fn lower_item_kind(
let (generics, struct_def) = self.lower_generics( let (generics, struct_def) = self.lower_generics(
generics, generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.lower_variant_data(hir_id, struct_def), |this| this.lower_variant_data(hir_id, struct_def),
); );
hir::ItemKind::Struct(struct_def, generics) hir::ItemKind::Struct(struct_def, generics)
@ -357,7 +357,7 @@ fn lower_item_kind(
let (generics, vdata) = self.lower_generics( let (generics, vdata) = self.lower_generics(
generics, generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| this.lower_variant_data(hir_id, vdata), |this| this.lower_variant_data(hir_id, vdata),
); );
hir::ItemKind::Union(vdata, generics) hir::ItemKind::Union(vdata, generics)
@ -391,14 +391,12 @@ fn lower_item_kind(
let trait_ref = trait_ref.as_ref().map(|trait_ref| { let trait_ref = trait_ref.as_ref().map(|trait_ref| {
this.lower_trait_ref( this.lower_trait_ref(
trait_ref, trait_ref,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Trait), &ImplTraitContext::Disallowed(ImplTraitPosition::Trait),
) )
}); });
let lowered_ty = this.lower_ty( let lowered_ty = this
ty, .lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Type),
);
(trait_ref, lowered_ty) (trait_ref, lowered_ty)
}); });
@ -437,11 +435,11 @@ fn lower_item_kind(
let (generics, (unsafety, items, bounds)) = self.lower_generics( let (generics, (unsafety, items, bounds)) = self.lower_generics(
generics, generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| { |this| {
let bounds = this.lower_param_bounds( let bounds = this.lower_param_bounds(
bounds, bounds,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Bound), &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
); );
let items = this.arena.alloc_from_iter( let items = this.arena.alloc_from_iter(
items.iter().map(|item| this.lower_trait_item_ref(item)), items.iter().map(|item| this.lower_trait_item_ref(item)),
@ -456,11 +454,11 @@ fn lower_item_kind(
let (generics, bounds) = self.lower_generics( let (generics, bounds) = self.lower_generics(
generics, generics,
id, id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| { |this| {
this.lower_param_bounds( this.lower_param_bounds(
bounds, bounds,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Bound), &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
) )
}, },
); );
@ -483,7 +481,7 @@ fn lower_const_item(
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, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type)); let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
(ty, self.lower_const_body(span, body)) (ty, self.lower_const_body(span, body))
} }
@ -675,8 +673,8 @@ fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'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 let ty =
.lower_ty(t, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type)); 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 +742,11 @@ fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'
qself, qself,
path, path,
ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124) ParamMode::ExplicitNamed, // no `'_` in declarations (Issue #61124)
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
); );
self.arena.alloc(t) self.arena.alloc(t)
} else { } else {
self.lower_ty(&f.ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type)) 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,8 +769,7 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
let (generics, kind, has_default) = match i.kind { let (generics, kind, has_default) = match i.kind {
AssocItemKind::Const(_, ref ty, ref default) => { AssocItemKind::Const(_, ref ty, ref default) => {
let ty = let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
self.lower_ty(ty, &mut 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), body.is_some()) (hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
} }
@ -813,18 +810,15 @@ fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
let (generics, kind) = self.lower_generics( let (generics, kind) = self.lower_generics(
&generics, &generics,
i.id, i.id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| { |this| {
let ty = ty.as_ref().map(|x| { let ty = ty.as_ref().map(|x| {
this.lower_ty( this.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
x,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Type),
)
}); });
hir::TraitItemKind::Type( hir::TraitItemKind::Type(
this.lower_param_bounds( this.lower_param_bounds(
bounds, bounds,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
), ),
ty, ty,
) )
@ -877,8 +871,7 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
let (generics, kind) = match &i.kind { let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => { AssocItemKind::Const(_, ty, expr) => {
let ty = let ty = self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
self.lower_ty(ty, &mut 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())),
@ -905,14 +898,14 @@ fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
self.lower_generics( self.lower_generics(
&generics, &generics,
i.id, i.id,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Generic), &ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| match ty { |this| match ty {
None => { None => {
let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err)); let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err));
hir::ImplItemKind::TyAlias(ty) hir::ImplItemKind::TyAlias(ty)
} }
Some(ty) => { Some(ty) => {
let ty = this.lower_ty(ty, &mut ImplTraitContext::TypeAliasesOpaqueTy); let ty = this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy);
hir::ImplItemKind::TyAlias(ty) hir::ImplItemKind::TyAlias(ty)
} }
}, },
@ -1322,7 +1315,7 @@ fn lower_generics<T>(
&mut self, &mut self,
generics: &Generics, generics: &Generics,
parent_node_id: NodeId, parent_node_id: NodeId,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
f: impl FnOnce(&mut Self) -> T, f: impl FnOnce(&mut Self) -> T,
) -> (&'hir hir::Generics<'hir>, T) { ) -> (&'hir hir::Generics<'hir>, T) {
debug_assert!(self.impl_trait_defs.is_empty()); debug_assert!(self.impl_trait_defs.is_empty());
@ -1427,7 +1420,7 @@ pub(super) fn lower_generic_bound_predicate(
id: NodeId, id: NodeId,
kind: &GenericParamKind, kind: &GenericParamKind,
bounds: &[GenericBound], bounds: &[GenericBound],
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
origin: PredicateOrigin, origin: PredicateOrigin,
) -> Option<hir::WherePredicate<'hir>> { ) -> Option<hir::WherePredicate<'hir>> {
// Do not create a clause if we do not have anything inside it. // Do not create a clause if we do not have anything inside it.
@ -1502,14 +1495,12 @@ fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicat
span, span,
}) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { }) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
bound_generic_params: self.lower_generic_params(bound_generic_params), bound_generic_params: self.lower_generic_params(bound_generic_params),
bounded_ty: self.lower_ty( bounded_ty: self
bounded_ty, .lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Type),
),
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| { bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
self.lower_param_bound( self.lower_param_bound(
bound, bound,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Bound), &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
) )
})), })),
span: self.lower_span(span), span: self.lower_span(span),
@ -1524,20 +1515,16 @@ fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicat
lifetime: self.lower_lifetime(lifetime), lifetime: self.lower_lifetime(lifetime),
bounds: self.lower_param_bounds( bounds: self.lower_param_bounds(
bounds, bounds,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Bound), &ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
), ),
in_where_clause: true, in_where_clause: true,
}), }),
WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, span }) => { WherePredicate::EqPredicate(WhereEqPredicate { ref lhs_ty, ref rhs_ty, span }) => {
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate { hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
lhs_ty: self.lower_ty( lhs_ty: self
lhs_ty, .lower_ty(lhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Type), rhs_ty: self
), .lower_ty(rhs_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)),
rhs_ty: self.lower_ty(
rhs_ty,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Type),
),
span: self.lower_span(span), span: self.lower_span(span),
}) })
} }

View File

@ -984,7 +984,7 @@ fn lower_mac_args(&self, args: &MacArgs) -> MacArgs {
fn lower_assoc_ty_constraint( fn lower_assoc_ty_constraint(
&mut self, &mut self,
constraint: &AssocConstraint, constraint: &AssocConstraint,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::TypeBinding<'hir> { ) -> hir::TypeBinding<'hir> {
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx); debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
// lower generic arguments of identifier in constraint // lower generic arguments of identifier in constraint
@ -1003,7 +1003,7 @@ fn lower_assoc_ty_constraint(
} else { } else {
self.arena.alloc(hir::GenericArgs::none()) self.arena.alloc(hir::GenericArgs::none())
}; };
let mut itctx_tait = ImplTraitContext::TypeAliasesOpaqueTy; let itctx_tait = &ImplTraitContext::TypeAliasesOpaqueTy;
let kind = match constraint.kind { let kind = match constraint.kind {
AssocConstraintKind::Equality { ref term } => { AssocConstraintKind::Equality { ref term } => {
@ -1041,9 +1041,7 @@ fn lower_assoc_ty_constraint(
// then to an opaque type). // then to an opaque type).
// //
// FIXME: this is only needed until `impl Trait` is allowed in type aliases. // FIXME: this is only needed until `impl Trait` is allowed in type aliases.
ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => { ImplTraitContext::Disallowed(_) if self.is_in_dyn_type => (true, itctx_tait),
(true, &mut itctx_tait)
}
// We are in the parameter position, but not within a dyn type: // We are in the parameter position, but not within a dyn type:
// //
@ -1122,7 +1120,7 @@ fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
fn lower_generic_arg( fn lower_generic_arg(
&mut self, &mut self,
arg: &ast::GenericArg, arg: &ast::GenericArg,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::GenericArg<'hir> { ) -> hir::GenericArg<'hir> {
match arg { match arg {
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)), ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
@ -1184,7 +1182,7 @@ fn lower_generic_arg(
} }
#[instrument(level = "debug", skip(self))] #[instrument(level = "debug", skip(self))]
fn lower_ty(&mut self, t: &Ty, itctx: &mut ImplTraitContext) -> &'hir hir::Ty<'hir> { fn lower_ty(&mut self, t: &Ty, itctx: &ImplTraitContext) -> &'hir hir::Ty<'hir> {
self.arena.alloc(self.lower_ty_direct(t, itctx)) self.arena.alloc(self.lower_ty_direct(t, itctx))
} }
@ -1194,7 +1192,7 @@ fn lower_path_ty(
qself: &Option<QSelf>, qself: &Option<QSelf>,
path: &Path, path: &Path,
param_mode: ParamMode, param_mode: ParamMode,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::Ty<'hir> { ) -> hir::Ty<'hir> {
// Check whether we should interpret this as a bare trait object. // Check whether we should interpret this as a bare trait object.
// This check mirrors the one in late resolution. We only introduce this special case in // This check mirrors the one in late resolution. We only introduce this special case in
@ -1237,7 +1235,7 @@ fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
self.ty(span, hir::TyKind::Tup(tys)) self.ty(span, hir::TyKind::Tup(tys))
} }
fn lower_ty_direct(&mut self, t: &Ty, itctx: &mut ImplTraitContext) -> hir::Ty<'hir> { fn lower_ty_direct(&mut self, t: &Ty, itctx: &ImplTraitContext) -> hir::Ty<'hir> {
let kind = match t.kind { let kind = match t.kind {
TyKind::Infer => hir::TyKind::Infer, TyKind::Infer => hir::TyKind::Infer,
TyKind::Err => hir::TyKind::Err, TyKind::Err => hir::TyKind::Err,
@ -1348,7 +1346,7 @@ fn lower_ty_direct(&mut self, t: &Ty, itctx: &mut ImplTraitContext) -> hir::Ty<'
def_node_id, def_node_id,
bounds, bounds,
false, false,
&mut ImplTraitContext::TypeAliasesOpaqueTy, &ImplTraitContext::TypeAliasesOpaqueTy,
), ),
ImplTraitContext::Universal => { ImplTraitContext::Universal => {
let span = t.span; let span = t.span;
@ -1435,7 +1433,7 @@ fn lower_opaque_impl_trait(
opaque_ty_node_id: NodeId, opaque_ty_node_id: NodeId,
bounds: &GenericBounds, bounds: &GenericBounds,
in_trait: bool, in_trait: bool,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::TyKind<'hir> { ) -> hir::TyKind<'hir> {
// Make sure we know that some funky desugaring has been going on here. // Make sure we know that some funky desugaring has been going on here.
// This is a first: there is code in other places like for loop // This is a first: there is code in other places like for loop
@ -1681,11 +1679,11 @@ fn lower_fn_decl(
} }
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| { let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
if fn_node_id.is_some() { if fn_node_id.is_some() {
self.lower_ty_direct(&param.ty, &mut ImplTraitContext::Universal) self.lower_ty_direct(&param.ty, &ImplTraitContext::Universal)
} else { } else {
self.lower_ty_direct( self.lower_ty_direct(
&param.ty, &param.ty,
&mut ImplTraitContext::Disallowed(match kind { &ImplTraitContext::Disallowed(match kind {
FnDeclKind::Fn | FnDeclKind::Inherent => { FnDeclKind::Fn | FnDeclKind::Inherent => {
unreachable!("fn should allow in-band lifetimes") unreachable!("fn should allow in-band lifetimes")
} }
@ -2084,7 +2082,7 @@ fn lower_async_fn_output_type_to_future_bound(
fn lower_param_bound( fn lower_param_bound(
&mut self, &mut self,
tpb: &GenericBound, tpb: &GenericBound,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::GenericBound<'hir> { ) -> hir::GenericBound<'hir> {
match tpb { match tpb {
GenericBound::Trait(p, modifier) => hir::GenericBound::Trait( GenericBound::Trait(p, modifier) => hir::GenericBound::Trait(
@ -2200,7 +2198,7 @@ fn lower_generic_param_kind(
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, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type)) self.lower_ty(x, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
}), }),
synthetic: false, synthetic: false,
}; };
@ -2208,8 +2206,7 @@ fn lower_generic_param_kind(
(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 = let ty = self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type));
self.lower_ty(&ty, &mut 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));
( (
hir::ParamName::Plain(self.lower_ident(param.ident)), hir::ParamName::Plain(self.lower_ident(param.ident)),
@ -2219,11 +2216,7 @@ fn lower_generic_param_kind(
} }
} }
fn lower_trait_ref( fn lower_trait_ref(&mut self, p: &TraitRef, itctx: &ImplTraitContext) -> hir::TraitRef<'hir> {
&mut self,
p: &TraitRef,
itctx: &mut ImplTraitContext,
) -> hir::TraitRef<'hir> {
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) { let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
hir::QPath::Resolved(None, path) => path, hir::QPath::Resolved(None, path) => path,
qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath), qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
@ -2235,7 +2228,7 @@ fn lower_trait_ref(
fn lower_poly_trait_ref( fn lower_poly_trait_ref(
&mut self, &mut self,
p: &PolyTraitRef, p: &PolyTraitRef,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::PolyTraitRef<'hir> { ) -> hir::PolyTraitRef<'hir> {
let bound_generic_params = let bound_generic_params =
self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params); self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params);
@ -2243,7 +2236,7 @@ fn lower_poly_trait_ref(
hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) } hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) }
} }
fn lower_mt(&mut self, mt: &MutTy, itctx: &mut ImplTraitContext) -> hir::MutTy<'hir> { fn lower_mt(&mut self, mt: &MutTy, itctx: &ImplTraitContext) -> hir::MutTy<'hir> {
hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl } hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
} }
@ -2251,7 +2244,7 @@ fn lower_mt(&mut self, mt: &MutTy, itctx: &mut ImplTraitContext) -> hir::MutTy<'
fn lower_param_bounds( fn lower_param_bounds(
&mut self, &mut self,
bounds: &[GenericBound], bounds: &[GenericBound],
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::GenericBounds<'hir> { ) -> hir::GenericBounds<'hir> {
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx)) self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
} }
@ -2259,7 +2252,7 @@ fn lower_param_bounds(
fn lower_param_bounds_mut<'s, 'b>( fn lower_param_bounds_mut<'s, 'b>(
&'s mut self, &'s mut self,
bounds: &'s [GenericBound], bounds: &'s [GenericBound],
itctx: &'b mut ImplTraitContext, itctx: &'b ImplTraitContext,
) -> impl Iterator<Item = hir::GenericBound<'hir>> + Captures<'s> + Captures<'a> + Captures<'b> ) -> impl Iterator<Item = hir::GenericBound<'hir>> + Captures<'s> + Captures<'a> + Captures<'b>
{ {
bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx)) bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx))
@ -2291,7 +2284,7 @@ fn lower_generic_and_bounds(
node_id, node_id,
&GenericParamKind::Type { default: None }, &GenericParamKind::Type { default: None },
bounds, bounds,
&mut ImplTraitContext::Universal, &ImplTraitContext::Universal,
hir::PredicateOrigin::ImplTrait, hir::PredicateOrigin::ImplTrait,
); );

View File

@ -22,7 +22,7 @@ pub(crate) fn lower_qpath(
qself: &Option<QSelf>, qself: &Option<QSelf>,
p: &Path, p: &Path,
param_mode: ParamMode, param_mode: ParamMode,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::QPath<'hir> { ) -> hir::QPath<'hir> {
let qself_position = qself.as_ref().map(|q| q.position); let qself_position = qself.as_ref().map(|q| q.position);
let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx)); let qself = qself.as_ref().map(|q| self.lower_ty(&q.ty, itctx));
@ -156,7 +156,7 @@ pub(crate) fn lower_path_extra(
segment, segment,
param_mode, param_mode,
ParenthesizedGenericArgs::Err, ParenthesizedGenericArgs::Err,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path), &ImplTraitContext::Disallowed(ImplTraitPosition::Path),
) )
})), })),
span: self.lower_span(p.span), span: self.lower_span(p.span),
@ -180,7 +180,7 @@ pub(crate) fn lower_path_segment(
segment: &PathSegment, segment: &PathSegment,
param_mode: ParamMode, param_mode: ParamMode,
parenthesized_generic_args: ParenthesizedGenericArgs, parenthesized_generic_args: ParenthesizedGenericArgs,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> hir::PathSegment<'hir> { ) -> hir::PathSegment<'hir> {
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment,); debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment,);
let (mut generic_args, infer_args) = if let Some(ref generic_args) = segment.args { let (mut generic_args, infer_args) = if let Some(ref generic_args) = segment.args {
@ -316,7 +316,7 @@ pub(crate) fn lower_angle_bracketed_parameter_data(
&mut self, &mut self,
data: &AngleBracketedArgs, data: &AngleBracketedArgs,
param_mode: ParamMode, param_mode: ParamMode,
itctx: &mut ImplTraitContext, itctx: &ImplTraitContext,
) -> (GenericArgsCtor<'hir>, bool) { ) -> (GenericArgsCtor<'hir>, bool) {
let has_non_lt_args = data.args.iter().any(|arg| match arg { let has_non_lt_args = data.args.iter().any(|arg| match arg {
AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_)) AngleBracketedArg::Arg(ast::GenericArg::Lifetime(_))
@ -350,14 +350,12 @@ fn lower_parenthesized_parameter_data(
// we generally don't permit such things (see #51008). // we generally don't permit such things (see #51008).
let ParenthesizedArgs { span, inputs, inputs_span, output } = data; let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|ty| { let inputs = self.arena.alloc_from_iter(inputs.iter().map(|ty| {
self.lower_ty_direct( self.lower_ty_direct(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam))
ty,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam),
)
})); }));
let output_ty = match output { let output_ty = match output {
FnRetTy::Ty(ty) => self FnRetTy::Ty(ty) => {
.lower_ty(&ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn)), self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn))
}
FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])), FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
}; };
let args = smallvec![GenericArg::Type(self.arena.alloc(self.ty_tup(*inputs_span, inputs)))]; let args = smallvec![GenericArg::Type(self.arena.alloc(self.ty_tup(*inputs_span, inputs)))];