Skip to content

Commit f631410

Browse files
Rollup merge of #81737 - camelid:typeck-structure-sugg, r=lcnr
typeck: Emit structured suggestions for tuple struct syntax And tuple variant syntax, but that didn't fit in the subject :) Now the fact that these are suggestions is exposed both to the layout engine and to IDEs and rustfix for automatic application.
2 parents 85fb5cd + ed62329 commit f631410

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

compiler/rustc_typeck/src/check/expr.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -1460,28 +1460,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14601460
),
14611461
);
14621462
err.span_label(field.ident.span, "field does not exist");
1463-
err.span_label(
1463+
err.span_suggestion(
14641464
ty_span,
1465+
&format!(
1466+
"`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
1467+
adt = ty,
1468+
variant = variant.ident,
1469+
),
14651470
format!(
1466-
"`{adt}::{variant}` is a tuple {kind_name}, \
1467-
use the appropriate syntax: `{adt}::{variant}(/* fields */)`",
1471+
"{adt}::{variant}(/* fields */)",
14681472
adt = ty,
14691473
variant = variant.ident,
1470-
kind_name = kind_name
14711474
),
1475+
Applicability::HasPlaceholders,
14721476
);
14731477
}
14741478
_ => {
14751479
err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty));
14761480
err.span_label(field.ident.span, "field does not exist");
1477-
err.span_label(
1481+
err.span_suggestion(
14781482
ty_span,
1479-
format!(
1480-
"`{adt}` is a tuple {kind_name}, \
1481-
use the appropriate syntax: `{adt}(/* fields */)`",
1483+
&format!(
1484+
"`{adt}` is a tuple {kind_name}, use the appropriate syntax",
14821485
adt = ty,
1483-
kind_name = kind_name
1486+
kind_name = kind_name,
14841487
),
1488+
format!("{adt}(/* fields */)", adt = ty),
1489+
Applicability::HasPlaceholders,
14851490
);
14861491
}
14871492
},

src/test/ui/issues/issue-4736.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | struct NonCopyable(());
77
LL | let z = NonCopyable{ p: () };
88
| ----------- ^ field does not exist
99
| |
10-
| `NonCopyable` is a tuple struct, use the appropriate syntax: `NonCopyable(/* fields */)`
10+
| help: `NonCopyable` is a tuple struct, use the appropriate syntax: `NonCopyable(/* fields */)`
1111

1212
error: aborting due to previous error
1313

src/test/ui/issues/issue-80607.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | V1(i32),
77
LL | Enum::V1 { x }
88
| -------- ^ field does not exist
99
| |
10-
| `Enum::V1` is a tuple variant, use the appropriate syntax: `Enum::V1(/* fields */)`
10+
| help: `Enum::V1` is a tuple variant, use the appropriate syntax: `Enum::V1(/* fields */)`
1111

1212
error: aborting due to previous error
1313

src/test/ui/numeric/numeric-fields.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | struct S(u8, u16);
77
LL | let s = S{0b1: 10, 0: 11};
88
| - ^^^ field does not exist
99
| |
10-
| `S` is a tuple struct, use the appropriate syntax: `S(/* fields */)`
10+
| help: `S` is a tuple struct, use the appropriate syntax: `S(/* fields */)`
1111

1212
error[E0026]: struct `S` does not have a field named `0x1`
1313
--> $DIR/numeric-fields.rs:7:17

0 commit comments

Comments
 (0)