stop pointing at definitions of missing fields

This commit is contained in:
Takayuki Maeda 2022-06-22 12:01:41 +09:00
parent 1e7ab0bbd7
commit f847261478
7 changed files with 35 additions and 68 deletions

View File

@ -4590,7 +4590,6 @@ dependencies = [
name = "rustc_typeck"
version = "0.0.0"
dependencies = [
"itertools",
"rustc_arena",
"rustc_ast",
"rustc_attr",

View File

@ -10,7 +10,6 @@ doctest = false
[dependencies]
rustc_arena = { path = "../rustc_arena" }
tracing = "0.1"
itertools = "0.10.1"
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_attr = { path = "../rustc_attr" }

View File

@ -23,13 +23,12 @@ use crate::type_error_struct;
use super::suggest_call_constructor;
use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive};
use itertools::{Either, Itertools};
use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
EmissionGuarantee, ErrorGuaranteed, MultiSpan,
EmissionGuarantee, ErrorGuaranteed,
};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
@ -1682,11 +1681,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
})
.collect();
if !private_fields.is_empty()
&& tcx
.visibility(variant.def_id)
.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
{
if !private_fields.is_empty() {
self.report_private_fields(adt_ty, span, private_fields, ast_fields);
} else {
self.report_missing_fields(
@ -1826,20 +1821,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
private_fields: Vec<&ty::FieldDef>,
used_fields: &'tcx [hir::ExprField<'tcx>],
) {
let field_names = |fields: Vec<Symbol>, len: usize| match &fields
.iter()
.map(|field| field.to_string())
.collect::<Vec<_>>()[..]
{
_ if len > 6 => String::new(),
[name] => format!("`{name}` "),
[names @ .., last] => {
let names = names.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
format!("{} and `{last}` ", names.join(", "))
}
[] => unreachable!(),
};
let mut err = self.tcx.sess.struct_span_err(
span,
&format!(
@ -1847,28 +1828,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
),
);
let (used_private_fields, remaining_private_fields): (
Vec<(Symbol, Span)>,
Vec<(Symbol, Span)>,
) = private_fields.iter().partition_map(|field| {
match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
Some(used_field) => Either::Left((field.name, used_field.span)),
None => Either::Right((field.name, self.tcx.def_span(field.did))),
}
});
Vec<(Symbol, Span, bool)>,
Vec<(Symbol, Span, bool)>,
) = private_fields
.iter()
.map(|field| {
match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
Some(used_field) => (field.name, used_field.span, true),
None => (field.name, self.tcx.def_span(field.did), false),
}
})
.partition(|field| field.2);
let remaining_private_fields_len = remaining_private_fields.len();
err.span_labels(used_private_fields.iter().map(|(_, span)| *span), "private field");
err.span_note(
MultiSpan::from_spans(remaining_private_fields.iter().map(|(_, span)| *span).collect()),
format!(
"missing field{s} {names}{are} private",
s = pluralize!(remaining_private_fields_len),
are = pluralize!("is", remaining_private_fields_len),
names = field_names(
remaining_private_fields.iter().map(|(name, _)| *name).collect(),
remaining_private_fields_len
)
),
);
let names = match &remaining_private_fields
.iter()
.map(|(name, _, _)| name.to_string())
.collect::<Vec<_>>()[..]
{
_ if remaining_private_fields_len > 6 => String::new(),
[name] => format!("`{name}` "),
[names @ .., last] => {
let names = names.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
format!("{} and `{last}` ", names.join(", "))
}
[] => unreachable!(),
};
err.span_labels(used_private_fields.iter().map(|(_, span, _)| *span), "private field");
err.note(format!(
"... and other private field{s} {names}that were not provided",
s = pluralize!(remaining_private_fields_len),
));
err.emit();
}

View File

@ -4,11 +4,7 @@ error: cannot construct `Foo` with struct literal syntax due to private fields
LL | foo::Foo {};
| ^^^^^^^^
|
note: missing field `you_cant_use_this_field` is private
--> $DIR/issue-76077.rs:3:9
|
LL | you_cant_use_this_field: bool,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ... and other private field `you_cant_use_this_field` that were not provided
error: aborting due to previous error

View File

@ -16,11 +16,7 @@ error: cannot construct `Pub` with struct literal syntax due to private fields
LL | foo::Pub {};
| ^^^^^^^^
|
note: missing field `private` is private
--> $DIR/issue-79593.rs:2:22
|
LL | pub struct Pub { private: () }
| ^^^^^^^^^^^
= note: ... and other private field `private` that were not provided
error[E0063]: missing field `y` in initializer of `Enum`
--> $DIR/issue-79593.rs:23:5

View File

@ -4,11 +4,7 @@ error: cannot construct `Foo` with struct literal syntax due to private fields
LL | foo::Foo {};
| ^^^^^^^^
|
note: missing field `you_cant_use_this_field` is private
--> $DIR/issue-87872-missing-inaccessible-field-literal.rs:4:9
|
LL | you_cant_use_this_field: bool,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ... and other private field `you_cant_use_this_field` that were not provided
error: aborting due to previous error

View File

@ -9,15 +9,7 @@ LL | a: (),
LL | b: (),
| ----- private field
|
note: missing fields `c`, `d` and `e` are private
--> $DIR/missing-private-fields-in-struct-literal.rs:6:9
|
LL | c: (),
| ^^^^^
LL | d: (),
| ^^^^^
LL | e: (),
| ^^^^^
= note: ... and other private fields `c`, `d` and `e` that were not provided
error: aborting due to previous error