Suggest making private tuple struct field public

Fix #52144.
This commit is contained in:
Esteban Küber 2023-01-08 01:15:28 +00:00
parent b22c152958
commit ad13d9fbbe
8 changed files with 49 additions and 6 deletions

View File

@ -331,7 +331,9 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
.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);
self.r.field_visibility_spans.insert(def_id, field_vis);
}
fn insert_field_names_extern(&mut self, def_id: DefId) {

View File

@ -1451,6 +1451,26 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
.collect();
if non_visible_spans.len() > 0 {
if let Some(visibility_spans) = self.r.field_visibility_spans.get(&def_id) {
err.multipart_suggestion_verbose(
&format!(
"consider making the field{} publicly accessible",
pluralize!(visibility_spans.len())
),
visibility_spans
.iter()
.map(|span| {
(
*span,
if span.lo() == span.hi() { "pub " } else { "pub" }
.to_string(),
)
})
.collect(),
Applicability::MaybeIncorrect,
);
}
let mut m: MultiSpan = non_visible_spans.clone().into();
non_visible_spans
.into_iter()

View File

@ -881,6 +881,10 @@ pub struct Resolver<'a> {
/// Used for hints during error reporting.
field_names: FxHashMap<DefId, Vec<Spanned<Symbol>>>,
/// Span of the privacy modifier in fields of an item `DefId` accessible with dot syntax.
/// Used for hints during error reporting.
field_visibility_spans: FxHashMap<DefId, Vec<Span>>,
/// All imports known to succeed or fail.
determined_imports: Vec<&'a Import<'a>>,
@ -1268,6 +1272,7 @@ impl<'a> Resolver<'a> {
has_self: FxHashSet::default(),
field_names: FxHashMap::default(),
field_visibility_spans: FxHashMap::default(),
determined_imports: Vec::new(),
indeterminate_imports: Vec::new(),

View File

@ -9,6 +9,10 @@ note: constructor is not visible here due to private fields
|
LL | pub struct Bar(u8);
| ^^ private field
help: consider making the field publicly accessible
|
LL | pub struct Bar(pub u8);
| +++
error: aborting due to previous error

View File

@ -2,7 +2,7 @@
mod foo {
pub(crate) struct Foo(u8);
pub(crate) struct Bar(pub u8, u8, Foo);
pub(crate) struct Bar(pub u8, pub(in crate::foo) u8, Foo);
pub(crate) fn make_bar() -> Bar {
Bar(1, 12, Foo(10))

View File

@ -11,6 +11,10 @@ LL | let Bar(x, y, Foo(z)) = make_bar();
| ^ ^^^^^^ private field
| |
| private field
help: consider making the fields publicly accessible
|
LL | pub(crate) struct Bar(pub u8, pub u8, pub Foo);
| ~~~ ~~~ +++
error[E0532]: cannot match against a tuple struct which contains private fields
--> $DIR/issue-75907.rs:15:19
@ -23,6 +27,10 @@ note: constructor is not visible here due to private fields
|
LL | let Bar(x, y, Foo(z)) = make_bar();
| ^ private field
help: consider making the field publicly accessible
|
LL | pub(crate) struct Foo(pub u8);
| +++
error: aborting due to 2 previous errors

View File

@ -1,5 +1,5 @@
mod foo {
pub struct Bx(());
pub struct Bx(pub(in crate::foo) ());
}
mod bar {

View File

@ -7,8 +7,8 @@ LL | Bx(());
note: tuple struct `foo::Bx` exists but is inaccessible
--> $DIR/issue-42944.rs:2:5
|
LL | pub struct Bx(());
| ^^^^^^^^^^^^^^^^^^ not accessible
LL | pub struct Bx(pub(in crate::foo) ());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not accessible
error[E0423]: cannot initialize a tuple struct which contains private fields
--> $DIR/issue-42944.rs:9:9
@ -19,8 +19,12 @@ LL | Bx(());
note: constructor is not visible here due to private fields
--> $DIR/issue-42944.rs:2:19
|
LL | pub struct Bx(());
| ^^ private field
LL | pub struct Bx(pub(in crate::foo) ());
| ^^^^^^^^^^^^^^^^^^^^^ private field
help: consider making the field publicly accessible
|
LL | pub struct Bx(pub ());
| ~~~
error: aborting due to 2 previous errors