Skip to content

Commit 7d7be2f

Browse files
committed
Setup ast_lowering functions for ConstArg
1 parent 11b144a commit 7d7be2f

File tree

4 files changed

+69
-55
lines changed

4 files changed

+69
-55
lines changed

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
187187
.emit();
188188
}
189189
hir::InlineAsmOperand::Const {
190-
anon_const: self.lower_anon_const(anon_const),
190+
anon_const: self.lower_anon_const_to_anon_const(anon_const),
191191
}
192192
}
193193
InlineAsmOperand::Sym { sym } => {
@@ -233,7 +233,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
233233
);
234234
let anon_const = AnonConst { id: node_id, value: P(expr) };
235235
hir::InlineAsmOperand::SymFn {
236-
anon_const: self.lower_anon_const(&anon_const),
236+
anon_const: self.lower_anon_const_to_anon_const(&anon_const),
237237
}
238238
}
239239
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
713713
hir_id,
714714
def_id: self.local_def_id(v.id),
715715
data: self.lower_variant_data(hir_id, &v.data),
716-
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const(e)),
716+
disr_expr: v.disr_expr.as_ref().map(|e| self.lower_anon_const_to_anon_const(e)),
717717
ident: self.lower_ident(v.ident),
718718
span: self.lower_span(v.span),
719719
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ use rustc_data_structures::sync::Lrc;
5353
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5454
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5555
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
56-
use rustc_hir::{self as hir, ConstArgKind};
57-
use rustc_hir::{
58-
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
59-
};
56+
use rustc_hir::{self as hir};
57+
use rustc_hir::{GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate};
6058
use rustc_index::{Idx, IndexSlice, IndexVec};
6159
use rustc_macros::extension;
6260
use rustc_middle::span_bug;
@@ -1064,7 +1062,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10641062
AssocItemConstraintKind::Equality { term } => {
10651063
let term = match term {
10661064
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
1067-
Term::Const(c) => self.lower_anon_const(c).into(),
1065+
Term::Const(c) => self.lower_anon_const_to_anon_const(c).into(),
10681066
};
10691067
hir::AssocItemConstraintKind::Equality { term }
10701068
}
@@ -1170,53 +1168,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11701168
ty,
11711169
);
11721170

1173-
// Construct an AnonConst where the expr is the "ty"'s path.
1174-
1175-
let parent_def_id = self.current_def_id_parent;
1176-
let node_id = self.next_node_id();
1177-
let span = self.lower_span(ty.span);
1178-
1179-
// Add a definition for the in-band const def.
1180-
let def_id = self.create_def(
1181-
parent_def_id,
1182-
node_id,
1183-
kw::Empty,
1184-
DefKind::AnonConst,
1185-
span,
1186-
);
1187-
1188-
let path_expr = Expr {
1189-
id: ty.id,
1190-
kind: ExprKind::Path(None, path.clone()),
1191-
span,
1192-
attrs: AttrVec::new(),
1193-
tokens: None,
1194-
};
1195-
1196-
let ct = self.with_new_scopes(span, |this| {
1197-
self.arena.alloc(hir::AnonConst {
1198-
def_id,
1199-
hir_id: this.lower_node_id(node_id),
1200-
body: this
1201-
.lower_const_body(path_expr.span, Some(&path_expr)),
1202-
span,
1203-
})
1204-
});
1205-
return GenericArg::Const(ConstArg {
1206-
kind: ConstArgKind::Anon(ct),
1207-
is_desugared_from_effects: false,
1208-
});
1171+
let ct =
1172+
self.lower_const_path_to_const_arg(path, res, ty.id, ty.span);
1173+
return GenericArg::Const(ct);
12091174
}
12101175
}
12111176
}
12121177
_ => {}
12131178
}
12141179
GenericArg::Type(self.lower_ty(ty, itctx))
12151180
}
1216-
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
1217-
kind: ConstArgKind::Anon(self.lower_anon_const(ct)),
1218-
is_desugared_from_effects: false,
1219-
}),
1181+
ast::GenericArg::Const(ct) => GenericArg::Const(self.lower_anon_const_to_const_arg(ct)),
12201182
}
12211183
}
12221184

@@ -1375,7 +1337,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13751337
TyKind::Array(ty, length) => {
13761338
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_array_length(length))
13771339
}
1378-
TyKind::Typeof(expr) => hir::TyKind::Typeof(self.lower_anon_const(expr)),
1340+
TyKind::Typeof(expr) => hir::TyKind::Typeof(self.lower_anon_const_to_anon_const(expr)),
13791341
TyKind::TraitObject(bounds, kind) => {
13801342
let mut lifetime_bound = None;
13811343
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
@@ -2242,7 +2204,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22422204
false
22432205
}
22442206
})
2245-
.map(|def| self.lower_anon_const(def));
2207+
.map(|def| self.lower_anon_const_to_anon_const(def));
22462208

22472209
(
22482210
hir::ParamName::Plain(self.lower_ident(param.ident)),
@@ -2380,14 +2342,66 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23802342
"using `_` for array lengths is unstable",
23812343
)
23822344
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
2383-
hir::ArrayLen::Body(self.lower_anon_const(c))
2345+
hir::ArrayLen::Body(self.lower_anon_const_to_anon_const(c))
23842346
}
23852347
}
2386-
_ => hir::ArrayLen::Body(self.lower_anon_const(c)),
2348+
_ => hir::ArrayLen::Body(self.lower_anon_const_to_anon_const(c)),
2349+
}
2350+
}
2351+
2352+
fn lower_const_path_to_const_arg(
2353+
&mut self,
2354+
path: &Path,
2355+
_res: Res<NodeId>,
2356+
ty_id: NodeId,
2357+
span: Span,
2358+
) -> &'hir hir::ConstArg<'hir> {
2359+
// Construct an AnonConst where the expr is the "ty"'s path.
2360+
2361+
let parent_def_id = self.current_def_id_parent;
2362+
let node_id = self.next_node_id();
2363+
let span = self.lower_span(span);
2364+
2365+
// Add a definition for the in-band const def.
2366+
let def_id = self.create_def(parent_def_id, node_id, kw::Empty, DefKind::AnonConst, span);
2367+
2368+
let path_expr = Expr {
2369+
id: ty_id,
2370+
kind: ExprKind::Path(None, path.clone()),
2371+
span,
2372+
attrs: AttrVec::new(),
2373+
tokens: None,
2374+
};
2375+
2376+
let ct = self.with_new_scopes(span, |this| {
2377+
self.arena.alloc(hir::AnonConst {
2378+
def_id,
2379+
hir_id: this.lower_node_id(node_id),
2380+
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
2381+
span,
2382+
})
2383+
});
2384+
2385+
self.arena.alloc(hir::ConstArg {
2386+
kind: hir::ConstArgKind::Anon(ct),
2387+
is_desugared_from_effects: false,
2388+
})
2389+
}
2390+
2391+
fn lower_anon_const_to_const_arg(&mut self, anon: &AnonConst) -> &'hir hir::ConstArg<'hir> {
2392+
self.arena.alloc(self.lower_anon_const_to_const_arg_direct(anon))
2393+
}
2394+
2395+
#[instrument(level = "debug", skip(self))]
2396+
fn lower_anon_const_to_const_arg_direct(&mut self, anon: &AnonConst) -> hir::ConstArg<'hir> {
2397+
let lowered_anon = self.lower_anon_const_to_anon_const(anon);
2398+
hir::ConstArg {
2399+
kind: hir::ConstArgKind::Anon(lowered_anon),
2400+
is_desugared_from_effects: false,
23872401
}
23882402
}
23892403

2390-
fn lower_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
2404+
fn lower_anon_const_to_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
23912405
self.arena.alloc(self.with_new_scopes(c.value.span, |this| hir::AnonConst {
23922406
def_id: this.local_def_id(c.id),
23932407
hir_id: this.lower_node_id(c.id),

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl InferArg {
270270
pub enum GenericArg<'hir> {
271271
Lifetime(&'hir Lifetime),
272272
Type(&'hir Ty<'hir>),
273-
Const(ConstArg<'hir>),
273+
Const(&'hir ConstArg<'hir>),
274274
Infer(InferArg),
275275
}
276276

@@ -3985,7 +3985,7 @@ mod size_asserts {
39853985
static_assert_size!(FnDecl<'_>, 40);
39863986
static_assert_size!(ForeignItem<'_>, 72);
39873987
static_assert_size!(ForeignItemKind<'_>, 40);
3988-
static_assert_size!(GenericArg<'_>, 24);
3988+
static_assert_size!(GenericArg<'_>, 16);
39893989
static_assert_size!(GenericBound<'_>, 48);
39903990
static_assert_size!(Generics<'_>, 56);
39913991
static_assert_size!(Impl<'_>, 80);

0 commit comments

Comments
 (0)