Parse try blocks with the try keyword instead of do catch placeholder

This commit is contained in:
Scott McMurray 2018-07-21 20:59:44 -07:00
parent 1c906093f9
commit 9e64ce1799
31 changed files with 123 additions and 276 deletions

View file

@ -1,39 +0,0 @@
error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/catch-bad-lifetime.rs:33:13
|
LL | let k = &mut i;
| ------ borrow of `i` occurs here
...
LL | i = 10; //~ ERROR cannot assign to `i` because it is borrowed
| ^^^^^^ assignment to borrowed `i` occurs here
LL | };
LL | ::std::mem::drop(k); //~ ERROR use of moved value: `k`
| - borrow later used here
error[E0382]: use of moved value: `k`
--> $DIR/catch-bad-lifetime.rs:35:26
|
LL | Err(k) ?;
| - value moved here
...
LL | ::std::mem::drop(k); //~ ERROR use of moved value: `k`
| ^ value used here after move
|
= note: move occurs because `k` has type `&mut i32`, which does not implement the `Copy` trait
error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/catch-bad-lifetime.rs:36:9
|
LL | let k = &mut i;
| ------ borrow of `i` occurs here
...
LL | i = 40; //~ ERROR cannot assign to `i` because it is borrowed
| ^^^^^^ assignment to borrowed `i` occurs here
LL |
LL | let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic ! ("") };
| - borrow later used here
error: aborting due to 3 previous errors
Some errors occurred: E0382, E0506.
For more information about an error, try `rustc --explain E0382`.

View file

@ -1,42 +0,0 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(catch_expr)]
// This test checks that borrows made and returned inside catch blocks are properly constrained
pub fn main() {
{
// Test that borrows returned from a catch block must be valid for the lifetime of the
// result variable
let _result: Result<(), &str> = do catch {
let my_string = String::from("");
let my_str: & str = & my_string;
//~^ ERROR `my_string` does not live long enough
Err(my_str) ?;
Err("") ?;
};
}
{
// Test that borrows returned from catch blocks freeze their referent
let mut i = 5;
let k = &mut i;
let mut j: Result<(), &mut i32> = do catch {
Err(k) ?;
i = 10; //~ ERROR cannot assign to `i` because it is borrowed
};
::std::mem::drop(k); //~ ERROR use of moved value: `k`
i = 40; //~ ERROR cannot assign to `i` because it is borrowed
let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic ! ("") };
*i_ptr = 50;
}
}

View file

@ -1,44 +0,0 @@
error[E0597]: `my_string` does not live long enough
--> $DIR/catch-bad-lifetime.rs:20:35
|
LL | let my_str: & str = & my_string;
| ^^^^^^^^^ borrowed value does not live long enough
...
LL | };
| - `my_string` dropped here while still borrowed
LL | }
| - borrowed value needs to live until here
error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/catch-bad-lifetime.rs:33:13
|
LL | let k = &mut i;
| - borrow of `i` occurs here
...
LL | i = 10; //~ ERROR cannot assign to `i` because it is borrowed
| ^^^^^^ assignment to borrowed `i` occurs here
error[E0382]: use of moved value: `k`
--> $DIR/catch-bad-lifetime.rs:35:26
|
LL | Err(k) ?;
| - value moved here
...
LL | ::std::mem::drop(k); //~ ERROR use of moved value: `k`
| ^ value used here after move
|
= note: move occurs because `k` has type `&mut i32`, which does not implement the `Copy` trait
error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/catch-bad-lifetime.rs:36:9
|
LL | let k = &mut i;
| - borrow of `i` occurs here
...
LL | i = 40; //~ ERROR cannot assign to `i` because it is borrowed
| ^^^^^^ assignment to borrowed `i` occurs here
error: aborting due to 4 previous errors
Some errors occurred: E0382, E0506, E0597.
For more information about an error, try `rustc --explain E0382`.

View file

@ -1,28 +0,0 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(catch_expr)]
pub fn main() {
let res: Result<u32, i32> = do catch {
Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>` is not satisfied
5
};
let res: Result<i32, i32> = do catch {
"" //~ ERROR type mismatch
};
let res: Result<i32, i32> = do catch { }; //~ ERROR type mismatch
let res: () = do catch { }; //~ the trait bound `(): std::ops::Try` is not satisfied
let res: i32 = do catch { 5 }; //~ ERROR the trait bound `i32: std::ops::Try` is not satisfied
}

View file

@ -1,52 +0,0 @@
error[E0277]: the trait bound `i32: std::convert::From<&str>` is not satisfied
--> $DIR/catch-bad-type.rs:15:9
|
LL | Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>` is not satisfied
| ^^^^^^^^ the trait `std::convert::From<&str>` is not implemented for `i32`
|
= help: the following implementations were found:
<i32 as std::convert::From<bool>>
<i32 as std::convert::From<i16>>
<i32 as std::convert::From<i8>>
<i32 as std::convert::From<u16>>
<i32 as std::convert::From<u8>>
= note: required by `std::convert::From::from`
error[E0271]: type mismatch resolving `<std::result::Result<i32, i32> as std::ops::Try>::Ok == &str`
--> $DIR/catch-bad-type.rs:20:9
|
LL | "" //~ ERROR type mismatch
| ^^ expected i32, found &str
|
= note: expected type `i32`
found type `&str`
error[E0271]: type mismatch resolving `<std::result::Result<i32, i32> as std::ops::Try>::Ok == ()`
--> $DIR/catch-bad-type.rs:23:44
|
LL | let res: Result<i32, i32> = do catch { }; //~ ERROR type mismatch
| ^ expected i32, found ()
|
= note: expected type `i32`
found type `()`
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> $DIR/catch-bad-type.rs:25:28
|
LL | let res: () = do catch { }; //~ the trait bound `(): std::ops::Try` is not satisfied
| ^^^ the trait `std::ops::Try` is not implemented for `()`
|
= note: required by `std::ops::Try::from_ok`
error[E0277]: the trait bound `i32: std::ops::Try` is not satisfied
--> $DIR/catch-bad-type.rs:27:29
|
LL | let res: i32 = do catch { 5 }; //~ ERROR the trait bound `i32: std::ops::Try` is not satisfied
| ^^^^^ the trait `std::ops::Try` is not implemented for `i32`
|
= note: required by `std::ops::Try::from_ok`
error: aborting due to 5 previous errors
Some errors occurred: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.

View file

@ -1,15 +0,0 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(catch_expr)]
fn main() {
match do catch { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `do`
}

View file

@ -1,8 +0,0 @@
error: expected expression, found reserved keyword `do`
--> $DIR/catch-in-match.rs:14:11
|
LL | match do catch { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `do`
| ^^ expected expression
error: aborting due to previous error

View file

@ -1,15 +0,0 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(catch_expr)]
fn main() {
while do catch { false } {} //~ ERROR expected expression, found reserved keyword `do`
}

View file

@ -1,8 +0,0 @@
error: expected expression, found reserved keyword `do`
--> $DIR/catch-in-while.rs:14:11
|
LL | while do catch { false } {} //~ ERROR expected expression, found reserved keyword `do`
| ^^ expected expression
error: aborting due to previous error

View file

@ -1,14 +0,0 @@
error[E0382]: borrow of moved value: `x`
--> $DIR/catch-maybe-bad-lifetime.rs:33:24
|
LL | ::std::mem::drop(x);
| - value moved here
LL | };
LL | println!("{}", x); //~ ERROR use of moved value: `x`
| ^ value borrowed here after move
|
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`.

View file

@ -1,49 +0,0 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(catch_expr)]
// This test checks that borrows made and returned inside catch blocks are properly constrained
pub fn main() {
{
// Test that a borrow which *might* be returned still freezes its referent
let mut i = 222;
let x: Result<&i32, ()> = do catch {
Err(())?;
&i
};
x.ok().cloned();
i = 0; //~ ERROR cannot assign to `i` because it is borrowed
let _ = i;
}
{
let x = String::new();
let _y: Result<(), ()> = do catch {
Err(())?;
::std::mem::drop(x);
};
println!("{}", x); //~ ERROR use of moved value: `x`
}
{
// Test that a borrow which *might* be assigned to an outer variable still freezes
// its referent
let mut i = 222;
let j;
let x: Result<(), ()> = do catch {
Err(())?;
j = &i;
};
i = 0; //~ ERROR cannot assign to `i` because it is borrowed
let _ = i;
}
}

View file

@ -1,33 +0,0 @@
error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/catch-maybe-bad-lifetime.rs:23:9
|
LL | &i
| - borrow of `i` occurs here
...
LL | i = 0; //~ ERROR cannot assign to `i` because it is borrowed
| ^^^^^ assignment to borrowed `i` occurs here
error[E0382]: use of moved value: `x`
--> $DIR/catch-maybe-bad-lifetime.rs:33:24
|
LL | ::std::mem::drop(x);
| - value moved here
LL | };
LL | println!("{}", x); //~ ERROR use of moved value: `x`
| ^ value used here after move
|
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
error[E0506]: cannot assign to `i` because it is borrowed
--> $DIR/catch-maybe-bad-lifetime.rs:45:9
|
LL | j = &i;
| - borrow of `i` occurs here
LL | };
LL | i = 0; //~ ERROR cannot assign to `i` because it is borrowed
| ^^^^^ assignment to borrowed `i` occurs here
error: aborting due to 3 previous errors
Some errors occurred: E0382, E0506.
For more information about an error, try `rustc --explain E0382`.

View file

@ -1,11 +0,0 @@
error[E0381]: borrow of possibly uninitialized variable: `cfg_res`
--> $DIR/catch-opt-init.rs:23:5
|
LL | assert_eq!(cfg_res, 5); //~ ERROR use of possibly uninitialized variable
| ^^^^^^^^^^^^^^^^^^^^^^^ use of possibly uninitialized `cfg_res`
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
error: aborting due to previous error
For more information about this error, try `rustc --explain E0381`.

View file

@ -1,9 +0,0 @@
error[E0381]: use of possibly uninitialized variable: `cfg_res`
--> $DIR/catch-opt-init.rs:23:16
|
LL | assert_eq!(cfg_res, 5); //~ ERROR use of possibly uninitialized variable
| ^^^^^^^ use of possibly uninitialized `cfg_res`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0381`.

View file

@ -8,10 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: --edition 2018
pub fn main() {
let catch_result = do catch { //~ ERROR `catch` expression is experimental
let try_result: Option<_> = try { //~ ERROR `try` expression is experimental
let x = 5;
x
};
assert_eq!(catch_result, 5);
assert_eq!(try_result, Some(5));
}

View file

@ -1,8 +1,8 @@
error[E0658]: `catch` expression is experimental (see issue #31436)
--> $DIR/feature-gate-catch_expr.rs:12:24
error[E0658]: `try` expression is experimental (see issue #31436)
--> $DIR/feature-gate-catch_expr.rs:14:33
|
LL | let catch_result = do catch { //~ ERROR `catch` expression is experimental
| ________________________^
LL | let try_result: Option<_> = try { //~ ERROR `try` expression is experimental
| _________________________________^
LL | | let x = 5;
LL | | x
LL | | };

View file

@ -1,4 +1,4 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@ -8,18 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(catch_expr)]
fn use_val<T: Sized>(_x: T) {}
// compile-flags: --edition 2015
pub fn main() {
let cfg_res;
let _: Result<(), ()> = do catch {
Err(())?;
cfg_res = 5;
Ok::<(), ()>(())?;
use_val(cfg_res);
let try_result: Option<_> = try {
//~^ ERROR expected struct, variant or union type, found macro `try`
let x = 5; //~ ERROR expected identifier, found keyword
x
};
assert_eq!(cfg_res, 5); //~ ERROR use of possibly uninitialized variable
assert_eq!(try_result, Some(5));
}

View file

@ -0,0 +1,18 @@
error: expected identifier, found keyword `let`
--> $DIR/try-block-in-edition2015.rs:16:9
|
LL | let try_result: Option<_> = try {
| --- while parsing this struct
LL | //~^ ERROR expected struct, variant or union type, found macro `try`
LL | let x = 5; //~ ERROR expected identifier, found keyword
| ^^^ expected identifier, found keyword
error[E0574]: expected struct, variant or union type, found macro `try`
--> $DIR/try-block-in-edition2015.rs:14:33
|
LL | let try_result: Option<_> = try {
| ^^^ did you mean `try!(...)`?
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0574`.

View file

@ -8,18 +8,20 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: --edition 2018
#![feature(catch_expr)]
fn foo() -> Option<()> { Some(()) }
fn main() {
let _: Option<f32> = do catch {
let _: Option<f32> = try {
foo()?;
42
//~^ ERROR type mismatch
};
let _: Option<i32> = do catch {
let _: Option<i32> = try {
foo()?;
};
//~^ ERROR type mismatch

View file

@ -1,5 +1,5 @@
error[E0271]: type mismatch resolving `<std::option::Option<f32> as std::ops::Try>::Ok == {integer}`
--> $DIR/catch-block-type-error.rs:18:9
--> $DIR/try-block-type-error.rs:20:9
|
LL | 42
| ^^
@ -11,7 +11,7 @@ LL | 42
found type `{integer}`
error[E0271]: type mismatch resolving `<std::option::Option<i32> as std::ops::Try>::Ok == ()`
--> $DIR/catch-block-type-error.rs:24:5
--> $DIR/try-block-type-error.rs:26:5
|
LL | };
| ^ expected i32, found ()