Skip to content

Commit 6946282

Browse files
committed
ast->hir lowering for const args
1 parent c90a4ed commit 6946282

File tree

3 files changed

+37
-43
lines changed

3 files changed

+37
-43
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ impl Expr {
11621162
/// `min_const_generics` as more complex expressions are not supported.
11631163
///
11641164
/// Does not ensure that the path resolves to a const param, the caller should check this.
1165-
pub fn is_potential_trivial_const_arg(&self) -> bool {
1165+
pub fn is_potential_trivial_const_arg(&self) -> Option<(NodeId, &Path)> {
11661166
let this = if let ExprKind::Block(block, None) = &self.kind
11671167
&& block.stmts.len() == 1
11681168
&& let StmtKind::Expr(expr) = &block.stmts[0].kind
@@ -1175,9 +1175,9 @@ impl Expr {
11751175
if let ExprKind::Path(None, path) = &this.kind
11761176
&& path.is_potential_trivial_const_arg()
11771177
{
1178-
true
1178+
Some((this.id, path))
11791179
} else {
1180-
false
1180+
None
11811181
}
11821182
}
11831183

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ extern crate tracing;
4242

4343
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait, TraitFnAsync};
4444

45-
use hir::ConstArgKind;
4645
use rustc_ast::ptr::P;
4746
use rustc_ast::visit;
4847
use rustc_ast::{self as ast, *};
@@ -1205,38 +1204,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12051204
ty,
12061205
);
12071206

1208-
// Construct an AnonConst where the expr is the "ty"'s path.
1209-
1210-
let parent_def_id = self.current_hir_id_owner;
1211-
let node_id = self.next_node_id();
1212-
let span = self.lower_span(ty.span);
1213-
1214-
// Add a definition for the in-band const def.
1215-
let def_id = self.create_def(
1216-
parent_def_id.def_id,
1217-
node_id,
1218-
DefPathData::AnonConst,
1219-
span,
1220-
);
1221-
1222-
let path_expr = Expr {
1223-
id: ty.id,
1224-
kind: ExprKind::Path(None, path.clone()),
1225-
span,
1226-
attrs: AttrVec::new(),
1227-
tokens: None,
1228-
};
1229-
1230-
let ct = self.with_new_scopes(|this| hir::AnonConst {
1231-
def_id,
1232-
hir_id: this.lower_node_id(node_id),
1233-
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
1234-
});
1235-
// FIXME(const_arg_kind)
12361207
return GenericArg::Const(
1237-
self.arena.alloc(ConstArg {
1238-
kind: ConstArgKind::AnonConst(span, ct),
1239-
}),
1208+
self.lower_const_arg_param(ty.id, path, itctx),
12401209
);
12411210
}
12421211
}
@@ -1245,16 +1214,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12451214
}
12461215
GenericArg::Type(self.lower_ty(&ty, itctx))
12471216
}
1248-
ast::GenericArg::Const(ct) => GenericArg::Const(self.arena.alloc(ConstArg {
1249-
// FIXME(const_arg_kind)
1250-
kind: ConstArgKind::AnonConst(
1251-
self.lower_span(ct.value.span),
1252-
self.lower_anon_const(&ct),
1217+
ast::GenericArg::Const(ct) => GenericArg::Const(self.lower_const_arg(ct, itctx)),
1218+
}
1219+
}
1220+
1221+
#[instrument(level = "debug", skip(self), ret)]
1222+
fn lower_const_arg(
1223+
&mut self,
1224+
c: &AnonConst,
1225+
itctx: &ImplTraitContext,
1226+
) -> &'hir hir::ConstArg<'hir> {
1227+
match c.value.is_potential_trivial_const_arg() {
1228+
Some((path_id, param_path)) => self.lower_const_arg_param(path_id, param_path, itctx),
1229+
None => self.arena.alloc(ConstArg {
1230+
kind: hir::ConstArgKind::AnonConst(
1231+
self.lower_span(c.value.span),
1232+
self.lower_anon_const(c),
12531233
),
1254-
})),
1234+
}),
12551235
}
12561236
}
12571237

1238+
fn lower_const_arg_param(
1239+
&mut self,
1240+
path_id: NodeId,
1241+
param_path: &Path,
1242+
itctx: &ImplTraitContext,
1243+
) -> &'hir hir::ConstArg<'hir> {
1244+
self.arena.alloc(ConstArg {
1245+
kind: hir::ConstArgKind::Param(
1246+
self.lower_node_id(path_id),
1247+
self.lower_qpath(path_id, &None, param_path, ParamMode::ExplicitNamed, itctx),
1248+
),
1249+
})
1250+
}
1251+
12581252
#[instrument(level = "debug", skip(self))]
12591253
fn lower_ty(&mut self, t: &Ty, itctx: &ImplTraitContext) -> &'hir hir::Ty<'hir> {
12601254
self.arena.alloc(self.lower_ty_direct(t, itctx))

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,7 +3972,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
39723972
);
39733973

39743974
self.resolve_anon_const_manual(
3975-
constant.value.is_potential_trivial_const_arg(),
3975+
constant.value.is_potential_trivial_const_arg().is_some(),
39763976
anon_const_kind,
39773977
|this| this.resolve_expr(&constant.value, None),
39783978
)
@@ -4118,7 +4118,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
41184118
// that is how they will be later lowered to HIR.
41194119
if const_args.contains(&idx) {
41204120
self.resolve_anon_const_manual(
4121-
argument.is_potential_trivial_const_arg(),
4121+
argument.is_potential_trivial_const_arg().is_some(),
41224122
AnonConstKind::ConstArg(IsRepeatExpr::No),
41234123
|this| this.resolve_expr(argument, None),
41244124
);

0 commit comments

Comments
 (0)