Skip to content

Commit 42745dc

Browse files
committed
Lower to const path if possible in const item value
1 parent 6054bd5 commit 42745dc

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
212212
},
213213
);
214214
self.lower_define_opaque(hir_id, &define_opaque);
215-
// TODO: make const arg instead of always using None
216-
hir::ItemKind::Const(ident, ty, generics, body_id, None)
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+
};
222+
hir::ItemKind::Const(ident, ty, generics, body_id, ct_arg)
217223
}
218224
ItemKind::Fn(box Fn {
219225
sig: FnSig { decl, header, span: fn_sig_span },
@@ -814,8 +820,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
814820
let ty = this
815821
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
816822
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
817-
// TODO: make const arg instead of always using None
818-
hir::TraitItemKind::Const(ty, body, None)
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)
819831
},
820832
);
821833

@@ -1008,8 +1020,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
10081020
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10091021
let body = this.lower_const_body(i.span, expr.as_deref());
10101022
this.lower_define_opaque(hir_id, &define_opaque);
1011-
// TODO: make const arg instead of always using None
1012-
hir::ImplItemKind::Const(ty, body, None)
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+
};
1030+
hir::ImplItemKind::Const(ty, body, ct_arg)
10131031
},
10141032
),
10151033
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20572057
}
20582058
}
20592059

2060+
/// Assumes mgca feature is enabled.
2061+
fn try_lower_as_const_path(&mut self, expr: &Expr) -> Option<&'hir hir::ConstArg<'hir>> {
2062+
let ExprKind::Path(qself, path) = &expr.kind else { return None };
2063+
let qpath = self.lower_qpath(
2064+
expr.id,
2065+
qself,
2066+
path,
2067+
ParamMode::Optional,
2068+
AllowReturnTypeNotation::No,
2069+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2070+
None,
2071+
);
2072+
let ct_kind = hir::ConstArgKind::Path(qpath);
2073+
Some(self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind }))
2074+
}
2075+
20602076
/// Used when lowering a type argument that turned out to actually be a const argument.
20612077
///
20622078
/// Only use for that purpose since otherwise it will create a duplicate def.

0 commit comments

Comments
 (0)