stop pointing at definitions of missing fields
This commit is contained in:
parent
1e7ab0bbd7
commit
f847261478
@ -4590,7 +4590,6 @@ dependencies = [
|
|||||||
name = "rustc_typeck"
|
name = "rustc_typeck"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itertools",
|
|
||||||
"rustc_arena",
|
"rustc_arena",
|
||||||
"rustc_ast",
|
"rustc_ast",
|
||||||
"rustc_attr",
|
"rustc_attr",
|
||||||
|
@ -10,7 +10,6 @@ doctest = false
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
rustc_arena = { path = "../rustc_arena" }
|
rustc_arena = { path = "../rustc_arena" }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
itertools = "0.10.1"
|
|
||||||
rustc_macros = { path = "../rustc_macros" }
|
rustc_macros = { path = "../rustc_macros" }
|
||||||
rustc_middle = { path = "../rustc_middle" }
|
rustc_middle = { path = "../rustc_middle" }
|
||||||
rustc_attr = { path = "../rustc_attr" }
|
rustc_attr = { path = "../rustc_attr" }
|
||||||
|
@ -23,13 +23,12 @@ use crate::type_error_struct;
|
|||||||
|
|
||||||
use super::suggest_call_constructor;
|
use super::suggest_call_constructor;
|
||||||
use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive};
|
use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive};
|
||||||
use itertools::{Either, Itertools};
|
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||||
use rustc_errors::{
|
use rustc_errors::{
|
||||||
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
|
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId,
|
||||||
EmissionGuarantee, ErrorGuaranteed, MultiSpan,
|
EmissionGuarantee, ErrorGuaranteed,
|
||||||
};
|
};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def::{CtorKind, DefKind, Res};
|
use rustc_hir::def::{CtorKind, DefKind, Res};
|
||||||
@ -1682,11 +1681,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if !private_fields.is_empty()
|
if !private_fields.is_empty() {
|
||||||
&& tcx
|
|
||||||
.visibility(variant.def_id)
|
|
||||||
.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
|
|
||||||
{
|
|
||||||
self.report_private_fields(adt_ty, span, private_fields, ast_fields);
|
self.report_private_fields(adt_ty, span, private_fields, ast_fields);
|
||||||
} else {
|
} else {
|
||||||
self.report_missing_fields(
|
self.report_missing_fields(
|
||||||
@ -1826,20 +1821,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
private_fields: Vec<&ty::FieldDef>,
|
private_fields: Vec<&ty::FieldDef>,
|
||||||
used_fields: &'tcx [hir::ExprField<'tcx>],
|
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(
|
let mut err = self.tcx.sess.struct_span_err(
|
||||||
span,
|
span,
|
||||||
&format!(
|
&format!(
|
||||||
@ -1847,28 +1828,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
let (used_private_fields, remaining_private_fields): (
|
let (used_private_fields, remaining_private_fields): (
|
||||||
Vec<(Symbol, Span)>,
|
Vec<(Symbol, Span, bool)>,
|
||||||
Vec<(Symbol, Span)>,
|
Vec<(Symbol, Span, bool)>,
|
||||||
) = private_fields.iter().partition_map(|field| {
|
) = private_fields
|
||||||
match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
|
.iter()
|
||||||
Some(used_field) => Either::Left((field.name, used_field.span)),
|
.map(|field| {
|
||||||
None => Either::Right((field.name, self.tcx.def_span(field.did))),
|
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();
|
let remaining_private_fields_len = remaining_private_fields.len();
|
||||||
err.span_labels(used_private_fields.iter().map(|(_, span)| *span), "private field");
|
let names = match &remaining_private_fields
|
||||||
err.span_note(
|
.iter()
|
||||||
MultiSpan::from_spans(remaining_private_fields.iter().map(|(_, span)| *span).collect()),
|
.map(|(name, _, _)| name.to_string())
|
||||||
format!(
|
.collect::<Vec<_>>()[..]
|
||||||
"missing field{s} {names}{are} private",
|
{
|
||||||
s = pluralize!(remaining_private_fields_len),
|
_ if remaining_private_fields_len > 6 => String::new(),
|
||||||
are = pluralize!("is", remaining_private_fields_len),
|
[name] => format!("`{name}` "),
|
||||||
names = field_names(
|
[names @ .., last] => {
|
||||||
remaining_private_fields.iter().map(|(name, _)| *name).collect(),
|
let names = names.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
|
||||||
remaining_private_fields_len
|
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();
|
err.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,11 +4,7 @@ error: cannot construct `Foo` with struct literal syntax due to private fields
|
|||||||
LL | foo::Foo {};
|
LL | foo::Foo {};
|
||||||
| ^^^^^^^^
|
| ^^^^^^^^
|
||||||
|
|
|
|
||||||
note: missing field `you_cant_use_this_field` is private
|
= note: ... and other private field `you_cant_use_this_field` that were not provided
|
||||||
--> $DIR/issue-76077.rs:3:9
|
|
||||||
|
|
|
||||||
LL | you_cant_use_this_field: bool,
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -16,11 +16,7 @@ error: cannot construct `Pub` with struct literal syntax due to private fields
|
|||||||
LL | foo::Pub {};
|
LL | foo::Pub {};
|
||||||
| ^^^^^^^^
|
| ^^^^^^^^
|
||||||
|
|
|
|
||||||
note: missing field `private` is private
|
= note: ... and other private field `private` that were not provided
|
||||||
--> $DIR/issue-79593.rs:2:22
|
|
||||||
|
|
|
||||||
LL | pub struct Pub { private: () }
|
|
||||||
| ^^^^^^^^^^^
|
|
||||||
|
|
||||||
error[E0063]: missing field `y` in initializer of `Enum`
|
error[E0063]: missing field `y` in initializer of `Enum`
|
||||||
--> $DIR/issue-79593.rs:23:5
|
--> $DIR/issue-79593.rs:23:5
|
||||||
|
@ -4,11 +4,7 @@ error: cannot construct `Foo` with struct literal syntax due to private fields
|
|||||||
LL | foo::Foo {};
|
LL | foo::Foo {};
|
||||||
| ^^^^^^^^
|
| ^^^^^^^^
|
||||||
|
|
|
|
||||||
note: missing field `you_cant_use_this_field` is private
|
= note: ... and other private field `you_cant_use_this_field` that were not provided
|
||||||
--> $DIR/issue-87872-missing-inaccessible-field-literal.rs:4:9
|
|
||||||
|
|
|
||||||
LL | you_cant_use_this_field: bool,
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -9,15 +9,7 @@ LL | a: (),
|
|||||||
LL | b: (),
|
LL | b: (),
|
||||||
| ----- private field
|
| ----- private field
|
||||||
|
|
|
|
||||||
note: missing fields `c`, `d` and `e` are private
|
= note: ... and other private fields `c`, `d` and `e` that were not provided
|
||||||
--> $DIR/missing-private-fields-in-struct-literal.rs:6:9
|
|
||||||
|
|
|
||||||
LL | c: (),
|
|
||||||
| ^^^^^
|
|
||||||
LL | d: (),
|
|
||||||
| ^^^^^
|
|
||||||
LL | e: (),
|
|
||||||
| ^^^^^
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user