Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle non_exhaustive in borrow checking #66722

Merged
merged 1 commit into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc_mir/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
self.hir.tcx().features().exhaustive_patterns &&
!v.uninhabited_from(self.hir.tcx(), substs, adt_def.adt_kind()).is_empty()
}
});
}) && (adt_def.did.is_local() || !adt_def.is_variant_list_non_exhaustive());
if irrefutable {
let place = tcx.mk_place_downcast(match_pair.place, adt_def, variant_index);
candidate.match_pairs.extend(self.field_match_pairs(place, subpatterns));
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/auxiliary/monovariants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[non_exhaustive]
pub enum NonExhaustiveMonovariant {
Variant(u32),
}

pub enum ExhaustiveMonovariant {
Variant(u32),
}
42 changes: 42 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/borrowck-exhaustive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Test that the borrow checker doesn't consider checking an exhaustive pattern
// as an access.

// check-pass

// aux-build:monovariants.rs
extern crate monovariants;

use monovariants::ExhaustiveMonovariant;

enum Local {
Variant(u32),
}

#[non_exhaustive]
enum LocalNonExhaustive {
Variant(u32),
}

fn main() {
let mut x = ExhaustiveMonovariant::Variant(1);
let y = &mut x;
match x {
ExhaustiveMonovariant::Variant(_) => {},
_ => {},
}
drop(y);
let mut x = Local::Variant(1);
let y = &mut x;
match x {
Local::Variant(_) => {},
_ => {},
}
drop(y);
let mut x = LocalNonExhaustive::Variant(1);
let y = &mut x;
match x {
LocalNonExhaustive::Variant(_) => {},
_ => {},
}
drop(y);
}
18 changes: 18 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/borrowck-non-exhaustive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Test that the borrow checker considers `#[non_exhaustive]` when checking
// whether a match contains a discriminant read.

// aux-build:monovariants.rs
extern crate monovariants;

use monovariants::NonExhaustiveMonovariant;

fn main() {
let mut x = NonExhaustiveMonovariant::Variant(1);
let y = &mut x;
match x {
NonExhaustiveMonovariant::Variant(_) => {},
//~^ ERROR cannot use `x` because it was mutably borrowed
_ => {},
}
drop(y);
}
15 changes: 15 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/borrowck-non-exhaustive.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0503]: cannot use `x` because it was mutably borrowed
--> $DIR/borrowck-non-exhaustive.rs:13:9
|
LL | let y = &mut x;
| ------ borrow of `x` occurs here
LL | match x {
LL | NonExhaustiveMonovariant::Variant(_) => {},
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ use of borrowed `x`
...
LL | drop(y);
| - borrow later used here

error: aborting due to previous error

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