Skip to content

Commit 1a5639f

Browse files
committed
Centralize const item lowering logic
1 parent 2a5da7a commit 1a5639f

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
@@ -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
}
@@ -208,7 +209,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
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);
@@ -492,15 +495,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
492495
}
493496
}
494497

495-
fn lower_const_item(
496-
&mut self,
497-
ty: &Ty,
498-
span: Span,
499-
body: Option<&Expr>,
500-
impl_trait_position: ImplTraitPosition,
501-
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
502-
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(impl_trait_position));
503-
(ty, self.lower_const_body(span, body))
498+
fn lower_const_item(&mut self, span: Span, body: Option<&Expr>) -> hir::BodyId {
499+
self.lower_const_body(span, body)
500+
// TODO: code to add next
501+
// let ct_arg = if self.tcx.features().min_generic_const_args()
502+
// && let Some(expr) = body
503+
// {
504+
// self.try_lower_as_const_path(expr)
505+
// } else {
506+
// None
507+
// };
508+
// let body_id = if body.is_some() && ct_arg.is_none() {
509+
// // TODO: lower as const block instead
510+
// self.lower_const_body(span, body)
511+
// } else {
512+
// self.lower_const_body(span, body)
513+
// };
514+
// (body_id, ct_arg)
504515
}
505516

506517
#[instrument(level = "debug", skip(self))]
@@ -809,8 +820,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
809820
|this| {
810821
let ty = this
811822
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
812-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
813-
823+
let body = expr.as_deref().map(|e| this.lower_const_item(i.span, Some(e)));
814824
hir::TraitItemKind::Const(ty, body)
815825
},
816826
);
@@ -1002,8 +1012,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
10021012
|this| {
10031013
let ty = this
10041014
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
1005-
let body = this.lower_const_body(i.span, expr.as_deref());
10061015
this.lower_define_opaque(hir_id, &define_opaque);
1016+
let body = this.lower_const_item(i.span, expr.as_deref());
10071017
hir::ImplItemKind::Const(ty, body)
10081018
},
10091019
),

0 commit comments

Comments
 (0)