2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2019-10-15 08:28:42 -05:00
|
|
|
|
2018-09-25 16:51:35 -05:00
|
|
|
#![allow(unreachable_code)]
|
2019-12-11 08:51:28 -06:00
|
|
|
#![feature(never_type)]
|
2018-04-20 10:07:58 -05:00
|
|
|
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 17:15:06 -05:00
|
|
|
#[allow(unused)]
|
|
|
|
fn never_returns() {
|
|
|
|
loop {
|
|
|
|
break loop {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let value = 'outer: loop {
|
|
|
|
if 1 == 1 {
|
|
|
|
break 13;
|
|
|
|
} else {
|
|
|
|
let _never: ! = loop {
|
|
|
|
break loop {
|
|
|
|
break 'outer panic!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(value, 13);
|
|
|
|
|
|
|
|
let x = [1, 3u32, 5];
|
|
|
|
let y = [17];
|
|
|
|
let z = [];
|
|
|
|
let coerced: &[_] = loop {
|
|
|
|
match 2 {
|
|
|
|
1 => break &x,
|
|
|
|
2 => break &y,
|
|
|
|
3 => break &z,
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(coerced, &[17u32]);
|
|
|
|
|
|
|
|
let trait_unified = loop {
|
|
|
|
break if true {
|
|
|
|
break Default::default()
|
|
|
|
} else {
|
|
|
|
break [13, 14]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
assert_eq!(trait_unified, [0, 0]);
|
|
|
|
|
|
|
|
let trait_unified_2 = loop {
|
|
|
|
if false {
|
|
|
|
break [String::from("Hello")]
|
|
|
|
} else {
|
|
|
|
break Default::default()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
assert_eq!(trait_unified_2, [""]);
|
|
|
|
|
|
|
|
let trait_unified_3 = loop {
|
|
|
|
break if false {
|
|
|
|
break [String::from("Hello")]
|
|
|
|
} else {
|
|
|
|
["Yes".into()]
|
|
|
|
};
|
|
|
|
};
|
|
|
|
assert_eq!(trait_unified_3, ["Yes"]);
|
|
|
|
|
|
|
|
let regular_break = loop {
|
|
|
|
if true {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
break break Default::default();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(regular_break, ());
|
|
|
|
|
|
|
|
let regular_break_2 = loop {
|
|
|
|
if true {
|
|
|
|
break Default::default();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(regular_break_2, ());
|
|
|
|
|
|
|
|
let regular_break_3 = loop {
|
|
|
|
break if true {
|
|
|
|
Default::default()
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(regular_break_3, ());
|
|
|
|
|
|
|
|
let regular_break_4 = loop {
|
|
|
|
break ();
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
assert_eq!(regular_break_4, ());
|
|
|
|
|
|
|
|
let regular_break_5 = loop {
|
|
|
|
break;
|
|
|
|
break ();
|
|
|
|
};
|
|
|
|
assert_eq!(regular_break_5, ());
|
|
|
|
|
|
|
|
let nested_break_value = 'outer2: loop {
|
|
|
|
let _a: u32 = 'inner: loop {
|
|
|
|
if true {
|
|
|
|
break 'outer2 "hello";
|
|
|
|
} else {
|
|
|
|
break 'inner 17;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
panic!();
|
|
|
|
};
|
|
|
|
assert_eq!(nested_break_value, "hello");
|
2017-02-16 01:28:59 -06:00
|
|
|
|
|
|
|
let break_from_while_cond = loop {
|
|
|
|
'inner_loop: while break 'inner_loop {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
break 123;
|
|
|
|
};
|
|
|
|
assert_eq!(break_from_while_cond, 123);
|
|
|
|
|
|
|
|
let break_from_while_to_outer = 'outer_loop: loop {
|
|
|
|
while break 'outer_loop 567 {
|
|
|
|
panic!("from_inner");
|
|
|
|
}
|
|
|
|
panic!("from outer");
|
|
|
|
};
|
|
|
|
assert_eq!(break_from_while_to_outer, 567);
|
2017-08-08 10:28:09 -05:00
|
|
|
|
|
|
|
let rust = true;
|
|
|
|
let value = loop {
|
|
|
|
break rust;
|
|
|
|
};
|
|
|
|
assert!(value);
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 17:15:06 -05:00
|
|
|
}
|