Skip to content

Commit

Permalink
Rollup merge of rust-lang#82789 - csmoe:issue-82772, r=estebank
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dylan-DPC authored Mar 13, 2021
2 parents e7e1dc1 + 77fb6a0 commit a58092b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, .. } => {
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/structs/struct-variant-privacy-xc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// aux-build:struct_variant_privacy.rs
extern crate struct_variant_privacy;

fn f(b: struct_variant_privacy::Bar) { //~ ERROR enum `Bar` is private
fn f(b: struct_variant_privacy::Bar) {
//~^ ERROR enum `Bar` is private
match b {
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/structs/struct-variant-privacy-xc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | enum Bar {
| ^^^^^^^^

error[E0603]: enum `Bar` is private
--> $DIR/struct-variant-privacy-xc.rs:6:33
--> $DIR/struct-variant-privacy-xc.rs:7:33
|
LL | struct_variant_privacy::Bar::Baz { a: _a } => {}
| ^^^ private enum
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/structs/struct-variant-privacy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod foo {
enum Bar {
Baz { a: isize }
Baz { a: isize },
}
}

fn f(b: foo::Bar) { //~ ERROR enum `Bar` is private
fn f(b: foo::Bar) {
//~^ ERROR enum `Bar` is private
match b {
foo::Bar::Baz { a: _a } => {} //~ ERROR enum `Bar` is private
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/structs/struct-variant-privacy.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LL | enum Bar {
| ^^^^^^^^

error[E0603]: enum `Bar` is private
--> $DIR/struct-variant-privacy.rs:9:14
--> $DIR/struct-variant-privacy.rs:10:14
|
LL | foo::Bar::Baz { a: _a } => {}
| ^^^ private enum
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/typeck/issue-82772.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// edition:2018

fn main() {
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 ModPrivateStruct(u8, u8);
}
21 changes: 21 additions & 0 deletions src/test/ui/typeck/issue-82772.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0451]: field `0` of struct `Box` is private
--> $DIR/issue-82772.rs:5:15
|
LL | let Box { 0: _, .. }: Box<()>;
| ^^^^ private field

error[E0451]: field `1` of struct `Box` is private
--> $DIR/issue-82772.rs:6:15
|
LL | let Box { 1: _, .. }: Box<()>;
| ^^^^ private field

error[E0451]: field `1` of struct `ModPrivateStruct` is private
--> $DIR/issue-82772.rs:7:28
|
LL | let ModPrivateStruct { 1: _, .. } = ModPrivateStruct::default();
| ^^^^ private field

error: aborting due to 3 previous errors

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

0 comments on commit a58092b

Please sign in to comment.