Skip to content

Commit e2e2957

Browse files
committed
Introduce hir::ConstArgKind::Struct
1 parent 9484663 commit e2e2957

File tree

11 files changed

+149
-7
lines changed

11 files changed

+149
-7
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
281281
});
282282
}
283283

284+
fn visit_const_arg_expr_field(&mut self, field: &'hir ConstArgExprField<'hir>) {
285+
self.insert(field.span, field.hir_id, Node::ConstArgExprField(field));
286+
self.with_parent(field.hir_id, |this| {
287+
intravisit::walk_const_arg_expr_field(this, field);
288+
})
289+
}
290+
284291
fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
285292
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
286293

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,37 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23832383

23842384
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
23852385
}
2386+
ExprKind::Struct(se) => {
2387+
let path = self.lower_qpath(
2388+
expr.id,
2389+
&se.qself,
2390+
&se.path,
2391+
ParamMode::Explicit,
2392+
AllowReturnTypeNotation::No,
2393+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2394+
None,
2395+
);
2396+
2397+
let fields = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
2398+
let hir_id = self.lower_node_id(f.id);
2399+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
2400+
2401+
let expr = if let ExprKind::ConstBlock(anon_const) = &f.expr.kind {
2402+
self.lower_anon_const_to_const_arg_direct(anon_const)
2403+
} else {
2404+
self.lower_expr_to_const_arg_direct(&f.expr)
2405+
};
2406+
2407+
&*self.arena.alloc(hir::ConstArgExprField {
2408+
hir_id,
2409+
field: self.lower_ident(f.ident),
2410+
expr: self.arena.alloc(expr),
2411+
span: self.lower_span(f.span),
2412+
})
2413+
}));
2414+
2415+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Struct(path, fields) }
2416+
}
23862417
ExprKind::Underscore => ConstArg {
23872418
hir_id: self.lower_node_id(expr.id),
23882419
kind: hir::ConstArgKind::Infer(expr.span, ()),

compiler/rustc_hir/src/hir.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ impl<'hir, Unambig> ConstArg<'hir, Unambig> {
494494

495495
pub fn span(&self) -> Span {
496496
match self.kind {
497+
ConstArgKind::Struct(path, _) => path.span(),
497498
ConstArgKind::Path(path) => path.span(),
498499
ConstArgKind::Anon(anon) => anon.span,
499500
ConstArgKind::Error(span, _) => span,
@@ -513,13 +514,23 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
513514
/// However, in the future, we'll be using it for all of those.
514515
Path(QPath<'hir>),
515516
Anon(&'hir AnonConst),
517+
/// Represents construction of struct/struct variants
518+
Struct(QPath<'hir>, &'hir [&'hir ConstArgExprField<'hir>]),
516519
/// Error const
517520
Error(Span, ErrorGuaranteed),
518521
/// This variant is not always used to represent inference consts, sometimes
519522
/// [`GenericArg::Infer`] is used instead.
520523
Infer(Span, Unambig),
521524
}
522525

526+
#[derive(Clone, Copy, Debug, HashStable_Generic)]
527+
pub struct ConstArgExprField<'hir> {
528+
pub hir_id: HirId,
529+
pub span: Span,
530+
pub field: Ident,
531+
pub expr: &'hir ConstArg<'hir>,
532+
}
533+
523534
#[derive(Clone, Copy, Debug, HashStable_Generic)]
524535
pub struct InferArg {
525536
#[stable_hasher(ignore)]
@@ -4646,6 +4657,7 @@ pub enum Node<'hir> {
46464657
ConstArg(&'hir ConstArg<'hir>),
46474658
Expr(&'hir Expr<'hir>),
46484659
ExprField(&'hir ExprField<'hir>),
4660+
ConstArgExprField(&'hir ConstArgExprField<'hir>),
46494661
Stmt(&'hir Stmt<'hir>),
46504662
PathSegment(&'hir PathSegment<'hir>),
46514663
Ty(&'hir Ty<'hir>),
@@ -4705,6 +4717,7 @@ impl<'hir> Node<'hir> {
47054717
Node::AssocItemConstraint(c) => Some(c.ident),
47064718
Node::PatField(f) => Some(f.ident),
47074719
Node::ExprField(f) => Some(f.ident),
4720+
Node::ConstArgExprField(f) => Some(f.field),
47084721
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
47094722
Node::Param(..)
47104723
| Node::AnonConst(..)

compiler/rustc_hir/src/intravisit.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ pub trait Visitor<'v>: Sized {
396396
fn visit_expr_field(&mut self, field: &'v ExprField<'v>) -> Self::Result {
397397
walk_expr_field(self, field)
398398
}
399+
fn visit_const_arg_expr_field(&mut self, field: &'v ConstArgExprField<'v>) -> Self::Result {
400+
walk_const_arg_expr_field(self, field)
401+
}
399402
fn visit_pattern_type_pattern(&mut self, p: &'v TyPat<'v>) -> Self::Result {
400403
walk_ty_pat(self, p)
401404
}
@@ -954,6 +957,17 @@ pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField
954957
try_visit!(visitor.visit_ident(*ident));
955958
visitor.visit_expr(*expr)
956959
}
960+
961+
pub fn walk_const_arg_expr_field<'v, V: Visitor<'v>>(
962+
visitor: &mut V,
963+
field: &'v ConstArgExprField<'v>,
964+
) -> V::Result {
965+
let ConstArgExprField { hir_id, field, expr, span: _ } = field;
966+
try_visit!(visitor.visit_id(*hir_id));
967+
try_visit!(visitor.visit_ident(*field));
968+
visitor.visit_const_arg_unambig(*expr)
969+
}
970+
957971
/// We track whether an infer var is from a [`Ty`], [`ConstArg`], or [`GenericArg`] so that
958972
/// HIR visitors overriding [`Visitor::visit_infer`] can determine what kind of infer is being visited
959973
pub enum InferKind<'hir> {
@@ -1068,7 +1082,17 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
10681082
) -> V::Result {
10691083
let ConstArg { hir_id, kind } = const_arg;
10701084
try_visit!(visitor.visit_id(*hir_id));
1085+
10711086
match kind {
1087+
ConstArgKind::Struct(qpath, field_exprs) => {
1088+
try_visit!(visitor.visit_qpath(qpath, *hir_id, qpath.span()));
1089+
1090+
for field_expr in *field_exprs {
1091+
try_visit!(visitor.visit_const_arg_expr_field(field_expr));
1092+
}
1093+
1094+
V::Result::output()
1095+
}
10721096
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
10731097
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
10741098
ConstArgKind::Error(_, _) => V::Result::output(), // errors and spans are not important

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,6 +2263,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22632263
)
22642264
.unwrap_or_else(|guar| Const::new_error(tcx, guar))
22652265
}
2266+
hir::ConstArgKind::Struct(..) => {
2267+
span_bug!(const_arg.span(), "lowering `{:?}` is not yet implemented", const_arg)
2268+
}
22662269
hir::ConstArgKind::Anon(anon) => self.lower_anon_const(anon),
22672270
hir::ConstArgKind::Infer(span, ()) => self.ct_infer(None, span),
22682271
hir::ConstArgKind::Error(_, e) => ty::Const::new_error(tcx, e),

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ impl<'a> State<'a> {
180180
Node::ConstArg(a) => self.print_const_arg(a),
181181
Node::Expr(a) => self.print_expr(a),
182182
Node::ExprField(a) => self.print_expr_field(a),
183+
// FIXME(mgca): proper printing for struct exprs
184+
Node::ConstArgExprField(_) => self.word("/* STRUCT EXPR */"),
183185
Node::Stmt(a) => self.print_stmt(a),
184186
Node::PathSegment(a) => self.print_path_segment(a),
185187
Node::Ty(a) => self.print_type(a),
@@ -1140,6 +1142,8 @@ impl<'a> State<'a> {
11401142

11411143
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11421144
match &const_arg.kind {
1145+
// FIXME(mgca): proper printing for struct exprs
1146+
ConstArgKind::Struct(..) => self.word("/* STRUCT EXPR */"),
11431147
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11441148
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
11451149
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
439439
hir::Node::Block(_)
440440
| hir::Node::Arm(_)
441441
| hir::Node::ExprField(_)
442+
| hir::Node::ConstArgExprField(_)
442443
| hir::Node::AnonConst(_)
443444
| hir::Node::ConstBlock(_)
444445
| hir::Node::ConstArg(_)

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14491449
hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
14501450
// Skip encoding defs for these as they should not have had a `DefId` created
14511451
hir::ConstArgKind::Error(..)
1452+
| hir::ConstArgKind::Struct(..)
14521453
| hir::ConstArgKind::Path(..)
14531454
| hir::ConstArgKind::Infer(..) => true,
14541455
hir::ConstArgKind::Anon(..) => false,

compiler/rustc_middle/src/hir/map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ impl<'tcx> TyCtxt<'tcx> {
740740
Node::ConstArg(_) => node_str("const"),
741741
Node::Expr(_) => node_str("expr"),
742742
Node::ExprField(_) => node_str("expr field"),
743+
Node::ConstArgExprField(_) => node_str("const arg expr field"),
743744
Node::Stmt(_) => node_str("stmt"),
744745
Node::PathSegment(_) => node_str("path segment"),
745746
Node::Ty(_) => node_str("type"),
@@ -1008,6 +1009,7 @@ impl<'tcx> TyCtxt<'tcx> {
10081009
Node::ConstArg(const_arg) => const_arg.span(),
10091010
Node::Expr(expr) => expr.span,
10101011
Node::ExprField(field) => field.span,
1012+
Node::ConstArgExprField(field) => field.span,
10111013
Node::Stmt(stmt) => stmt.span,
10121014
Node::PathSegment(seg) => {
10131015
let ident_span = seg.ident.span;

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
7474
self.invocation_parent.impl_trait_context = orig_itc;
7575
}
7676

77+
fn with_direct_const_arg<F: FnOnce(&mut Self)>(&mut self, is_direct: bool, f: F) {
78+
let orig = mem::replace(&mut self.invocation_parent.in_direct_const_arg, is_direct);
79+
f(self);
80+
self.invocation_parent.in_direct_const_arg = orig;
81+
}
82+
7783
fn collect_field(&mut self, field: &'a FieldDef, index: Option<usize>) {
7884
let index = |this: &Self| {
7985
index.unwrap_or_else(|| {
@@ -357,25 +363,73 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
357363
}
358364

359365
fn visit_anon_const(&mut self, constant: &'a AnonConst) {
360-
let parent = self.create_def(constant.id, None, DefKind::AnonConst, constant.value.span);
361-
self.with_parent(parent, |this| visit::walk_anon_const(this, constant));
366+
if let MgcaDisambiguation::Direct = constant.mgca_disambiguation
367+
&& self.resolver.tcx.features().min_generic_const_args()
368+
{
369+
self.with_direct_const_arg(true, |this| {
370+
visit::walk_anon_const(this, constant);
371+
});
372+
} else {
373+
self.with_direct_const_arg(false, |this| {
374+
let parent =
375+
this.create_def(constant.id, None, DefKind::AnonConst, constant.value.span);
376+
this.with_parent(parent, |this| visit::walk_anon_const(this, constant));
377+
})
378+
}
362379
}
363380

364381
fn visit_expr(&mut self, expr: &'a Expr) {
382+
let handle_const_block = |this: &mut Self, constant: &'a AnonConst, def_kind: DefKind| {
383+
for attr in &expr.attrs {
384+
visit::walk_attribute(this, attr);
385+
}
386+
387+
let def = this.create_def(constant.id, None, def_kind, constant.value.span);
388+
this.with_direct_const_arg(false, |this| {
389+
this.with_parent(def, |this| visit::walk_anon_const(this, constant));
390+
});
391+
};
392+
365393
let parent_def = match expr.kind {
366394
ExprKind::MacCall(..) => return self.visit_macro_invoc(expr.id),
367395
ExprKind::Closure(..) | ExprKind::Gen(..) => {
368396
self.create_def(expr.id, None, DefKind::Closure, expr.span)
369397
}
370398
ExprKind::ConstBlock(ref constant) => {
371-
for attr in &expr.attrs {
372-
visit::walk_attribute(self, attr);
399+
handle_const_block(self, constant, DefKind::InlineConst);
400+
return;
401+
}
402+
ExprKind::Struct(ref se) if self.invocation_parent.in_direct_const_arg => {
403+
let StructExpr { qself, path, fields, rest } = &**se;
404+
405+
for init_expr in fields {
406+
if let ExprKind::ConstBlock(ref constant) = init_expr.expr.kind {
407+
handle_const_block(self, constant, DefKind::AnonConst);
408+
} else {
409+
visit::walk_expr_field(self, init_expr);
410+
}
411+
}
412+
413+
if let Some(qself) = qself {
414+
self.visit_qself(qself);
373415
}
374-
let def =
375-
self.create_def(constant.id, None, DefKind::InlineConst, constant.value.span);
376-
self.with_parent(def, |this| visit::walk_anon_const(this, constant));
416+
self.visit_path(path);
417+
418+
match rest {
419+
StructRest::Base(expr) => self.visit_expr(expr),
420+
_ => (),
421+
}
422+
377423
return;
378424
}
425+
ExprKind::Field(ref init_expr, _) if self.invocation_parent.in_direct_const_arg => {
426+
if let ExprKind::ConstBlock(ref constant) = init_expr.kind {
427+
handle_const_block(self, constant, DefKind::AnonConst);
428+
return;
429+
} else {
430+
self.invocation_parent.parent_def
431+
}
432+
}
379433
_ => self.invocation_parent.parent_def,
380434
};
381435

0 commit comments

Comments
 (0)