Skip to content

Commit 96580b7

Browse files
committed
Introduce hir::ConstArgKind::Struct
1 parent 34d5e7d commit 96580b7

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
@@ -2394,6 +2394,37 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23942394

23952395
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
23962396
}
2397+
ExprKind::Struct(se) => {
2398+
let path = self.lower_qpath(
2399+
expr.id,
2400+
&se.qself,
2401+
&se.path,
2402+
ParamMode::Explicit,
2403+
AllowReturnTypeNotation::No,
2404+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
2405+
None,
2406+
);
2407+
2408+
let fields = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
2409+
let hir_id = self.lower_node_id(f.id);
2410+
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
2411+
2412+
let expr = if let ExprKind::ConstBlock(anon_const) = &f.expr.kind {
2413+
self.lower_anon_const_to_const_arg_direct(anon_const)
2414+
} else {
2415+
self.lower_expr_to_const_arg_direct(&f.expr)
2416+
};
2417+
2418+
&*self.arena.alloc(hir::ConstArgExprField {
2419+
hir_id,
2420+
field: self.lower_ident(f.ident),
2421+
expr: self.arena.alloc(expr),
2422+
span: self.lower_span(f.span),
2423+
})
2424+
}));
2425+
2426+
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Struct(path, fields) }
2427+
}
23972428
ExprKind::Underscore => ConstArg {
23982429
hir_id: self.lower_node_id(expr.id),
23992430
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)]
@@ -4711,6 +4722,7 @@ pub enum Node<'hir> {
47114722
ConstArg(&'hir ConstArg<'hir>),
47124723
Expr(&'hir Expr<'hir>),
47134724
ExprField(&'hir ExprField<'hir>),
4725+
ConstArgExprField(&'hir ConstArgExprField<'hir>),
47144726
Stmt(&'hir Stmt<'hir>),
47154727
PathSegment(&'hir PathSegment<'hir>),
47164728
Ty(&'hir Ty<'hir>),
@@ -4770,6 +4782,7 @@ impl<'hir> Node<'hir> {
47704782
Node::AssocItemConstraint(c) => Some(c.ident),
47714783
Node::PatField(f) => Some(f.ident),
47724784
Node::ExprField(f) => Some(f.ident),
4785+
Node::ConstArgExprField(f) => Some(f.field),
47734786
Node::PreciseCapturingNonLifetimeArg(a) => Some(a.ident),
47744787
Node::Param(..)
47754788
| 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> {
@@ -1067,7 +1081,17 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
10671081
) -> V::Result {
10681082
let ConstArg { hir_id, kind } = const_arg;
10691083
try_visit!(visitor.visit_id(*hir_id));
1084+
10701085
match kind {
1086+
ConstArgKind::Struct(qpath, field_exprs) => {
1087+
try_visit!(visitor.visit_qpath(qpath, *hir_id, qpath.span()));
1088+
1089+
for field_expr in *field_exprs {
1090+
try_visit!(visitor.visit_const_arg_expr_field(field_expr));
1091+
}
1092+
1093+
V::Result::output()
1094+
}
10711095
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
10721096
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
10731097
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),
@@ -1135,6 +1137,8 @@ impl<'a> State<'a> {
11351137

11361138
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11371139
match &const_arg.kind {
1140+
// FIXME(mgca): proper printing for struct exprs
1141+
ConstArgKind::Struct(..) => self.word("/* STRUCT EXPR */"),
11381142
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11391143
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
11401144
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14431443
hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
14441444
// Skip encoding defs for these as they should not have had a `DefId` created
14451445
hir::ConstArgKind::Error(..)
1446+
| hir::ConstArgKind::Struct(..)
14461447
| hir::ConstArgKind::Path(..)
14471448
| hir::ConstArgKind::Infer(..) => true,
14481449
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
@@ -739,6 +739,7 @@ impl<'tcx> TyCtxt<'tcx> {
739739
Node::ConstArg(_) => node_str("const"),
740740
Node::Expr(_) => node_str("expr"),
741741
Node::ExprField(_) => node_str("expr field"),
742+
Node::ConstArgExprField(_) => node_str("const arg expr field"),
742743
Node::Stmt(_) => node_str("stmt"),
743744
Node::PathSegment(_) => node_str("path segment"),
744745
Node::Ty(_) => node_str("type"),
@@ -1007,6 +1008,7 @@ impl<'tcx> TyCtxt<'tcx> {
10071008
Node::ConstArg(const_arg) => const_arg.span(),
10081009
Node::Expr(expr) => expr.span,
10091010
Node::ExprField(field) => field.span,
1011+
Node::ConstArgExprField(field) => field.span,
10101012
Node::Stmt(stmt) => stmt.span,
10111013
Node::PathSegment(seg) => {
10121014
let ident_span = seg.ident.span;

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ impl<'tcx> TyCtxt<'tcx> {
315315
Node::Block(_)
316316
| Node::Arm(_)
317317
| Node::ExprField(_)
318+
| Node::ConstArgExprField(_)
318319
| Node::AnonConst(_)
319320
| Node::ConstBlock(_)
320321
| Node::ConstArg(_)

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)