Skip to content

Commit 0d9a6ed

Browse files
authored
Rollup merge of rust-lang#82789 - csmoe:issue-82772, r=estebank
Get with field index from pattern slice instead of directly indexing Closes rust-lang#82772 r? ``@estebank`` rust-lang#82789 (comment) > ``@estebank`` So the real cause is we only generate single pattern for Box here https://github.com/csmoe/rust/blob/615b03aeaa8ce9819de7828740ab3cd7def4fa76/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs#L1130-L1132 But in the replacing function, it tries to index on the 1-length pattern slice with field 1, thus out of bounds. https://github.com/csmoe/rust/blob/615b03aeaa8ce9819de7828740ab3cd7def4fa76/compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs#L1346
2 parents f0ebc10 + 77fb6a0 commit 0d9a6ed

7 files changed

+44
-6
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, .. } => {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// aux-build:struct_variant_privacy.rs
22
extern crate struct_variant_privacy;
33

4-
fn f(b: struct_variant_privacy::Bar) { //~ ERROR enum `Bar` is private
4+
fn f(b: struct_variant_privacy::Bar) {
5+
//~^ ERROR enum `Bar` is private
56
match b {
67
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
78
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | enum Bar {
1111
| ^^^^^^^^
1212

1313
error[E0603]: enum `Bar` is private
14-
--> $DIR/struct-variant-privacy-xc.rs:6:33
14+
--> $DIR/struct-variant-privacy-xc.rs:7:33
1515
|
1616
LL | struct_variant_privacy::Bar::Baz { a: _a } => {}
1717
| ^^^ private enum

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
mod foo {
22
enum Bar {
3-
Baz { a: isize }
3+
Baz { a: isize },
44
}
55
}
66

7-
fn f(b: foo::Bar) { //~ ERROR enum `Bar` is private
7+
fn f(b: foo::Bar) {
8+
//~^ ERROR enum `Bar` is private
89
match b {
910
foo::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
1011
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | enum Bar {
1111
| ^^^^^^^^
1212

1313
error[E0603]: enum `Bar` is private
14-
--> $DIR/struct-variant-privacy.rs:9:14
14+
--> $DIR/struct-variant-privacy.rs:10:14
1515
|
1616
LL | foo::Bar::Baz { a: _a } => {}
1717
| ^^^ private enum

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
3+
fn main() {
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
8+
}
9+
10+
mod a {
11+
#[derive(Default)]
12+
pub struct ModPrivateStruct(u8, u8);
13+
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0451]: field `0` of struct `Box` is private
2+
--> $DIR/issue-82772.rs:5:15
3+
|
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+
|
10+
LL | let Box { 1: _, .. }: Box<()>;
11+
| ^^^^ private field
12+
13+
error[E0451]: field `1` of struct `ModPrivateStruct` is private
14+
--> $DIR/issue-82772.rs:7:28
15+
|
16+
LL | let ModPrivateStruct { 1: _, .. } = ModPrivateStruct::default();
17+
| ^^^^ private field
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0451`.

0 commit comments

Comments
 (0)