1
Fork 0

Label non_exhaustive on privacy errors

This commit is contained in:
Michael Goulet 2023-04-06 16:49:43 +00:00
parent cf7ada217c
commit 00d54c879b
3 changed files with 40 additions and 4 deletions

View file

@ -1607,7 +1607,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
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 {
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)
{
err.span_label(attr.span, format!("the {nonimport_descr} is `#[non_exhaustive]`"));
} 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(

View file

@ -10,10 +10,10 @@ 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 ::: $DIR/auxiliary/structs.rs:11:1
| |
LL | pub struct TupleStruct(pub u16, pub u16); LL | #[non_exhaustive]
| ---------------- a constructor is private if any of the fields is private | ----------------- the tuple struct constructor is `#[non_exhaustive]`
| |
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
@ -27,6 +27,11 @@ error[E0603]: unit struct `UnitStruct` is private
LL | let us_explicit = structs::UnitStruct; LL | let us_explicit = structs::UnitStruct;
| ^^^^^^^^^^ private unit struct | ^^^^^^^^^^ private unit struct
| |
::: $DIR/auxiliary/structs.rs:8:1
|
LL | #[non_exhaustive]
| ----------------- the unit struct is `#[non_exhaustive]`
|
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
| |

View file

@ -4,6 +4,11 @@ error[E0603]: tuple variant `Tuple` is private
LL | let variant_tuple = NonExhaustiveVariants::Tuple(640); LL | let variant_tuple = NonExhaustiveVariants::Tuple(640);
| ^^^^^ private tuple variant | ^^^^^ private tuple variant
| |
::: $DIR/auxiliary/variants.rs:5:5
|
LL | #[non_exhaustive] Tuple(u32),
| ----------------- the tuple variant is `#[non_exhaustive]`
|
note: the tuple variant `Tuple` is defined here note: the tuple variant `Tuple` is defined here
--> $DIR/auxiliary/variants.rs:5:23 --> $DIR/auxiliary/variants.rs:5:23
| |
@ -16,6 +21,11 @@ error[E0603]: unit variant `Unit` is private
LL | let variant_unit = NonExhaustiveVariants::Unit; LL | let variant_unit = NonExhaustiveVariants::Unit;
| ^^^^ private unit variant | ^^^^ private unit variant
| |
::: $DIR/auxiliary/variants.rs:4:5
|
LL | #[non_exhaustive] Unit,
| ----------------- the unit variant is `#[non_exhaustive]`
|
note: the unit variant `Unit` is defined here note: the unit variant `Unit` is defined here
--> $DIR/auxiliary/variants.rs:4:23 --> $DIR/auxiliary/variants.rs:4:23
| |
@ -28,6 +38,11 @@ error[E0603]: unit variant `Unit` is private
LL | NonExhaustiveVariants::Unit => "", LL | NonExhaustiveVariants::Unit => "",
| ^^^^ private unit variant | ^^^^ private unit variant
| |
::: $DIR/auxiliary/variants.rs:4:5
|
LL | #[non_exhaustive] Unit,
| ----------------- the unit variant is `#[non_exhaustive]`
|
note: the unit variant `Unit` is defined here note: the unit variant `Unit` is defined here
--> $DIR/auxiliary/variants.rs:4:23 --> $DIR/auxiliary/variants.rs:4:23
| |
@ -40,6 +55,11 @@ error[E0603]: tuple variant `Tuple` is private
LL | NonExhaustiveVariants::Tuple(fe_tpl) => "", LL | NonExhaustiveVariants::Tuple(fe_tpl) => "",
| ^^^^^ private tuple variant | ^^^^^ private tuple variant
| |
::: $DIR/auxiliary/variants.rs:5:5
|
LL | #[non_exhaustive] Tuple(u32),
| ----------------- the tuple variant is `#[non_exhaustive]`
|
note: the tuple variant `Tuple` is defined here note: the tuple variant `Tuple` is defined here
--> $DIR/auxiliary/variants.rs:5:23 --> $DIR/auxiliary/variants.rs:5:23
| |
@ -52,6 +72,11 @@ error[E0603]: tuple variant `Tuple` is private
LL | if let NonExhaustiveVariants::Tuple(fe_tpl) = variant_struct { LL | if let NonExhaustiveVariants::Tuple(fe_tpl) = variant_struct {
| ^^^^^ private tuple variant | ^^^^^ private tuple variant
| |
::: $DIR/auxiliary/variants.rs:5:5
|
LL | #[non_exhaustive] Tuple(u32),
| ----------------- the tuple variant is `#[non_exhaustive]`
|
note: the tuple variant `Tuple` is defined here note: the tuple variant `Tuple` is defined here
--> $DIR/auxiliary/variants.rs:5:23 --> $DIR/auxiliary/variants.rs:5:23
| |