Skip to content

Commit 0cea2c1

Browse files
committed
Centralize const item lowering logic
1 parent 42745dc commit 0cea2c1

File tree

1 file changed

+30
-34
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+30
-34
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
182182
}
183183
ItemKind::Static(box ast::StaticItem {
184184
ident,
185-
ty: t,
185+
ty,
186186
safety: _,
187187
mutability: m,
188188
expr: e,
189189
define_opaque,
190190
}) => {
191191
let ident = self.lower_ident(*ident);
192-
let (ty, body_id) =
193-
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
192+
let ty =
193+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
194+
let body_id = self.lower_const_body(span, e.as_deref());
194195
self.lower_define_opaque(hir_id, define_opaque);
195196
hir::ItemKind::Static(ident, ty, *m, body_id)
196197
}
@@ -203,22 +204,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
203204
..
204205
}) => {
205206
let ident = self.lower_ident(*ident);
206-
let (generics, (ty, body_id)) = self.lower_generics(
207+
let (generics, (ty, (body_id, ct_arg))) = self.lower_generics(
207208
generics,
208209
id,
209210
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
210211
|this| {
211-
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
212+
let ty = this
213+
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
214+
(ty, this.lower_const_item(span, expr.as_deref()))
212215
},
213216
);
214217
self.lower_define_opaque(hir_id, &define_opaque);
215-
let ct_arg = if self.tcx.features().min_generic_const_args()
216-
&& let Some(expr) = expr
217-
{
218-
self.try_lower_as_const_path(expr)
219-
} else {
220-
None
221-
};
222218
hir::ItemKind::Const(ident, ty, generics, body_id, ct_arg)
223219
}
224220
ItemKind::Fn(box Fn {
@@ -501,13 +497,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
501497

502498
fn lower_const_item(
503499
&mut self,
504-
ty: &Ty,
505500
span: Span,
506501
body: Option<&Expr>,
507-
impl_trait_position: ImplTraitPosition,
508-
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
509-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
510-
(ty, self.lower_const_body(span, body))
502+
) -> (hir::BodyId, Option<&'hir hir::ConstArg<'hir>>) {
503+
let ct_arg = if self.tcx.features().min_generic_const_args()
504+
&& let Some(expr) = body
505+
{
506+
self.try_lower_as_const_path(expr)
507+
} else {
508+
None
509+
};
510+
let body_id = if body.is_some() && ct_arg.is_none() {
511+
// TODO: lower as const block instead
512+
self.lower_const_body(span, body)
513+
} else {
514+
self.lower_const_body(span, body)
515+
};
516+
(body_id, ct_arg)
511517
}
512518

513519
#[instrument(level = "debug", skip(self))]
@@ -819,15 +825,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
819825
|this| {
820826
let ty = this
821827
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
822-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
823-
let ct_arg = if this.tcx.features().min_generic_const_args()
824-
&& let Some(expr) = expr
825-
{
826-
this.try_lower_as_const_path(expr)
827-
} else {
828-
None
829-
};
830-
hir::TraitItemKind::Const(ty, body, ct_arg)
828+
match expr.as_deref().map(|e| this.lower_const_item(i.span, Some(e))) {
829+
Some((body, ct_arg)) => {
830+
hir::TraitItemKind::Const(ty, Some(body), ct_arg)
831+
}
832+
None => hir::TraitItemKind::Const(ty, None, None),
833+
}
831834
},
832835
);
833836

@@ -1018,15 +1021,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10181021
|this| {
10191022
let ty = this
10201023
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1021-
let body = this.lower_const_body(i.span, expr.as_deref());
10221024
this.lower_define_opaque(hir_id, &define_opaque);
1023-
let ct_arg = if this.tcx.features().min_generic_const_args()
1024-
&& let Some(expr) = expr
1025-
{
1026-
this.try_lower_as_const_path(expr)
1027-
} else {
1028-
None
1029-
};
1025+
let (body, ct_arg) = this.lower_const_item(i.span, expr.as_deref());
10301026
hir::ImplItemKind::Const(ty, body, ct_arg)
10311027
},
10321028
),

0 commit comments

Comments
 (0)