1
Fork 0

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 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.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,