forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#66722 - matthewjasper:non_exhaustive_borrow…
…ck, r=varkor Handle non_exhaustive in borrow checking Borrow check can tell whether a pattern is exhaustive or not, make sure that `non_exhaustive` prevents this.
- Loading branch information
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/test/ui/rfc-2008-non-exhaustive/auxiliary/monovariants.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/test/ui/rfc-2008-non-exhaustive/borrowck-exhaustive.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/test/ui/rfc-2008-non-exhaustive/borrowck-non-exhaustive.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/test/ui/rfc-2008-non-exhaustive/borrowck-non-exhaustive.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`. |