Skip to content

Commit b0668f7

Browse files
committed
rename the lower_ty and lower_ty_direct to lower_ty_and_alloc and
`lower_ty`
1 parent ed0006a commit b0668f7

File tree

5 files changed

+90
-55
lines changed

5 files changed

+90
-55
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9898
// Let statements are allowed to have impl trait in bindings.
9999
let super_ = l.super_.map(|span| self.lower_span(span));
100100
let ty = l.ty.as_ref().map(|t| {
101-
self.lower_ty(t, self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable))
101+
self.lower_ty_and_alloc(
102+
t,
103+
self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable),
104+
)
102105
});
103106
let init = l.kind.init().map(|init| self.lower_expr(init));
104107
let hir_id = self.lower_node_id(l.id);

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
157157
}
158158
ExprKind::Cast(expr, ty) => {
159159
let expr = self.lower_expr(expr);
160-
let ty =
161-
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
160+
let ty = self.lower_ty_and_alloc(
161+
ty,
162+
ImplTraitContext::Disallowed(ImplTraitPosition::Cast),
163+
);
162164
hir::ExprKind::Cast(expr, ty)
163165
}
164166
ExprKind::Type(expr, ty) => {
165167
let expr = self.lower_expr(expr);
166-
let ty =
167-
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
168+
let ty = self.lower_ty_and_alloc(
169+
ty,
170+
ImplTraitContext::Disallowed(ImplTraitPosition::Cast),
171+
);
168172
hir::ExprKind::Type(expr, ty)
169173
}
170174
ExprKind::AddrOf(k, m, ohs) => {
@@ -334,7 +338,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
334338
}
335339
ExprKind::FormatArgs(fmt) => self.lower_format_args(e.span, fmt),
336340
ExprKind::OffsetOf(container, fields) => hir::ExprKind::OffsetOf(
337-
self.lower_ty(
341+
self.lower_ty_and_alloc(
338342
container,
339343
ImplTraitContext::Disallowed(ImplTraitPosition::OffsetOf),
340344
),
@@ -370,7 +374,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
370374
*kind,
371375
self.lower_expr(expr),
372376
ty.as_ref().map(|ty| {
373-
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast))
377+
self.lower_ty_and_alloc(
378+
ty,
379+
ImplTraitContext::Disallowed(ImplTraitPosition::Cast),
380+
)
374381
}),
375382
),
376383

@@ -616,7 +623,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
616623
});
617624

618625
if let Some(ty) = opt_ty {
619-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path));
626+
let ty =
627+
self.lower_ty_and_alloc(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path));
620628
let block_expr = self.arena.alloc(self.expr_block(whole_block));
621629
hir::ExprKind::Type(block_expr, ty)
622630
} else {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
264264
define_opaque,
265265
}) => {
266266
let ident = self.lower_ident(*ident);
267-
let ty =
268-
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
267+
let ty = self.lower_ty_and_alloc(
268+
ty,
269+
ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy),
270+
);
269271
let body_id = self.lower_const_body(span, e.as_deref());
270272
self.lower_define_opaque(hir_id, define_opaque);
271273
hir::ItemKind::Static(*m, ident, ty, body_id)
@@ -279,8 +281,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
279281
id,
280282
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
281283
|this| {
282-
let ty = this
283-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
284+
let ty = this.lower_ty_and_alloc(
285+
ty,
286+
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
287+
);
284288
let rhs = this.lower_const_item_rhs(attrs, rhs.as_ref(), span);
285289
(ty, rhs)
286290
},
@@ -379,7 +383,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
379383
);
380384
this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
381385
}
382-
Some(ty) => this.lower_ty(
386+
Some(ty) => this.lower_ty_and_alloc(
383387
ty,
384388
ImplTraitContext::OpaqueTy {
385389
origin: hir::OpaqueTyOrigin::TyAlias {
@@ -453,7 +457,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
453457
.as_deref()
454458
.map(|of_trait| this.lower_trait_impl_header(of_trait));
455459

456-
let lowered_ty = this.lower_ty(
460+
let lowered_ty = this.lower_ty_and_alloc(
457461
ty,
458462
ImplTraitContext::Disallowed(ImplTraitPosition::ImplSelf),
459463
);
@@ -758,8 +762,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
758762
safety,
759763
define_opaque,
760764
}) => {
761-
let ty =
762-
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
765+
let ty = self.lower_ty_and_alloc(
766+
ty,
767+
ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy),
768+
);
763769
let safety = self.lower_safety(*safety, hir::Safety::Unsafe);
764770
if define_opaque.is_some() {
765771
self.dcx().span_err(i.span, "foreign statics cannot define opaque types");
@@ -870,7 +876,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
870876
&mut self,
871877
(index, f): (usize, &FieldDef),
872878
) -> hir::FieldDef<'hir> {
873-
let ty = self.lower_ty(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
879+
let ty = self
880+
.lower_ty_and_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
874881
let hir_id = self.lower_node_id(f.id);
875882
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field);
876883
hir::FieldDef {
@@ -908,8 +915,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
908915
i.id,
909916
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
910917
|this| {
911-
let ty = this
912-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
918+
let ty = this.lower_ty_and_alloc(
919+
ty,
920+
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
921+
);
913922
let rhs = rhs
914923
.as_ref()
915924
.map(|rhs| this.lower_const_item_rhs(attrs, Some(rhs), i.span));
@@ -1008,7 +1017,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10081017
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
10091018
|this| {
10101019
let ty = ty.as_ref().map(|x| {
1011-
this.lower_ty(
1020+
this.lower_ty_and_alloc(
10121021
x,
10131022
ImplTraitContext::Disallowed(ImplTraitPosition::AssocTy),
10141023
)
@@ -1120,8 +1129,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
11201129
i.id,
11211130
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
11221131
|this| {
1123-
let ty = this
1124-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1132+
let ty = this.lower_ty_and_alloc(
1133+
ty,
1134+
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
1135+
);
11251136
this.lower_define_opaque(hir_id, &define_opaque);
11261137
let rhs = this.lower_const_item_rhs(attrs, rhs.as_ref(), i.span);
11271138
hir::ImplItemKind::Const(ty, rhs)
@@ -1180,7 +1191,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11801191
hir::ImplItemKind::Type(ty)
11811192
}
11821193
Some(ty) => {
1183-
let ty = this.lower_ty(
1194+
let ty = this.lower_ty_and_alloc(
11841195
ty,
11851196
ImplTraitContext::OpaqueTy {
11861197
origin: hir::OpaqueTyOrigin::TyAlias {
@@ -1916,7 +1927,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19161927
bound_generic_params,
19171928
hir::GenericParamSource::Binder,
19181929
),
1919-
bounded_ty: self.lower_ty(
1930+
bounded_ty: self.lower_ty_and_alloc(
19201931
bounded_ty,
19211932
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
19221933
),
@@ -1945,10 +1956,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
19451956
}
19461957
WherePredicateKind::EqPredicate(WhereEqPredicate { lhs_ty, rhs_ty }) => {
19471958
hir::WherePredicateKind::EqPredicate(hir::WhereEqPredicate {
1948-
lhs_ty: self
1949-
.lower_ty(lhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1950-
rhs_ty: self
1951-
.lower_ty(rhs_ty, ImplTraitContext::Disallowed(ImplTraitPosition::Bound)),
1959+
lhs_ty: self.lower_ty_and_alloc(
1960+
lhs_ty,
1961+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1962+
),
1963+
rhs_ty: self.lower_ty_and_alloc(
1964+
rhs_ty,
1965+
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
1966+
),
19521967
})
19531968
}
19541969
});

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11231123
let kind = match &constraint.kind {
11241124
AssocItemConstraintKind::Equality { term } => {
11251125
let term = match term {
1126-
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
1126+
Term::Ty(ty) => self.lower_ty_and_alloc(ty, itctx).into(),
11271127
Term::Const(c) => self.lower_anon_const_to_const_arg(c).into(),
11281128
};
11291129
hir::AssocItemConstraintKind::Equality { term }
@@ -1248,7 +1248,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12481248
}
12491249
_ => {}
12501250
}
1251-
GenericArg::Type(self.lower_ty(ty, itctx).try_as_ambig_ty().unwrap())
1251+
GenericArg::Type(self.lower_ty_and_alloc(ty, itctx).try_as_ambig_ty().unwrap())
12521252
}
12531253
ast::GenericArg::Const(ct) => {
12541254
GenericArg::Const(self.lower_anon_const_to_const_arg(ct).try_as_ambig_ct().unwrap())
@@ -1257,8 +1257,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12571257
}
12581258

12591259
#[instrument(level = "debug", skip(self))]
1260-
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> &'hir hir::Ty<'hir> {
1261-
self.arena.alloc(self.lower_ty_direct(t, itctx))
1260+
fn lower_ty_and_alloc(&mut self, t: &Ty, itctx: ImplTraitContext) -> &'hir hir::Ty<'hir> {
1261+
self.arena.alloc(self.lower_ty(t, itctx))
12621262
}
12631263

12641264
fn lower_path_ty(
@@ -1322,11 +1322,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13221322
self.ty(span, hir::TyKind::Tup(tys))
13231323
}
13241324

1325-
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
1325+
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
13261326
let kind = match &t.kind {
13271327
TyKind::Infer => hir::TyKind::Infer(()),
13281328
TyKind::Err(guar) => hir::TyKind::Err(*guar),
1329-
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
1329+
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty_and_alloc(ty, itctx)),
13301330
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
13311331
TyKind::Ref(region, mt) => {
13321332
let lifetime = self.lower_ty_direct_lifetime(t, *region);
@@ -1360,15 +1360,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13601360
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
13611361
hir::TyKind::UnsafeBinder(self.arena.alloc(hir::UnsafeBinderTy {
13621362
generic_params,
1363-
inner_ty: self.lower_ty(&f.inner_ty, itctx),
1363+
inner_ty: self.lower_ty_and_alloc(&f.inner_ty, itctx),
13641364
}))
13651365
}
13661366
TyKind::Never => hir::TyKind::Never,
13671367
TyKind::Tup(tys) => hir::TyKind::Tup(
1368-
self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty_direct(ty, itctx))),
1368+
self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty(ty, itctx))),
13691369
),
13701370
TyKind::Paren(ty) => {
1371-
return self.lower_ty_direct(ty, itctx);
1371+
return self.lower_ty(ty, itctx);
13721372
}
13731373
TyKind::Path(qself, path) => {
13741374
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
@@ -1391,7 +1391,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13911391
))
13921392
}
13931393
TyKind::Array(ty, length) => hir::TyKind::Array(
1394-
self.lower_ty(ty, itctx),
1394+
self.lower_ty_and_alloc(ty, itctx),
13951395
self.lower_array_length_to_const_arg(length),
13961396
),
13971397
TyKind::TraitObject(bounds, kind) => {
@@ -1490,9 +1490,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14901490
}
14911491
}
14921492
}
1493-
TyKind::Pat(ty, pat) => {
1494-
hir::TyKind::Pat(self.lower_ty(ty, itctx), self.lower_ty_pat(pat, ty.span))
1495-
}
1493+
TyKind::Pat(ty, pat) => hir::TyKind::Pat(
1494+
self.lower_ty_and_alloc(ty, itctx),
1495+
self.lower_ty_pat(pat, ty.span),
1496+
),
14961497
TyKind::MacCall(_) => {
14971498
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
14981499
}
@@ -1691,7 +1692,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16911692
ImplTraitContext::Disallowed(ImplTraitPosition::PointerParam)
16921693
}
16931694
};
1694-
self.lower_ty_direct(&param.ty, itctx)
1695+
self.lower_ty(&param.ty, itctx)
16951696
}));
16961697

16971698
let output = match coro {
@@ -1730,7 +1731,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17301731
ImplTraitContext::Disallowed(ImplTraitPosition::PointerReturn)
17311732
}
17321733
};
1733-
hir::FnRetTy::Return(self.lower_ty(ty, itctx))
1734+
hir::FnRetTy::Return(self.lower_ty_and_alloc(ty, itctx))
17341735
}
17351736
FnRetTy::Default(span) => hir::FnRetTy::DefaultReturn(self.lower_span(*span)),
17361737
},
@@ -1841,7 +1842,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18411842
// Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
18421843
// `impl Future` opaque type that `async fn` implicitly
18431844
// generates.
1844-
self.lower_ty(ty, itctx)
1845+
self.lower_ty_and_alloc(ty, itctx)
18451846
}
18461847
FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
18471848
};
@@ -2034,7 +2035,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20342035
}
20352036
})
20362037
.map(|def| {
2037-
self.lower_ty(
2038+
self.lower_ty_and_alloc(
20382039
def,
20392040
ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
20402041
)
@@ -2045,8 +2046,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20452046
(hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
20462047
}
20472048
GenericParamKind::Const { ty, span: _, default } => {
2048-
let ty = self
2049-
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault));
2049+
let ty = self.lower_ty_and_alloc(
2050+
ty,
2051+
ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
2052+
);
20502053

20512054
// Not only do we deny const param defaults in binders but we also map them to `None`
20522055
// since later compiler stages cannot handle them (and shouldn't need to be able to).
@@ -2196,7 +2199,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21962199
}
21972200

21982201
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy<'hir> {
2199-
hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
2202+
hir::MutTy { ty: self.lower_ty_and_alloc(&mt.ty, itctx), mutbl: mt.mutbl }
22002203
}
22012204

22022205
#[instrument(level = "debug", skip(self), ret)]

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3636
let qself = qself
3737
.as_ref()
3838
// Reject cases like `<impl Trait>::Assoc` and `<impl Trait as Trait>::Assoc`.
39-
.map(|q| self.lower_ty(&q.ty, ImplTraitContext::Disallowed(ImplTraitPosition::Path)));
39+
.map(|q| {
40+
self.lower_ty_and_alloc(
41+
&q.ty,
42+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
43+
)
44+
});
4045

4146
let partial_res =
4247
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
@@ -510,7 +515,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
510515
// we generally don't permit such things (see #51008).
511516
let ParenthesizedArgs { span, inputs, inputs_span, output } = data;
512517
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|ty| {
513-
self.lower_ty_direct(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam))
518+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitParam))
514519
}));
515520
let output_ty = match output {
516521
// Only allow `impl Trait` in return position. i.e.:
@@ -520,9 +525,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
520525
// ```
521526
FnRetTy::Ty(ty) if matches!(itctx, ImplTraitContext::OpaqueTy { .. }) => {
522527
if self.tcx.features().impl_trait_in_fn_trait_return() {
523-
self.lower_ty(ty, itctx)
528+
self.lower_ty_and_alloc(ty, itctx)
524529
} else {
525-
self.lower_ty(
530+
self.lower_ty_and_alloc(
526531
ty,
527532
ImplTraitContext::FeatureGated(
528533
ImplTraitPosition::FnTraitReturn,
@@ -531,9 +536,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
531536
)
532537
}
533538
}
534-
FnRetTy::Ty(ty) => {
535-
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn))
536-
}
539+
FnRetTy::Ty(ty) => self.lower_ty_and_alloc(
540+
ty,
541+
ImplTraitContext::Disallowed(ImplTraitPosition::FnTraitReturn),
542+
),
537543
FnRetTy::Default(_) => self.arena.alloc(self.ty_tup(*span, &[])),
538544
};
539545
let args = smallvec![GenericArg::Type(

0 commit comments

Comments
 (0)