Skip to content

Commit 4b4f8cd

Browse files
committed
Centralize const item lowering logic
1 parent 283db70 commit 4b4f8cd

File tree

1 file changed

+26
-16
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+26
-16
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
170170
}
171171
ItemKind::Static(box ast::StaticItem {
172172
ident,
173-
ty: t,
173+
ty,
174174
safety: _,
175175
mutability: m,
176176
expr: e,
177177
define_opaque,
178178
}) => {
179179
let ident = self.lower_ident(*ident);
180-
let (ty, body_id) =
181-
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
180+
let ty =
181+
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
182+
let body_id = self.lower_const_body(span, e.as_deref());
182183
self.lower_define_opaque(hir_id, define_opaque);
183184
hir::ItemKind::Static(ident, ty, *m, body_id)
184185
}
@@ -196,7 +197,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
196197
id,
197198
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
198199
|this| {
199-
this.lower_const_item(ty, span, expr.as_deref(), ImplTraitPosition::ConstTy)
200+
let ty = this
201+
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
202+
(ty, this.lower_const_item(span, expr.as_deref()))
200203
},
201204
);
202205
self.lower_define_opaque(hir_id, &define_opaque);
@@ -480,15 +483,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
480483
}
481484
}
482485

483-
fn lower_const_item(
484-
&mut self,
485-
ty: &Ty,
486-
span: Span,
487-
body: Option<&Expr>,
488-
impl_trait_position: ImplTraitPosition,
489-
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
490-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
491-
(ty, self.lower_const_body(span, body))
486+
fn lower_const_item(&mut self, span: Span, body: Option<&Expr>) -> hir::BodyId {
487+
self.lower_const_body(span, body)
488+
// TODO: code to add next
489+
// let ct_arg = if self.tcx.features().min_generic_const_args()
490+
// && let Some(expr) = body
491+
// {
492+
// self.try_lower_as_const_path(expr)
493+
// } else {
494+
// None
495+
// };
496+
// let body_id = if body.is_some() && ct_arg.is_none() {
497+
// // TODO: lower as const block instead
498+
// self.lower_const_body(span, body)
499+
// } else {
500+
// self.lower_const_body(span, body)
501+
// };
502+
// (body_id, ct_arg)
492503
}
493504

494505
#[instrument(level = "debug", skip(self))]
@@ -797,8 +808,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
797808
|this| {
798809
let ty = this
799810
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
800-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
801-
811+
let body = expr.as_deref().map(|e| this.lower_const_item(i.span, Some(e)));
802812
hir::TraitItemKind::Const(ty, body)
803813
},
804814
);
@@ -990,8 +1000,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
9901000
|this| {
9911001
let ty = this
9921002
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
993-
let body = this.lower_const_body(i.span, expr.as_deref());
9941003
this.lower_define_opaque(hir_id, &define_opaque);
1004+
let body = this.lower_const_item(i.span, expr.as_deref());
9951005
hir::ImplItemKind::Const(ty, body)
9961006
},
9971007
),

0 commit comments

Comments
 (0)