Skip to content

Instantly share code, notes, and snippets.

@matthewjasper
Created September 12, 2018 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjasper/20706927286a2f81768dc67a175b15d2 to your computer and use it in GitHub Desktop.
Save matthewjasper/20706927286a2f81768dc67a175b15d2 to your computer and use it in GitHub Desktop.
// Test that a (partially) mutably borrowed place can be matched on, so long as
// we don't have to read any values that are mutably borrowed to determine
// which arm to take.
//
// Test that we don't allow mutating the value being matched on in a way that
// changes which patterns it matches, until we have chosen an arm.
// compile-flags: -Zdisable-ast-check-for-mutation-in-guard
#![feature(nll)]
fn ok_mutation_in_guard(mut q: i32) {
match q {
// OK, mutation doesn't change which patterns g matches
_ if { q = 1; false } => (),
_ => (),
}
}
fn ok_indirect_mutation_in_guard(mut p: &bool) {
match *p {
// OK, mutation doesn't change which patterns s matches
_ if {
p = &true;
false
} => (),
_ => (),
}
}
fn mutation_invalidates_pattern_in_guard(mut q: bool) {
match q {
// s doesn't match the pattern with the guard by the end of the guard.
false if {
q = true; //~ ERROR
true
} => (),
_ => (),
}
}
fn mutation_invalidates_previous_pattern_in_guard(mut r: bool) {
match r {
// s matches a previous pattern by the end of the guard.
true => (),
_ if {
r = true; //~ ERROR
true
} => (),
_ => (),
}
}
fn match_on_borrowed_early_end(mut s: bool) {
let h = &mut s;
match s { //~ ERROR
// v changes value between the start of the match and when its value is checked.
_ if {
*h = !*h;
false
} => (),
true => (),
false => (),
}
}
fn bad_mutation_in_guard(mut t: bool) {
match t {
true => (),
false if {
t = true; //~ ERROR
false
} => (),
false => (),
}
}
fn bad_mutation_in_guard2(mut u: bool) {
match u {
// Guard changes the value bound in the last pattern.
_ => (),
_ if {
u = true; //~ ERROR
false
} => (),
x => (),
}
}
fn bad_indirect_mutation_in_guard(mut v: &bool) {
match *v {
true => (),
false if {
v = &true; //~ ERROR
false
} => (),
false => (),
}
}
fn bad_indirect_mutation_in_guard2(mut w: &bool) {
match w {
&true => (),
&false if {
w = &true; //~ ERROR
false
} => (),
&false => (),
}
}
fn bad_indirect_mutation_in_guard3(mut y: &bool) {
match y {
&_ => (),
&_ if {
y = &true; //~ ERROR
false
} => (),
&x => (),
}
}
fn main() {}
// Test that a (partially) mutably borrowed place can be matched on, so long as
// we don't have to read any values that are mutably borrowed to determine
// which arm to take.
//
// Test that we don't allow mutating the value being matched on in a way that
// changes which patterns it matches, until we have chosen an arm.
#![feature(nll)]
struct A(i32, i32);
fn struct_example(mut a: A) {
let x = &mut a.0;
match a { // OK, no access of borrowed data
_ if false => (),
A(_, r) => (),
}
x;
}
fn indirect_struct_example(mut b: &mut A) {
let x = &mut b.0;
match *b { // OK, no access of borrowed data
_ if false => (),
A(_, r) => (),
}
x;
}
fn underscore_example(mut c: i32) {
let r = &mut c;
match c { // OK, no access of borrowed data (or any data at all)
_ if false => (),
_ => (),
}
r;
}
enum E {
V(i32, i32),
W,
}
fn enum_example(mut e: E) {
let x = match e {
E::V(ref mut x, _) => x,
E::W => panic!(),
};
match e { // OK, no access of borrowed data
_ if false => (),
E::V(_, r) => (),
E::W => (),
}
x;
}
fn indirect_enum_example(mut f: &mut E) {
let x = match *f {
E::V(ref mut x, _) => x,
E::W => panic!(),
};
match f { // OK, no access of borrowed data
_ if false => (),
E::V(_, r) => (),
E::W => (),
}
x;
}
fn match_on_muatbly_borrowed_ref(mut p: &bool) {
let r = &mut p;
match *p { // OK, no access at all
_ if false => (),
_ => (),
}
r;
}
fn match_on_borrowed(mut t: bool) {
let x = &mut t;
match t {
true => (), //~ ERROR
false => (),
}
x;
}
enum Never {}
fn never_init() {
let n: Never;
match n {} //~ ERROR
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment