Skip to content

Commit dd0ba66

Browse files
committed
mgca: Use span of entire struct expr const arg, not just path
Ideally in field WF checking, we'd point to the specific field, but at least now we point to the whole expression instead of just the struct path. Also, in general, we should use the span of the whole expression for clarity and consistency's sake.
1 parent 8c4e145 commit dd0ba66

9 files changed

Lines changed: 26 additions & 18 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2440,7 +2440,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24402440
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Path(qpath) }
24412441
}
24422442
ExprKind::Struct(se) => {
2443-
let path = self.lower_qpath(
2443+
let qpath = self.lower_qpath(
24442444
expr.id,
24452445
&se.qself,
24462446
&se.path,
@@ -2478,7 +2478,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24782478
})
24792479
}));
24802480

2481-
ConstArg { hir_id: self.next_id(), kind: hir::ConstArgKind::Struct(path, fields) }
2481+
let span = expr.span;
2482+
ConstArg {
2483+
hir_id: self.next_id(),
2484+
kind: hir::ConstArgKind::Struct { qpath, fields, span },
2485+
}
24822486
}
24832487
ExprKind::Underscore => ConstArg {
24842488
hir_id: self.lower_node_id(expr.id),

compiler/rustc_hir/src/hir.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'hir, Unambig> ConstArg<'hir, Unambig> {
497497

498498
pub fn span(&self) -> Span {
499499
match self.kind {
500-
ConstArgKind::Struct(path, _) => path.span(),
500+
ConstArgKind::Struct { span, .. } => span,
501501
ConstArgKind::Path(path) => path.span(),
502502
ConstArgKind::TupleCall(path, _) => path.span(),
503503
ConstArgKind::Anon(anon) => anon.span,
@@ -519,7 +519,11 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
519519
Path(QPath<'hir>),
520520
Anon(&'hir AnonConst),
521521
/// Represents construction of struct/struct variants
522-
Struct(QPath<'hir>, &'hir [&'hir ConstArgExprField<'hir>]),
522+
Struct {
523+
qpath: QPath<'hir>,
524+
fields: &'hir [&'hir ConstArgExprField<'hir>],
525+
span: Span,
526+
},
523527
/// Tuple constructor variant
524528
TupleCall(QPath<'hir>, &'hir [&'hir ConstArg<'hir>]),
525529
/// Error const

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,10 +1081,10 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
10811081
let ConstArg { hir_id, kind } = const_arg;
10821082
try_visit!(visitor.visit_id(*hir_id));
10831083
match kind {
1084-
ConstArgKind::Struct(qpath, field_exprs) => {
1084+
ConstArgKind::Struct { qpath, fields, span: _ } => {
10851085
try_visit!(visitor.visit_qpath(qpath, *hir_id, qpath.span()));
10861086

1087-
for field_expr in *field_exprs {
1087+
for field_expr in *fields {
10881088
try_visit!(visitor.visit_const_arg_expr_field(field_expr));
10891089
}
10901090

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,8 +2371,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23712371
)
23722372
.unwrap_or_else(|guar| Const::new_error(tcx, guar))
23732373
}
2374-
hir::ConstArgKind::Struct(qpath, inits) => {
2375-
self.lower_const_arg_struct(hir_id, qpath, inits, const_arg.span())
2374+
hir::ConstArgKind::Struct { qpath, fields, span } => {
2375+
self.lower_const_arg_struct(hir_id, qpath, fields, span)
23762376
}
23772377
hir::ConstArgKind::TupleCall(qpath, args) => {
23782378
self.lower_const_arg_tuple_call(hir_id, qpath, args, const_arg.span())

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ impl<'a> State<'a> {
11381138
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11391139
match &const_arg.kind {
11401140
// FIXME(mgca): proper printing for struct exprs
1141-
ConstArgKind::Struct(..) => self.word("/* STRUCT EXPR */"),
1141+
ConstArgKind::Struct { .. } => self.word("/* STRUCT EXPR */"),
11421142
ConstArgKind::TupleCall(..) => self.word("/* TUPLE CALL */"),
11431143
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11441144
ConstArgKind::Anon(anon) => self.print_anon_const(anon),

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14401440
hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
14411441
// Skip encoding defs for these as they should not have had a `DefId` created
14421442
hir::ConstArgKind::Error(..)
1443-
| hir::ConstArgKind::Struct(..)
1443+
| hir::ConstArgKind::Struct { .. }
14441444
| hir::ConstArgKind::TupleCall(..)
14451445
| hir::ConstArgKind::Path(..)
14461446
| hir::ConstArgKind::Infer(..) => true,

tests/ui/const-generics/mgca/adt_expr_erroneuous_inits.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ error: struct expression with missing field initialiser for `field`
2323
--> $DIR/adt_expr_erroneuous_inits.rs:16:17
2424
|
2525
LL | accepts::<{ Foo::<u8> { }}>();
26-
| ^^^^^^^^^
26+
| ^^^^^^^^^^^^^
2727

2828
error: struct expression with multiple initialisers for `field`
2929
--> $DIR/adt_expr_erroneuous_inits.rs:18:49
@@ -35,13 +35,13 @@ error: struct expression with invalid base path
3535
--> $DIR/adt_expr_erroneuous_inits.rs:20:17
3636
|
3737
LL | accepts::<{ Fooo::<u8> { field: const { 1 } }}>();
38-
| ^^^^^^^^^^
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939

4040
error: struct expression with invalid base path
4141
--> $DIR/adt_expr_erroneuous_inits.rs:23:17
4242
|
4343
LL | accepts::<{ NonStruct { }}>();
44-
| ^^^^^^^^^
44+
| ^^^^^^^^^^^^^
4545

4646
error: aborting due to 6 previous errors
4747

tests/ui/const-generics/mgca/adt_expr_fields_type_check.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: the constant `N` is not of type `u8`
22
--> $DIR/adt_expr_fields_type_check.rs:11:17
33
|
44
LL | accepts::<{ Foo::<u8> { field: N }}>();
5-
| ^^^^^^^^^ expected `u8`, found `bool`
5+
| ^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `bool`
66

77
error: aborting due to 1 previous error
88

tests/ui/const-generics/mgca/printing_valtrees_supports_non_values.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: the constant `Option::<u32>::Some(N)` is not of type `Foo`
22
--> $DIR/printing_valtrees_supports_non_values.rs:18:13
33
|
44
LL | foo::<{ Option::Some::<u32> { 0: N } }>;
5-
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
66
|
77
note: required by a const generic parameter in `foo`
88
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
@@ -14,7 +14,7 @@ error: the constant `Option::<u32>::Some(<T as Trait>::ASSOC)` is not of type `F
1414
--> $DIR/printing_valtrees_supports_non_values.rs:23:13
1515
|
1616
LL | foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
17-
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
1818
|
1919
note: required by a const generic parameter in `foo`
2020
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
@@ -37,7 +37,7 @@ error: the constant `Option::<u32>::Some(_)` is not of type `Foo`
3737
--> $DIR/printing_valtrees_supports_non_values.rs:30:13
3838
|
3939
LL | foo::<{ Option::Some::<u32> { 0: <T as Trait>::ASSOC } }>();
40-
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
4141
|
4242
note: required by a const generic parameter in `foo`
4343
--> $DIR/printing_valtrees_supports_non_values.rs:15:8
@@ -49,7 +49,7 @@ error: the constant `Option::<u32>::Some(_)` is not of type `Foo`
4949
--> $DIR/printing_valtrees_supports_non_values.rs:36:13
5050
|
5151
LL | foo::<{ Option::Some::<u32> { 0: _ } }>();
52-
| ^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Foo`, found `Option<u32>`
5353
|
5454
note: required by a const generic parameter in `foo`
5555
--> $DIR/printing_valtrees_supports_non_values.rs:15:8

0 commit comments

Comments
 (0)