Rollup merge of #110013 - compiler-errors:non-exhaustive-privacy-reason, r=WaffleLapkin

Label `non_exhaustive` attribute on privacy errors from non-local items

Label when an ADT is `non_exhaustive` and we get a privacy error, help with confusion in a case like this:

```rust
#[non_exhaustive]
pub struct Foo;

// other crate
let x = Foo;
//~^ ERROR unit struct `Foo` is private
```
This commit is contained in:
Matthias Krüger 2023-04-07 00:00:25 +02:00 committed by GitHub
commit e70818983b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 11 deletions

View File

@ -1607,7 +1607,17 @@ fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
let mut err = let mut err =
struct_span_err!(self.tcx.sess, ident.span, E0603, "{} `{}` is private", descr, ident); struct_span_err!(self.tcx.sess, ident.span, E0603, "{} `{}` is private", descr, ident);
err.span_label(ident.span, &format!("private {}", descr)); err.span_label(ident.span, &format!("private {}", descr));
if let Some(span) = ctor_fields_span {
let mut non_exhaustive = None;
// If an ADT is foreign and marked as `non_exhaustive`, then that's
// probably why we have the privacy error.
// Otherwise, point out if the struct has any private fields.
if let Some(def_id) = res.opt_def_id()
&& !def_id.is_local()
&& let Some(attr) = self.tcx.get_attr(def_id, sym::non_exhaustive)
{
non_exhaustive = Some(attr.span);
} else if let Some(span) = ctor_fields_span {
err.span_label(span, "a constructor is private if any of the fields is private"); err.span_label(span, "a constructor is private if any of the fields is private");
if let Res::Def(_, d) = res && let Some(fields) = self.field_visibility_spans.get(&d) { if let Res::Def(_, d) = res && let Some(fields) = self.field_visibility_spans.get(&d) {
err.multipart_suggestion_verbose( err.multipart_suggestion_verbose(
@ -1656,6 +1666,14 @@ fn report_privacy_error(&self, privacy_error: &PrivacyError<'_>) {
if !first && binding.vis.is_public() { if !first && binding.vis.is_public() {
note_span.push_span_label(def_span, "consider importing it directly"); note_span.push_span_label(def_span, "consider importing it directly");
} }
// Final step in the import chain, point out if the ADT is `non_exhaustive`
// which is probably why this privacy violation occurred.
if next_binding.is_none() && let Some(span) = non_exhaustive {
note_span.push_span_label(
span,
format!("cannot be constructed because it is `#[non_exhaustive]`"),
);
}
err.span_note(note_span, &msg); err.span_note(note_span, &msg);
} }

View File

@ -10,14 +10,11 @@ error[E0603]: tuple struct constructor `TupleStruct` is private
LL | let ts_explicit = structs::TupleStruct(640, 480); LL | let ts_explicit = structs::TupleStruct(640, 480);
| ^^^^^^^^^^^ private tuple struct constructor | ^^^^^^^^^^^ private tuple struct constructor
| |
::: $DIR/auxiliary/structs.rs:12:24
|
LL | pub struct TupleStruct(pub u16, pub u16);
| ---------------- a constructor is private if any of the fields is private
|
note: the tuple struct constructor `TupleStruct` is defined here note: the tuple struct constructor `TupleStruct` is defined here
--> $DIR/auxiliary/structs.rs:12:1 --> $DIR/auxiliary/structs.rs:12:1
| |
LL | #[non_exhaustive]
| ----------------- cannot be constructed because it is `#[non_exhaustive]`
LL | pub struct TupleStruct(pub u16, pub u16); LL | pub struct TupleStruct(pub u16, pub u16);
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -30,6 +27,8 @@ LL | let us_explicit = structs::UnitStruct;
note: the unit struct `UnitStruct` is defined here note: the unit struct `UnitStruct` is defined here
--> $DIR/auxiliary/structs.rs:9:1 --> $DIR/auxiliary/structs.rs:9:1
| |
LL | #[non_exhaustive]
| ----------------- cannot be constructed because it is `#[non_exhaustive]`
LL | pub struct UnitStruct; LL | pub struct UnitStruct;
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^

View File

@ -8,7 +8,9 @@ note: the tuple variant `Tuple` is defined here
--> $DIR/auxiliary/variants.rs:5:23 --> $DIR/auxiliary/variants.rs:5:23
| |
LL | #[non_exhaustive] Tuple(u32), LL | #[non_exhaustive] Tuple(u32),
| ^^^^^ | ----------------- ^^^^^
| |
| cannot be constructed because it is `#[non_exhaustive]`
error[E0603]: unit variant `Unit` is private error[E0603]: unit variant `Unit` is private
--> $DIR/variant.rs:14:47 --> $DIR/variant.rs:14:47
@ -20,7 +22,9 @@ note: the unit variant `Unit` is defined here
--> $DIR/auxiliary/variants.rs:4:23 --> $DIR/auxiliary/variants.rs:4:23
| |
LL | #[non_exhaustive] Unit, LL | #[non_exhaustive] Unit,
| ^^^^ | ----------------- ^^^^
| |
| cannot be constructed because it is `#[non_exhaustive]`
error[E0603]: unit variant `Unit` is private error[E0603]: unit variant `Unit` is private
--> $DIR/variant.rs:18:32 --> $DIR/variant.rs:18:32
@ -32,7 +36,9 @@ note: the unit variant `Unit` is defined here
--> $DIR/auxiliary/variants.rs:4:23 --> $DIR/auxiliary/variants.rs:4:23
| |
LL | #[non_exhaustive] Unit, LL | #[non_exhaustive] Unit,
| ^^^^ | ----------------- ^^^^
| |
| cannot be constructed because it is `#[non_exhaustive]`
error[E0603]: tuple variant `Tuple` is private error[E0603]: tuple variant `Tuple` is private
--> $DIR/variant.rs:20:32 --> $DIR/variant.rs:20:32
@ -44,7 +50,9 @@ note: the tuple variant `Tuple` is defined here
--> $DIR/auxiliary/variants.rs:5:23 --> $DIR/auxiliary/variants.rs:5:23
| |
LL | #[non_exhaustive] Tuple(u32), LL | #[non_exhaustive] Tuple(u32),
| ^^^^^ | ----------------- ^^^^^
| |
| cannot be constructed because it is `#[non_exhaustive]`
error[E0603]: tuple variant `Tuple` is private error[E0603]: tuple variant `Tuple` is private
--> $DIR/variant.rs:26:35 --> $DIR/variant.rs:26:35
@ -56,7 +64,9 @@ note: the tuple variant `Tuple` is defined here
--> $DIR/auxiliary/variants.rs:5:23 --> $DIR/auxiliary/variants.rs:5:23
| |
LL | #[non_exhaustive] Tuple(u32), LL | #[non_exhaustive] Tuple(u32),
| ^^^^^ | ----------------- ^^^^^
| |
| cannot be constructed because it is `#[non_exhaustive]`
error[E0639]: cannot create non-exhaustive variant using struct expression error[E0639]: cannot create non-exhaustive variant using struct expression
--> $DIR/variant.rs:8:26 --> $DIR/variant.rs:8:26