Skip to content

Commit 8ed2dc0

Browse files
Make span a bit better
1 parent 00d54c8 commit 8ed2dc0

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1608,11 +1608,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16081608
struct_span_err!(self.tcx.sess, ident.span, E0603, "{} `{}` is private", descr, ident);
16091609
err.span_label(ident.span, &format!("private {}", descr));
16101610

1611+
let mut non_exhaustive = None;
1612+
// If an ADT is foreign and marked as `non_exhaustive`, then that's
1613+
// probably why we have the privacy error.
1614+
// Otherwise, point out if the struct has any private fields.
16111615
if let Some(def_id) = res.opt_def_id()
16121616
&& !def_id.is_local()
16131617
&& let Some(attr) = self.tcx.get_attr(def_id, sym::non_exhaustive)
16141618
{
1615-
err.span_label(attr.span, format!("the {nonimport_descr} is `#[non_exhaustive]`"));
1619+
non_exhaustive = Some(attr.span);
16161620
} else if let Some(span) = ctor_fields_span {
16171621
err.span_label(span, "a constructor is private if any of the fields is private");
16181622
if let Res::Def(_, d) = res && let Some(fields) = self.field_visibility_spans.get(&d) {
@@ -1662,6 +1666,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16621666
if !first && binding.vis.is_public() {
16631667
note_span.push_span_label(def_span, "consider importing it directly");
16641668
}
1669+
// Final step in the import chain, point out if the ADT is `non_exhaustive`
1670+
// which is probably why this privacy violation occurred.
1671+
if next_binding.is_none() && let Some(span) = non_exhaustive {
1672+
note_span.push_span_label(
1673+
span,
1674+
format!("cannot be constructed because it is `#[non_exhaustive]`"),
1675+
);
1676+
}
16651677
err.span_note(note_span, &msg);
16661678
}
16671679

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ 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:11:1
14-
|
15-
LL | #[non_exhaustive]
16-
| ----------------- the tuple struct constructor is `#[non_exhaustive]`
17-
|
1813
note: the tuple struct constructor `TupleStruct` is defined here
1914
--> $DIR/auxiliary/structs.rs:12:1
2015
|
16+
LL | #[non_exhaustive]
17+
| ----------------- cannot be constructed because it is `#[non_exhaustive]`
2118
LL | pub struct TupleStruct(pub u16, pub u16);
2219
| ^^^^^^^^^^^^^^^^^^^^^^
2320

@@ -27,14 +24,11 @@ error[E0603]: unit struct `UnitStruct` is private
2724
LL | let us_explicit = structs::UnitStruct;
2825
| ^^^^^^^^^^ private unit struct
2926
|
30-
::: $DIR/auxiliary/structs.rs:8:1
31-
|
32-
LL | #[non_exhaustive]
33-
| ----------------- the unit struct is `#[non_exhaustive]`
34-
|
3527
note: the unit struct `UnitStruct` is defined here
3628
--> $DIR/auxiliary/structs.rs:9:1
3729
|
30+
LL | #[non_exhaustive]
31+
| ----------------- cannot be constructed because it is `#[non_exhaustive]`
3832
LL | pub struct UnitStruct;
3933
| ^^^^^^^^^^^^^^^^^^^^^
4034

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

+15-30
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,69 @@ 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-
|
127
note: the tuple variant `Tuple` is defined here
138
--> $DIR/auxiliary/variants.rs:5:23
149
|
1510
LL | #[non_exhaustive] Tuple(u32),
16-
| ^^^^^
11+
| ----------------- ^^^^^
12+
| |
13+
| cannot be constructed because it is `#[non_exhaustive]`
1714

1815
error[E0603]: unit variant `Unit` is private
1916
--> $DIR/variant.rs:14:47
2017
|
2118
LL | let variant_unit = NonExhaustiveVariants::Unit;
2219
| ^^^^ private unit variant
2320
|
24-
::: $DIR/auxiliary/variants.rs:4:5
25-
|
26-
LL | #[non_exhaustive] Unit,
27-
| ----------------- the unit variant is `#[non_exhaustive]`
28-
|
2921
note: the unit variant `Unit` is defined here
3022
--> $DIR/auxiliary/variants.rs:4:23
3123
|
3224
LL | #[non_exhaustive] Unit,
33-
| ^^^^
25+
| ----------------- ^^^^
26+
| |
27+
| cannot be constructed because it is `#[non_exhaustive]`
3428

3529
error[E0603]: unit variant `Unit` is private
3630
--> $DIR/variant.rs:18:32
3731
|
3832
LL | NonExhaustiveVariants::Unit => "",
3933
| ^^^^ private unit variant
4034
|
41-
::: $DIR/auxiliary/variants.rs:4:5
42-
|
43-
LL | #[non_exhaustive] Unit,
44-
| ----------------- the unit variant is `#[non_exhaustive]`
45-
|
4635
note: the unit variant `Unit` is defined here
4736
--> $DIR/auxiliary/variants.rs:4:23
4837
|
4938
LL | #[non_exhaustive] Unit,
50-
| ^^^^
39+
| ----------------- ^^^^
40+
| |
41+
| cannot be constructed because it is `#[non_exhaustive]`
5142

5243
error[E0603]: tuple variant `Tuple` is private
5344
--> $DIR/variant.rs:20:32
5445
|
5546
LL | NonExhaustiveVariants::Tuple(fe_tpl) => "",
5647
| ^^^^^ private tuple variant
5748
|
58-
::: $DIR/auxiliary/variants.rs:5:5
59-
|
60-
LL | #[non_exhaustive] Tuple(u32),
61-
| ----------------- the tuple variant is `#[non_exhaustive]`
62-
|
6349
note: the tuple variant `Tuple` is defined here
6450
--> $DIR/auxiliary/variants.rs:5:23
6551
|
6652
LL | #[non_exhaustive] Tuple(u32),
67-
| ^^^^^
53+
| ----------------- ^^^^^
54+
| |
55+
| cannot be constructed because it is `#[non_exhaustive]`
6856

6957
error[E0603]: tuple variant `Tuple` is private
7058
--> $DIR/variant.rs:26:35
7159
|
7260
LL | if let NonExhaustiveVariants::Tuple(fe_tpl) = variant_struct {
7361
| ^^^^^ private tuple variant
7462
|
75-
::: $DIR/auxiliary/variants.rs:5:5
76-
|
77-
LL | #[non_exhaustive] Tuple(u32),
78-
| ----------------- the tuple variant is `#[non_exhaustive]`
79-
|
8063
note: the tuple variant `Tuple` is defined here
8164
--> $DIR/auxiliary/variants.rs:5:23
8265
|
8366
LL | #[non_exhaustive] Tuple(u32),
84-
| ^^^^^
67+
| ----------------- ^^^^^
68+
| |
69+
| cannot be constructed because it is `#[non_exhaustive]`
8570

8671
error[E0639]: cannot create non-exhaustive variant using struct expression
8772
--> $DIR/variant.rs:8:26

0 commit comments

Comments
 (0)