Skip to content

Commit 56e30e1

Browse files
committed
Tweak transparent enums and unions diagnostic spans
1 parent 9606f6f commit 56e30e1

File tree

4 files changed

+83
-92
lines changed

4 files changed

+83
-92
lines changed

src/librustc_typeck/check/mod.rs

+45-23
Original file line numberDiff line numberDiff line change
@@ -1794,25 +1794,39 @@ fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, def_id: DefId) {
17941794
if !adt.repr.transparent() {
17951795
return;
17961796
}
1797+
let sp = tcx.sess.source_map().def_span(sp);
17971798

17981799
if adt.is_enum() {
17991800
if !tcx.features().transparent_enums {
1800-
emit_feature_err(&tcx.sess.parse_sess,
1801-
sym::transparent_enums,
1802-
sp,
1803-
GateIssue::Language,
1804-
"transparent enums are unstable");
1801+
emit_feature_err(
1802+
&tcx.sess.parse_sess,
1803+
sym::transparent_enums,
1804+
sp,
1805+
GateIssue::Language,
1806+
"transparent enums are unstable",
1807+
);
18051808
}
18061809
if adt.variants.len() != 1 {
18071810
let variant_spans: Vec<_> = adt.variants.iter().map(|variant| {
18081811
tcx.hir().span_if_local(variant.def_id).unwrap()
18091812
}).collect();
1810-
let mut err = struct_span_err!(tcx.sess, sp, E0731,
1811-
"transparent enum needs exactly one variant, but has {}",
1812-
adt.variants.len());
1813-
if !variant_spans.is_empty() {
1814-
err.span_note(variant_spans, &format!("the following variants exist on `{}`",
1815-
tcx.def_path_str(def_id)));
1813+
let msg = format!(
1814+
"needs exactly one variant, but has {}",
1815+
adt.variants.len(),
1816+
);
1817+
let mut err = struct_span_err!(tcx.sess, sp, E0731, "transparent enum {}", msg);
1818+
err.span_label(sp, &msg);
1819+
match &variant_spans[..] {
1820+
&[] => {},
1821+
&[ref start.., ref end] => {
1822+
for variant_span in start {
1823+
err.span_label(*variant_span, "");
1824+
}
1825+
err.span_label(*end, &format!(
1826+
"too many variants in `{}`",
1827+
tcx.def_path_str(def_id),
1828+
));
1829+
},
18161830
}
18171831
err.emit();
18181832
if adt.variants.is_empty() {
@@ -1847,23 +1861,31 @@ fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, def_id: DefId) {
18471861
if non_zst_count != 1 {
18481862
let field_spans: Vec<_> = non_zst_fields.map(|(span, _zst, _align1)| span).collect();
18491863

1850-
let mut err = struct_span_err!(tcx.sess, sp, E0690,
1851-
"{}transparent {} needs exactly one non-zero-sized field, but has {}",
1852-
if adt.is_enum() { "the variant of a " } else { "" },
1853-
adt.descr(),
1854-
non_zst_count);
1855-
if !field_spans.is_empty() {
1856-
err.span_note(field_spans,
1857-
&format!("the following non-zero-sized fields exist on `{}`:",
1858-
tcx.def_path_str(def_id)));
1864+
let msg = format!("needs exactly one non-zero-sized field, but has {}", non_zst_count);
1865+
let mut err = struct_span_err!(
1866+
tcx.sess,
1867+
sp,
1868+
E0690,
1869+
"{}transparent {} {}",
1870+
if adt.is_enum() { "the variant of a " } else { "" },
1871+
adt.descr(),
1872+
msg,
1873+
);
1874+
err.span_label(sp, &msg);
1875+
for sp in &field_spans {
1876+
err.span_label(*sp, "this field is non-zero-sized");
18591877
}
18601878
err.emit();
18611879
}
18621880
for (span, zst, align1) in field_infos {
18631881
if zst && !align1 {
1864-
span_err!(tcx.sess, span, E0691,
1865-
"zero-sized field in transparent {} has alignment larger than 1",
1866-
adt.descr());
1882+
struct_span_err!(
1883+
tcx.sess,
1884+
span,
1885+
E0691,
1886+
"zero-sized field in transparent {} has alignment larger than 1",
1887+
adt.descr(),
1888+
).span_label(span, "has alignment larger than 1").emit();
18671889
}
18681890
}
18691891
}

src/test/ui/feature-gates/feature-gate-transparent_enums.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
error[E0658]: transparent enums are unstable
22
--> $DIR/feature-gate-transparent_enums.rs:2:1
33
|
4-
LL | / enum OkButUnstableEnum {
5-
LL | | Foo((), String, ()),
6-
LL | | }
7-
| |_^
4+
LL | enum OkButUnstableEnum {
5+
| ^^^^^^^^^^^^^^^^^^^^^^
86
|
97
= note: for more information, see https://github.com/rust-lang/rust/issues/60405
108
= help: add #![feature(transparent_enums)] to the crate attributes to enable

src/test/ui/feature-gates/feature-gate-transparent_unions.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error[E0658]: transparent unions are unstable
22
--> $DIR/feature-gate-transparent_unions.rs:2:1
33
|
4-
LL | / union OkButUnstableUnion {
5-
LL | | field: u8,
6-
LL | | zst: (),
7-
LL | | }
8-
| |_^
4+
LL | union OkButUnstableUnion {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
96
|
107
= note: for more information, see https://github.com/rust-lang/rust/issues/60405
118
= help: add #![feature(transparent_unions)] to the crate attributes to enable

src/test/ui/repr/repr-transparent.stderr

+34-60
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,57 @@ error[E0690]: transparent struct needs exactly one non-zero-sized field, but has
22
--> $DIR/repr-transparent.rs:11:1
33
|
44
LL | struct NoFields;
5-
| ^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
66

77
error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 0
88
--> $DIR/repr-transparent.rs:14:1
99
|
1010
LL | struct ContainsOnlyZst(());
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
1212

1313
error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 0
1414
--> $DIR/repr-transparent.rs:17:1
1515
|
1616
LL | struct ContainsOnlyZstArray([bool; 0]);
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
1818

1919
error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 0
2020
--> $DIR/repr-transparent.rs:20:1
2121
|
2222
LL | struct ContainsMultipleZst(PhantomData<*const i32>, NoFields);
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
2424

2525
error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 2
2626
--> $DIR/repr-transparent.rs:24:1
2727
|
2828
LL | struct MultipleNonZst(u8, u8);
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30-
|
31-
note: the following non-zero-sized fields exist on `MultipleNonZst`:
32-
--> $DIR/repr-transparent.rs:24:23
33-
|
34-
LL | struct MultipleNonZst(u8, u8);
35-
| ^^ ^^
29+
| ^^^^^^^^^^^^^^^^^^^^^^--^^--^^
30+
| | | |
31+
| | | this field is non-zero-sized
32+
| | this field is non-zero-sized
33+
| needs exactly one non-zero-sized field, but has 2
3634

3735
error[E0690]: transparent struct needs exactly one non-zero-sized field, but has 2
3836
--> $DIR/repr-transparent.rs:30:1
3937
|
4038
LL | pub struct StructWithProjection(f32, <f32 as Mirror>::It);
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42-
|
43-
note: the following non-zero-sized fields exist on `StructWithProjection`:
44-
--> $DIR/repr-transparent.rs:30:33
45-
|
46-
LL | pub struct StructWithProjection(f32, <f32 as Mirror>::It);
47-
| ^^^ ^^^^^^^^^^^^^^^^^^^
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---^^-------------------^^
40+
| | | |
41+
| | | this field is non-zero-sized
42+
| | this field is non-zero-sized
43+
| needs exactly one non-zero-sized field, but has 2
4844

4945
error[E0691]: zero-sized field in transparent struct has alignment larger than 1
5046
--> $DIR/repr-transparent.rs:34:32
5147
|
5248
LL | struct NontrivialAlignZst(u32, [u16; 0]);
53-
| ^^^^^^^^
49+
| ^^^^^^^^ has alignment larger than 1
5450

5551
error[E0691]: zero-sized field in transparent struct has alignment larger than 1
5652
--> $DIR/repr-transparent.rs:40:24
5753
|
5854
LL | struct GenericAlign<T>(ZstAlign32<T>, u32);
59-
| ^^^^^^^^^^^^^
55+
| ^^^^^^^^^^^^^ has alignment larger than 1
6056

6157
error[E0084]: unsupported representation for zero-variant enum
6258
--> $DIR/repr-transparent.rs:42:1
@@ -70,71 +66,49 @@ error[E0731]: transparent enum needs exactly one variant, but has 0
7066
--> $DIR/repr-transparent.rs:43:1
7167
|
7268
LL | enum Void {}
73-
| ^^^^^^^^^^^^
69+
| ^^^^^^^^^ needs exactly one variant, but has 0
7470

7571
error[E0690]: the variant of a transparent enum needs exactly one non-zero-sized field, but has 0
7672
--> $DIR/repr-transparent.rs:47:1
7773
|
78-
LL | / enum FieldlessEnum {
79-
LL | | Foo,
80-
LL | | }
81-
| |_^
74+
LL | enum FieldlessEnum {
75+
| ^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
8276

8377
error[E0690]: the variant of a transparent enum needs exactly one non-zero-sized field, but has 2
8478
--> $DIR/repr-transparent.rs:52:1
8579
|
86-
LL | / enum TooManyFieldsEnum {
87-
LL | | Foo(u32, String),
88-
LL | | }
89-
| |_^
90-
|
91-
note: the following non-zero-sized fields exist on `TooManyFieldsEnum`:
92-
--> $DIR/repr-transparent.rs:53:9
93-
|
80+
LL | enum TooManyFieldsEnum {
81+
| ^^^^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 2
9482
LL | Foo(u32, String),
95-
| ^^^ ^^^^^^
83+
| --- ------ this field is non-zero-sized
84+
| |
85+
| this field is non-zero-sized
9686

9787
error[E0731]: transparent enum needs exactly one variant, but has 2
9888
--> $DIR/repr-transparent.rs:58:1
9989
|
100-
LL | / enum TooManyVariants {
101-
LL | | Foo(String),
102-
LL | | Bar,
103-
LL | | }
104-
| |_^
105-
|
106-
note: the following variants exist on `TooManyVariants`
107-
--> $DIR/repr-transparent.rs:59:5
108-
|
90+
LL | enum TooManyVariants {
91+
| ^^^^^^^^^^^^^^^^^^^^ needs exactly one variant, but has 2
10992
LL | Foo(String),
110-
| ^^^^^^^^^^^
93+
| -----------
11194
LL | Bar,
112-
| ^^^
95+
| --- too many variants in `TooManyVariants`
11396

11497
error[E0690]: transparent union needs exactly one non-zero-sized field, but has 0
11598
--> $DIR/repr-transparent.rs:64:1
11699
|
117-
LL | / union UnitUnion {
118-
LL | | u: (),
119-
LL | | }
120-
| |_^
100+
LL | union UnitUnion {
101+
| ^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 0
121102

122103
error[E0690]: transparent union needs exactly one non-zero-sized field, but has 2
123104
--> $DIR/repr-transparent.rs:69:1
124105
|
125-
LL | / union TooManyFields {
126-
LL | | u: u32,
127-
LL | | s: i32
128-
LL | | }
129-
| |_^
130-
|
131-
note: the following non-zero-sized fields exist on `TooManyFields`:
132-
--> $DIR/repr-transparent.rs:70:5
133-
|
106+
LL | union TooManyFields {
107+
| ^^^^^^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 2
134108
LL | u: u32,
135-
| ^^^^^^
109+
| ------ this field is non-zero-sized
136110
LL | s: i32
137-
| ^^^^^^
111+
| ------ this field is non-zero-sized
138112

139113
error: aborting due to 15 previous errors
140114

0 commit comments

Comments
 (0)