.iter()
.map(|field| respan(field.span, field.ident.map_or(kw::Empty, |ident| ident.name)))
.collect();
- let field_vis = vdata.fields().iter().map(|field| field.vis.span).collect();
self.r.field_names.insert(def_id, field_names);
+ }
+
+ fn insert_field_visibilities_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
+ let field_vis = vdata
+ .fields()
+ .iter()
+ .map(|field| field.vis.span.until(field.ident.map_or(field.ty.span, |i| i.span)))
+ .collect();
self.r.field_visibility_spans.insert(def_id, field_vis);
}
// Record field names for error reporting.
self.insert_field_names_local(def_id, vdata);
+ self.insert_field_visibilities_local(def_id, vdata);
// If this is a tuple or unit struct, define a name
// in the value namespace as well.
Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id.to_def_id());
self.r.define(parent, ident, ValueNS, (ctor_res, ctor_vis, sp, expansion));
self.r.visibilities.insert(ctor_def_id, ctor_vis);
+ // We need the field visibility spans also for the constructor for E0603.
+ self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata);
self.r
.struct_constructors
// Record field names for error reporting.
self.insert_field_names_local(def_id, vdata);
+ self.insert_field_visibilities_local(def_id, vdata);
}
ItemKind::Trait(..) => {
// Record field names for error reporting.
self.insert_field_names_local(def_id.to_def_id(), &variant.data);
+ self.insert_field_visibilities_local(def_id.to_def_id(), &variant.data);
visit::walk_variant(self, variant);
}
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
-use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
+use rustc_errors::{
+ pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
+};
use rustc_feature::BUILTIN_ATTRIBUTES;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
err.span_label(ident.span, &format!("private {}", descr));
if let Some(span) = ctor_fields_span {
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) {
+ err.multipart_suggestion_verbose(
+ &format!(
+ "consider making the field{} publicly accessible",
+ pluralize!(fields.len())
+ ),
+ fields.iter().map(|span| (*span, "pub ".to_string())).collect(),
+ Applicability::MaybeIncorrect,
+ );
+ }
}
// Print the whole import chain to make it easier to see what happens.
|
LL | pub struct A(());
| ^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct A(pub ());
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:52:16
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:53:16
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `A` is private
--> $DIR/privacy5.rs:56:12
|
LL | pub struct A(());
| ^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct A(pub ());
+ | +++
error[E0603]: tuple struct constructor `A` is private
--> $DIR/privacy5.rs:57:12
|
LL | pub struct A(());
| ^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct A(pub ());
+ | +++
error[E0603]: tuple struct constructor `A` is private
--> $DIR/privacy5.rs:58:18
|
LL | pub struct A(());
| ^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct A(pub ());
+ | +++
error[E0603]: tuple struct constructor `A` is private
--> $DIR/privacy5.rs:59:18
|
LL | pub struct A(());
| ^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct A(pub ());
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:61:12
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:62:12
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:63:18
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:64:18
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:65:18
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:65:32
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:68:12
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:69:12
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:70:12
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:71:12
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:72:18
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:73:18
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:74:18
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:75:18
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `A` is private
--> $DIR/privacy5.rs:83:17
|
LL | pub struct A(());
| ^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct A(pub ());
+ | +++
error[E0603]: tuple struct constructor `B` is private
--> $DIR/privacy5.rs:84:17
|
LL | pub struct B(isize);
| ^^^^^^^^^^^^^^^^^^^^
+help: consider making the field publicly accessible
+ |
+LL | pub struct B(pub isize);
+ | +++
error[E0603]: tuple struct constructor `C` is private
--> $DIR/privacy5.rs:85:17
|
LL | pub struct C(pub isize, isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+help: consider making the fields publicly accessible
+ |
+LL | pub struct C(pub isize, pub isize);
+ | ~~~ +++
error[E0603]: tuple struct constructor `A` is private
--> $DIR/privacy5.rs:90:20