Skip to content

Commit 9e64ce1

Browse files
committed
Parse try blocks with the try keyword instead of do catch placeholder
1 parent 1c90609 commit 9e64ce1

31 files changed

+123
-276
lines changed

src/librustc_mir/borrow_check/nll/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@ fn dump_mir_results<'a, 'gcx, 'tcx>(
312312
);
313313

314314
// Also dump the inference graph constraints as a graphviz file.
315-
let _: io::Result<()> = do catch {
315+
let _: io::Result<()> = try_block! {
316316
let mut file =
317317
pretty::create_dump_file(infcx.tcx, "regioncx.all.dot", None, "nll", &0, source)?;
318318
regioncx.dump_graphviz_raw_constraints(&mut file)?;
319319
};
320320

321321
// Also dump the inference graph constraints as a graphviz file.
322-
let _: io::Result<()> = do catch {
322+
let _: io::Result<()> = try_block! {
323323
let mut file =
324324
pretty::create_dump_file(infcx.tcx, "regioncx.scc.dot", None, "nll", &0, source)?;
325325
regioncx.dump_graphviz_scc_constraints(&mut file)?;

src/librustc_mir/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
2121
#![feature(slice_sort_by_cached_key)]
2222
#![feature(box_patterns)]
2323
#![feature(box_syntax)]
24-
#![feature(catch_expr)]
2524
#![feature(crate_visibility_modifier)]
2625
#![feature(const_fn)]
2726
#![feature(core_intrinsics)]
@@ -61,6 +60,14 @@ extern crate rustc_apfloat;
6160
extern crate byteorder;
6261
extern crate core;
6362

63+
// Once we can use edition 2018 in the compiler,
64+
// replace this with real try blocks.
65+
macro_rules! try_block {
66+
($($inside:tt)*) => (
67+
(||{ ::std::ops::Try::from_ok({ $($inside)* }) })()
68+
)
69+
}
70+
6471
mod diagnostics;
6572

6673
mod borrow_check;

src/librustc_mir/util/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
140140
) where
141141
F: FnMut(PassWhere, &mut dyn Write) -> io::Result<()>,
142142
{
143-
let _: io::Result<()> = do catch {
143+
let _: io::Result<()> = try_block! {
144144
let mut file = create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, source)?;
145145
writeln!(file, "// MIR for `{}`", node_path)?;
146146
writeln!(file, "// source = {:?}", source)?;
@@ -156,7 +156,7 @@ fn dump_matched_mir_node<'a, 'gcx, 'tcx, F>(
156156
};
157157

158158
if tcx.sess.opts.debugging_opts.dump_mir_graphviz {
159-
let _: io::Result<()> = do catch {
159+
let _: io::Result<()> = try_block! {
160160
let mut file =
161161
create_dump_file(tcx, "dot", pass_num, pass_name, disambiguator, source)?;
162162
write_mir_fn_graphviz(tcx, source.def_id, mir, &mut file)?;

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4461,7 +4461,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
44614461
// In some cases, blocks have just one exit, but other blocks
44624462
// can be targeted by multiple breaks. This can happen both
44634463
// with labeled blocks as well as when we desugar
4464-
// a `do catch { ... }` expression.
4464+
// a `try { ... }` expression.
44654465
//
44664466
// Example 1:
44674467
//

src/libsyntax/feature_gate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ declare_features! (
330330
// `extern "x86-interrupt" fn()`
331331
(active, abi_x86_interrupt, "1.17.0", Some(40180), None),
332332

333-
// Allows the `catch {...}` expression
333+
// Allows the `try {...}` expression
334334
(active, catch_expr, "1.17.0", Some(31436), None),
335335

336336
// Used to preserve symbols (see llvm.used)
@@ -1735,7 +1735,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17351735
"yield syntax is experimental");
17361736
}
17371737
ast::ExprKind::TryBlock(_) => {
1738-
gate_feature_post!(&self, catch_expr, e.span, "`catch` expression is experimental");
1738+
gate_feature_post!(&self, catch_expr, e.span, "`try` expression is experimental");
17391739
}
17401740
ast::ExprKind::IfLet(ref pats, ..) | ast::ExprKind::WhileLet(ref pats, ..) => {
17411741
if pats.len() > 1 {

src/libsyntax/parse/parser.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,11 +2386,10 @@ impl<'a> Parser<'a> {
23862386
BlockCheckMode::Unsafe(ast::UserProvided),
23872387
attrs);
23882388
}
2389-
if self.is_catch_expr() {
2389+
if self.is_try_block() {
23902390
let lo = self.span;
2391-
assert!(self.eat_keyword(keywords::Do));
2392-
assert!(self.eat_keyword(keywords::Catch));
2393-
return self.parse_catch_expr(lo, attrs);
2391+
assert!(self.eat_keyword(keywords::Try));
2392+
return self.parse_try_block(lo, attrs);
23942393
}
23952394
if self.eat_keyword(keywords::Return) {
23962395
if self.token.can_begin_expr() {
@@ -3452,8 +3451,8 @@ impl<'a> Parser<'a> {
34523451
ExprKind::Async(capture_clause, ast::DUMMY_NODE_ID, body), attrs))
34533452
}
34543453

3455-
/// Parse a `do catch {...}` expression (`do catch` token already eaten)
3456-
fn parse_catch_expr(&mut self, span_lo: Span, mut attrs: ThinVec<Attribute>)
3454+
/// Parse a `try {...}` expression (`try` token already eaten)
3455+
fn parse_try_block(&mut self, span_lo: Span, mut attrs: ThinVec<Attribute>)
34573456
-> PResult<'a, P<Expr>>
34583457
{
34593458
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
@@ -4407,12 +4406,13 @@ impl<'a> Parser<'a> {
44074406
)
44084407
}
44094408

4410-
fn is_catch_expr(&mut self) -> bool {
4411-
self.token.is_keyword(keywords::Do) &&
4412-
self.look_ahead(1, |t| t.is_keyword(keywords::Catch)) &&
4413-
self.look_ahead(2, |t| *t == token::OpenDelim(token::Brace)) &&
4409+
fn is_try_block(&mut self) -> bool {
4410+
self.token.is_keyword(keywords::Try) &&
4411+
self.look_ahead(1, |t| *t == token::OpenDelim(token::Brace)) &&
44144412

4415-
// prevent `while catch {} {}`, `if catch {} {} else {}`, etc.
4413+
self.span.edition() >= Edition::Edition2018 &&
4414+
4415+
// prevent `while try {} {}`, `if try {} {} else {}`, etc.
44164416
!self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL)
44174417
}
44184418

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2380,7 +2380,7 @@ impl<'a> State<'a> {
23802380
self.s.word("?")?
23812381
}
23822382
ast::ExprKind::TryBlock(ref blk) => {
2383-
self.head("do catch")?;
2383+
self.head("try")?;
23842384
self.s.space()?;
23852385
self.print_block_with_attrs(blk, attrs)?
23862386
}

src/libsyntax_pos/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ impl CompilerDesugaringKind {
609609
Symbol::intern(match self {
610610
CompilerDesugaringKind::Async => "async",
611611
CompilerDesugaringKind::QuestionMark => "?",
612-
CompilerDesugaringKind::TryBlock => "do catch",
612+
CompilerDesugaringKind::TryBlock => "try block",
613613
CompilerDesugaringKind::ExistentialReturnType => "existential type",
614614
CompilerDesugaringKind::ForLoop => "for loop",
615615
})

src/test/ui/catch/catch-bad-lifetime.rs renamed to src/test/compile-fail/try-block-bad-lifetime.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

13-
// This test checks that borrows made and returned inside catch blocks are properly constrained
15+
// This test checks that borrows made and returned inside try blocks are properly constrained
1416
pub fn main() {
1517
{
16-
// Test that borrows returned from a catch block must be valid for the lifetime of the
18+
// Test that borrows returned from a try block must be valid for the lifetime of the
1719
// result variable
18-
let _result: Result<(), &str> = do catch {
20+
let _result: Result<(), &str> = try {
1921
let my_string = String::from("");
2022
let my_str: & str = & my_string;
2123
//~^ ERROR `my_string` does not live long enough
@@ -25,10 +27,10 @@ pub fn main() {
2527
}
2628

2729
{
28-
// Test that borrows returned from catch blocks freeze their referent
30+
// Test that borrows returned from try blocks freeze their referent
2931
let mut i = 5;
3032
let k = &mut i;
31-
let mut j: Result<(), &mut i32> = do catch {
33+
let mut j: Result<(), &mut i32> = try {
3234
Err(k) ?;
3335
i = 10; //~ ERROR cannot assign to `i` because it is borrowed
3436
};

src/test/ui/catch/catch-bad-type.rs renamed to src/test/compile-fail/try-block-bad-type.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

1315
pub fn main() {
14-
let res: Result<u32, i32> = do catch {
16+
let res: Result<u32, i32> = try {
1517
Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>` is not satisfied
1618
5
1719
};
1820

19-
let res: Result<i32, i32> = do catch {
21+
let res: Result<i32, i32> = try {
2022
"" //~ ERROR type mismatch
2123
};
2224

23-
let res: Result<i32, i32> = do catch { }; //~ ERROR type mismatch
25+
let res: Result<i32, i32> = try { }; //~ ERROR type mismatch
2426

25-
let res: () = do catch { }; //~ the trait bound `(): std::ops::Try` is not satisfied
27+
let res: () = try { }; //~ the trait bound `(): std::ops::Try` is not satisfied
2628

27-
let res: i32 = do catch { 5 }; //~ ERROR the trait bound `i32: std::ops::Try` is not satisfied
29+
let res: i32 = try { 5 }; //~ ERROR the trait bound `i32: std::ops::Try` is not satisfied
2830
}

src/test/ui/catch/catch-in-match.rs renamed to src/test/compile-fail/try-block-in-match.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

1315
fn main() {
14-
match do catch { false } { _ => {} } //~ ERROR expected expression, found reserved keyword `do`
16+
match try { false } { _ => {} } //~ ERROR expected expression, found keyword `try`
1517
}

src/test/ui/catch/catch-in-while.rs renamed to src/test/compile-fail/try-block-in-while.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

1315
fn main() {
14-
while do catch { false } {} //~ ERROR expected expression, found reserved keyword `do`
16+
while try { false } {} //~ ERROR expected expression, found keyword `try`
1517
}

src/test/ui/catch/catch-maybe-bad-lifetime.rs renamed to src/test/compile-fail/try-block-maybe-bad-lifetime.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

13-
// This test checks that borrows made and returned inside catch blocks are properly constrained
15+
// This test checks that borrows made and returned inside try blocks are properly constrained
1416
pub fn main() {
1517
{
1618
// Test that a borrow which *might* be returned still freezes its referent
1719
let mut i = 222;
18-
let x: Result<&i32, ()> = do catch {
20+
let x: Result<&i32, ()> = try {
1921
Err(())?;
2022
&i
2123
};
@@ -26,7 +28,7 @@ pub fn main() {
2628

2729
{
2830
let x = String::new();
29-
let _y: Result<(), ()> = do catch {
31+
let _y: Result<(), ()> = try {
3032
Err(())?;
3133
::std::mem::drop(x);
3234
};
@@ -38,7 +40,7 @@ pub fn main() {
3840
// its referent
3941
let mut i = 222;
4042
let j;
41-
let x: Result<(), ()> = do catch {
43+
let x: Result<(), ()> = try {
4244
Err(())?;
4345
j = &i;
4446
};

src/test/ui/catch/catch-opt-init.rs renamed to src/test/compile-fail/try-block-opt-init.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

1315
fn use_val<T: Sized>(_x: T) {}
1416

1517
pub fn main() {
1618
let cfg_res;
17-
let _: Result<(), ()> = do catch {
19+
let _: Result<(), ()> = try {
1820
Err(())?;
1921
cfg_res = 5;
2022
Ok::<(), ()>(())?;

src/test/run-pass/issue-45124.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

1315
fn main() {
1416
let mut a = 0;
1517
let () = {
16-
let _: Result<(), ()> = do catch {
18+
let _: Result<(), ()> = try {
1719
let _ = Err(())?;
1820
return
1921
};

src/test/run-pass/catch-expr.rs renamed to src/test/run-pass/try-block.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags: --edition 2018
12+
1113
#![feature(catch_expr)]
1214

1315
struct catch {}
1416

1517
pub fn main() {
16-
let catch_result: Option<_> = do catch {
18+
let catch_result: Option<_> = try {
1719
let x = 5;
1820
x
1921
};
@@ -30,20 +32,20 @@ pub fn main() {
3032
_ => {}
3133
};
3234

33-
let catch_err: Result<_, i32> = do catch {
35+
let catch_err: Result<_, i32> = try {
3436
Err(22)?;
3537
1
3638
};
3739
assert_eq!(catch_err, Err(22));
3840

39-
let catch_okay: Result<i32, i32> = do catch {
41+
let catch_okay: Result<i32, i32> = try {
4042
if false { Err(25)?; }
4143
Ok::<(), i32>(())?;
4244
28
4345
};
4446
assert_eq!(catch_okay, Ok(28));
4547

46-
let catch_from_loop: Result<i32, i32> = do catch {
48+
let catch_from_loop: Result<i32, i32> = try {
4749
for i in 0..10 {
4850
if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; }
4951
}
@@ -52,28 +54,28 @@ pub fn main() {
5254
assert_eq!(catch_from_loop, Err(5));
5355

5456
let cfg_init;
55-
let _res: Result<(), ()> = do catch {
57+
let _res: Result<(), ()> = try {
5658
cfg_init = 5;
5759
};
5860
assert_eq!(cfg_init, 5);
5961

6062
let cfg_init_2;
61-
let _res: Result<(), ()> = do catch {
63+
let _res: Result<(), ()> = try {
6264
cfg_init_2 = 6;
6365
Err(())?;
6466
};
6567
assert_eq!(cfg_init_2, 6);
6668

6769
let my_string = "test".to_string();
68-
let res: Result<&str, ()> = do catch {
70+
let res: Result<&str, ()> = try {
6971
// Unfortunately, deref doesn't fire here (#49356)
7072
&my_string[..]
7173
};
7274
assert_eq!(res, Ok("test"));
7375

74-
let my_opt: Option<_> = do catch { () };
76+
let my_opt: Option<_> = try { () };
7577
assert_eq!(my_opt, Some(()));
7678

77-
let my_opt: Option<_> = do catch { };
79+
let my_opt: Option<_> = try { };
7880
assert_eq!(my_opt, Some(()));
7981
}

0 commit comments

Comments
 (0)