Skip to content

Commit 00d54c8

Browse files
Label non_exhaustive on privacy errors
1 parent cf7ada2 commit 00d54c8

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16071607
let mut err =
16081608
struct_span_err!(self.tcx.sess, ident.span, E0603, "{} `{}` is private", descr, ident);
16091609
err.span_label(ident.span, &format!("private {}", descr));
1610-
if let Some(span) = ctor_fields_span {
1610+
1611+
if let Some(def_id) = res.opt_def_id()
1612+
&& !def_id.is_local()
1613+
&& let Some(attr) = self.tcx.get_attr(def_id, sym::non_exhaustive)
1614+
{
1615+
err.span_label(attr.span, format!("the {nonimport_descr} is `#[non_exhaustive]`"));
1616+
} else if let Some(span) = ctor_fields_span {
16111617
err.span_label(span, "a constructor is private if any of the fields is private");
16121618
if let Res::Def(_, d) = res && let Some(fields) = self.field_visibility_spans.get(&d) {
16131619
err.multipart_suggestion_verbose(

tests/ui/rfc-2008-non-exhaustive/struct.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ error[E0603]: tuple struct constructor `TupleStruct` is private
1010
LL | let ts_explicit = structs::TupleStruct(640, 480);
1111
| ^^^^^^^^^^^ private tuple struct constructor
1212
|
13-
::: $DIR/auxiliary/structs.rs:12:24
13+
::: $DIR/auxiliary/structs.rs:11:1
1414
|
15-
LL | pub struct TupleStruct(pub u16, pub u16);
16-
| ---------------- a constructor is private if any of the fields is private
15+
LL | #[non_exhaustive]
16+
| ----------------- the tuple struct constructor is `#[non_exhaustive]`
1717
|
1818
note: the tuple struct constructor `TupleStruct` is defined here
1919
--> $DIR/auxiliary/structs.rs:12:1
@@ -27,6 +27,11 @@ error[E0603]: unit struct `UnitStruct` is private
2727
LL | let us_explicit = structs::UnitStruct;
2828
| ^^^^^^^^^^ private unit struct
2929
|
30+
::: $DIR/auxiliary/structs.rs:8:1
31+
|
32+
LL | #[non_exhaustive]
33+
| ----------------- the unit struct is `#[non_exhaustive]`
34+
|
3035
note: the unit struct `UnitStruct` is defined here
3136
--> $DIR/auxiliary/structs.rs:9:1
3237
|

tests/ui/rfc-2008-non-exhaustive/variant.stderr

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0603]: tuple variant `Tuple` is private
44
LL | let variant_tuple = NonExhaustiveVariants::Tuple(640);
55
| ^^^^^ private tuple variant
66
|
7+
::: $DIR/auxiliary/variants.rs:5:5
8+
|
9+
LL | #[non_exhaustive] Tuple(u32),
10+
| ----------------- the tuple variant is `#[non_exhaustive]`
11+
|
712
note: the tuple variant `Tuple` is defined here
813
--> $DIR/auxiliary/variants.rs:5:23
914
|
@@ -16,6 +21,11 @@ error[E0603]: unit variant `Unit` is private
1621
LL | let variant_unit = NonExhaustiveVariants::Unit;
1722
| ^^^^ private unit variant
1823
|
24+
::: $DIR/auxiliary/variants.rs:4:5
25+
|
26+
LL | #[non_exhaustive] Unit,
27+
| ----------------- the unit variant is `#[non_exhaustive]`
28+
|
1929
note: the unit variant `Unit` is defined here
2030
--> $DIR/auxiliary/variants.rs:4:23
2131
|
@@ -28,6 +38,11 @@ error[E0603]: unit variant `Unit` is private
2838
LL | NonExhaustiveVariants::Unit => "",
2939
| ^^^^ private unit variant
3040
|
41+
::: $DIR/auxiliary/variants.rs:4:5
42+
|
43+
LL | #[non_exhaustive] Unit,
44+
| ----------------- the unit variant is `#[non_exhaustive]`
45+
|
3146
note: the unit variant `Unit` is defined here
3247
--> $DIR/auxiliary/variants.rs:4:23
3348
|
@@ -40,6 +55,11 @@ error[E0603]: tuple variant `Tuple` is private
4055
LL | NonExhaustiveVariants::Tuple(fe_tpl) => "",
4156
| ^^^^^ private tuple variant
4257
|
58+
::: $DIR/auxiliary/variants.rs:5:5
59+
|
60+
LL | #[non_exhaustive] Tuple(u32),
61+
| ----------------- the tuple variant is `#[non_exhaustive]`
62+
|
4363
note: the tuple variant `Tuple` is defined here
4464
--> $DIR/auxiliary/variants.rs:5:23
4565
|
@@ -52,6 +72,11 @@ error[E0603]: tuple variant `Tuple` is private
5272
LL | if let NonExhaustiveVariants::Tuple(fe_tpl) = variant_struct {
5373
| ^^^^^ private tuple variant
5474
|
75+
::: $DIR/auxiliary/variants.rs:5:5
76+
|
77+
LL | #[non_exhaustive] Tuple(u32),
78+
| ----------------- the tuple variant is `#[non_exhaustive]`
79+
|
5580
note: the tuple variant `Tuple` is defined here
5681
--> $DIR/auxiliary/variants.rs:5:23
5782
|

0 commit comments

Comments
 (0)