fix: check before index into generated patterns

This commit is contained in:
csmoe 2021-03-12 01:49:27 +08:00
parent 2fd2796aae
commit 77fb6a0f32
8 changed files with 26 additions and 81 deletions

View File

@ -1343,7 +1343,9 @@ fn replace_fields_indexed(
match &mut fields {
Fields::Vec(pats) => {
for (i, pat) in new_pats {
pats[i] = pat
if let Some(p) = pats.get_mut(i) {
*p = pat;
}
}
}
Fields::Filtered { fields, .. } => {

View File

@ -1176,7 +1176,6 @@ fn check_struct_pat_fields(
let mut no_field_errors = true;
let mut inexistent_fields = vec![];
let mut invisible_fields = vec![];
// Typecheck each field.
for field in fields {
let span = field.span;
@ -1192,12 +1191,6 @@ fn check_struct_pat_fields(
field_map
.get(&ident)
.map(|(i, f)| {
if !f
.vis
.is_accessible_from(tcx.parent_module(pat.hir_id).to_def_id(), tcx)
{
invisible_fields.push(field.ident);
}
self.write_field_index(field.hir_id, *i);
self.tcx.check_stability(f.did, Some(pat.hir_id), span);
self.field_ty(span, f, substs)
@ -1288,13 +1281,6 @@ fn check_struct_pat_fields(
self.error_tuple_variant_index_shorthand(variant, pat, fields)
{
err.emit();
} else if !invisible_fields.is_empty() {
let mut err = self.error_invisible_fields(
adt.variant_descr(),
&invisible_fields,
variant,
);
err.emit();
}
}
}
@ -1373,41 +1359,6 @@ fn error_field_already_bound(&self, span: Span, ident: Ident, other_field: Span)
.emit();
}
fn error_invisible_fields(
&self,
kind_name: &str,
invisible_fields: &[Ident],
variant: &ty::VariantDef,
) -> DiagnosticBuilder<'tcx> {
let spans = invisible_fields.iter().map(|ident| ident.span).collect::<Vec<_>>();
let (field_names, t) = if invisible_fields.len() == 1 {
(format!("a field named `{}`", invisible_fields[0]), "is")
} else {
(
format!(
"fields named {}",
invisible_fields
.iter()
.map(|ident| format!("`{}`", ident))
.collect::<Vec<String>>()
.join(", ")
),
"are",
)
};
let err = struct_span_err!(
self.tcx.sess,
spans,
E0603,
"cannot match on {} of {} `{}`, which {} not accessible in current scope",
field_names,
kind_name,
self.tcx.def_path_str(variant.def_id),
t
);
err
}
fn error_inexistent_fields(
&self,
kind_name: &str,

View File

@ -4,8 +4,7 @@
fn f(b: struct_variant_privacy::Bar) {
//~^ ERROR enum `Bar` is private
match b {
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR cannot match on
//~^ ERROR enum `Bar` is private
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
}
}

View File

@ -22,12 +22,6 @@ note: the enum `Bar` is defined here
LL | enum Bar {
| ^^^^^^^^
error[E0603]: cannot match on a field named `a` of variant `struct_variant_privacy::Bar::Baz`, which is not accessible in current scope
--> $DIR/struct-variant-privacy-xc.rs:7:44
|
LL | struct_variant_privacy::Bar::Baz { a: _a } => {}
| ^
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0603`.

View File

@ -8,7 +8,6 @@ fn f(b: foo::Bar) {
//~^ ERROR enum `Bar` is private
match b {
foo::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
//~^ ERROR cannot match on
}
}

View File

@ -22,12 +22,6 @@ note: the enum `Bar` is defined here
LL | enum Bar {
| ^^^^^^^^
error[E0603]: cannot match on a field named `a` of variant `Bar::Baz`, which is not accessible in current scope
--> $DIR/struct-variant-privacy.rs:10:25
|
LL | foo::Bar::Baz { a: _a } => {}
| ^
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0603`.

View File

@ -1,13 +1,13 @@
// edition:2018
fn main() {
use a::LocalModPrivateStruct;
let Box { 1: _, .. }: Box<()>; //~ ERROR cannot match on
let LocalModPrivateStruct { 1: _, .. } = LocalModPrivateStruct::default();
//~^ ERROR cannot match on
use a::ModPrivateStruct;
let Box { 0: _, .. }: Box<()>; //~ ERROR field `0` of
let Box { 1: _, .. }: Box<()>; //~ ERROR field `1` of
let ModPrivateStruct { 1: _, .. } = ModPrivateStruct::default(); //~ ERROR field `1` of
}
mod a {
#[derive(Default)]
pub struct LocalModPrivateStruct(u8, u8);
pub struct ModPrivateStruct(u8, u8);
}

View File

@ -1,15 +1,21 @@
error[E0603]: cannot match on a field named `1` of struct `Box`, which is not accessible in current scope
error[E0451]: field `0` of struct `Box` is private
--> $DIR/issue-82772.rs:5:15
|
LL | let Box { 1: _, .. }: Box<()>;
| ^
LL | let Box { 0: _, .. }: Box<()>;
| ^^^^ private field
error[E0603]: cannot match on a field named `1` of struct `LocalModPrivateStruct`, which is not accessible in current scope
--> $DIR/issue-82772.rs:6:33
error[E0451]: field `1` of struct `Box` is private
--> $DIR/issue-82772.rs:6:15
|
LL | let LocalModPrivateStruct { 1: _, .. } = LocalModPrivateStruct::default();
| ^
LL | let Box { 1: _, .. }: Box<()>;
| ^^^^ private field
error: aborting due to 2 previous errors
error[E0451]: field `1` of struct `ModPrivateStruct` is private
--> $DIR/issue-82772.rs:7:28
|
LL | let ModPrivateStruct { 1: _, .. } = ModPrivateStruct::default();
| ^^^^ private field
For more information about this error, try `rustc --explain E0603`.
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0451`.