Skip to content

Commit 77fb6a0

Browse files
committed
fix: check before index into generated patterns
1 parent 2fd2796 commit 77fb6a0

File tree

8 files changed

+25
-80
lines changed

8 files changed

+25
-80
lines changed

compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,9 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
13431343
match &mut fields {
13441344
Fields::Vec(pats) => {
13451345
for (i, pat) in new_pats {
1346-
pats[i] = pat
1346+
if let Some(p) = pats.get_mut(i) {
1347+
*p = pat;
1348+
}
13471349
}
13481350
}
13491351
Fields::Filtered { fields, .. } => {

compiler/rustc_typeck/src/check/pat.rs

-49
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11761176
let mut no_field_errors = true;
11771177

11781178
let mut inexistent_fields = vec![];
1179-
let mut invisible_fields = vec![];
11801179
// Typecheck each field.
11811180
for field in fields {
11821181
let span = field.span;
@@ -1192,12 +1191,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11921191
field_map
11931192
.get(&ident)
11941193
.map(|(i, f)| {
1195-
if !f
1196-
.vis
1197-
.is_accessible_from(tcx.parent_module(pat.hir_id).to_def_id(), tcx)
1198-
{
1199-
invisible_fields.push(field.ident);
1200-
}
12011194
self.write_field_index(field.hir_id, *i);
12021195
self.tcx.check_stability(f.did, Some(pat.hir_id), span);
12031196
self.field_ty(span, f, substs)
@@ -1288,13 +1281,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12881281
self.error_tuple_variant_index_shorthand(variant, pat, fields)
12891282
{
12901283
err.emit();
1291-
} else if !invisible_fields.is_empty() {
1292-
let mut err = self.error_invisible_fields(
1293-
adt.variant_descr(),
1294-
&invisible_fields,
1295-
variant,
1296-
);
1297-
err.emit();
12981284
}
12991285
}
13001286
}
@@ -1373,41 +1359,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13731359
.emit();
13741360
}
13751361

1376-
fn error_invisible_fields(
1377-
&self,
1378-
kind_name: &str,
1379-
invisible_fields: &[Ident],
1380-
variant: &ty::VariantDef,
1381-
) -> DiagnosticBuilder<'tcx> {
1382-
let spans = invisible_fields.iter().map(|ident| ident.span).collect::<Vec<_>>();
1383-
let (field_names, t) = if invisible_fields.len() == 1 {
1384-
(format!("a field named `{}`", invisible_fields[0]), "is")
1385-
} else {
1386-
(
1387-
format!(
1388-
"fields named {}",
1389-
invisible_fields
1390-
.iter()
1391-
.map(|ident| format!("`{}`", ident))
1392-
.collect::<Vec<String>>()
1393-
.join(", ")
1394-
),
1395-
"are",
1396-
)
1397-
};
1398-
let err = struct_span_err!(
1399-
self.tcx.sess,
1400-
spans,
1401-
E0603,
1402-
"cannot match on {} of {} `{}`, which {} not accessible in current scope",
1403-
field_names,
1404-
kind_name,
1405-
self.tcx.def_path_str(variant.def_id),
1406-
t
1407-
);
1408-
err
1409-
}
1410-
14111362
fn error_inexistent_fields(
14121363
&self,
14131364
kind_name: &str,

src/test/ui/structs/struct-variant-privacy-xc.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ extern crate struct_variant_privacy;
44
fn f(b: struct_variant_privacy::Bar) {
55
//~^ ERROR enum `Bar` is private
66
match b {
7-
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR cannot match on
8-
//~^ ERROR enum `Bar` is private
7+
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
98
}
109
}
1110

src/test/ui/structs/struct-variant-privacy-xc.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ note: the enum `Bar` is defined here
2222
LL | enum Bar {
2323
| ^^^^^^^^
2424

25-
error[E0603]: cannot match on a field named `a` of variant `struct_variant_privacy::Bar::Baz`, which is not accessible in current scope
26-
--> $DIR/struct-variant-privacy-xc.rs:7:44
27-
|
28-
LL | struct_variant_privacy::Bar::Baz { a: _a } => {}
29-
| ^
30-
31-
error: aborting due to 3 previous errors
25+
error: aborting due to 2 previous errors
3226

3327
For more information about this error, try `rustc --explain E0603`.

src/test/ui/structs/struct-variant-privacy.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn f(b: foo::Bar) {
88
//~^ ERROR enum `Bar` is private
99
match b {
1010
foo::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
11-
//~^ ERROR cannot match on
1211
}
1312
}
1413

src/test/ui/structs/struct-variant-privacy.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ note: the enum `Bar` is defined here
2222
LL | enum Bar {
2323
| ^^^^^^^^
2424

25-
error[E0603]: cannot match on a field named `a` of variant `Bar::Baz`, which is not accessible in current scope
26-
--> $DIR/struct-variant-privacy.rs:10:25
27-
|
28-
LL | foo::Bar::Baz { a: _a } => {}
29-
| ^
30-
31-
error: aborting due to 3 previous errors
25+
error: aborting due to 2 previous errors
3226

3327
For more information about this error, try `rustc --explain E0603`.

src/test/ui/typeck/issue-82772.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// edition:2018
22

33
fn main() {
4-
use a::LocalModPrivateStruct;
5-
let Box { 1: _, .. }: Box<()>; //~ ERROR cannot match on
6-
let LocalModPrivateStruct { 1: _, .. } = LocalModPrivateStruct::default();
7-
//~^ ERROR cannot match on
4+
use a::ModPrivateStruct;
5+
let Box { 0: _, .. }: Box<()>; //~ ERROR field `0` of
6+
let Box { 1: _, .. }: Box<()>; //~ ERROR field `1` of
7+
let ModPrivateStruct { 1: _, .. } = ModPrivateStruct::default(); //~ ERROR field `1` of
88
}
99

1010
mod a {
1111
#[derive(Default)]
12-
pub struct LocalModPrivateStruct(u8, u8);
12+
pub struct ModPrivateStruct(u8, u8);
1313
}

src/test/ui/typeck/issue-82772.stderr

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
error[E0603]: cannot match on a field named `1` of struct `Box`, which is not accessible in current scope
1+
error[E0451]: field `0` of struct `Box` is private
22
--> $DIR/issue-82772.rs:5:15
33
|
4+
LL | let Box { 0: _, .. }: Box<()>;
5+
| ^^^^ private field
6+
7+
error[E0451]: field `1` of struct `Box` is private
8+
--> $DIR/issue-82772.rs:6:15
9+
|
410
LL | let Box { 1: _, .. }: Box<()>;
5-
| ^
11+
| ^^^^ private field
612

7-
error[E0603]: cannot match on a field named `1` of struct `LocalModPrivateStruct`, which is not accessible in current scope
8-
--> $DIR/issue-82772.rs:6:33
13+
error[E0451]: field `1` of struct `ModPrivateStruct` is private
14+
--> $DIR/issue-82772.rs:7:28
915
|
10-
LL | let LocalModPrivateStruct { 1: _, .. } = LocalModPrivateStruct::default();
11-
| ^
16+
LL | let ModPrivateStruct { 1: _, .. } = ModPrivateStruct::default();
17+
| ^^^^ private field
1218

13-
error: aborting due to 2 previous errors
19+
error: aborting due to 3 previous errors
1420

15-
For more information about this error, try `rustc --explain E0603`.
21+
For more information about this error, try `rustc --explain E0451`.

0 commit comments

Comments
 (0)