Skip to content

Commit 35bf1ae

Browse files
committed
Auto merge of #52602 - scottmcm:tryblock-expr, r=nikomatsakis
Implement try block expressions I noticed that `try` wasn't a keyword yet in Rust 2018, so... ~~Fix​es #52604 That was fixed by PR #53135 cc #31436 #50412
2 parents 827e57c + 0095471 commit 35bf1ae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+320
-224
lines changed

src/doc/unstable-book/src/language-features/catch-expr.md renamed to src/doc/unstable-book/src/language-features/try-blocks.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
# `catch_expr`
1+
# `try_blocks`
22

33
The tracking issue for this feature is: [#31436]
44

55
[#31436]: https://github.com/rust-lang/rust/issues/31436
66

77
------------------------
88

9-
The `catch_expr` feature adds support for a `catch` expression. The `catch`
10-
expression creates a new scope one can use the `?` operator in.
9+
The `try_blocks` feature adds support for `try` blocks. A `try`
10+
block creates a new scope one can use the `?` operator in.
1111

12-
```rust
13-
#![feature(catch_expr)]
12+
```rust,ignore
13+
// This code needs the 2018 edition
14+
15+
#![feature(try_blocks)]
1416
1517
use std::num::ParseIntError;
1618
17-
let result: Result<i32, ParseIntError> = do catch {
19+
let result: Result<i32, ParseIntError> = try {
1820
"1".parse::<i32>()?
1921
+ "2".parse::<i32>()?
2022
+ "3".parse::<i32>()?
2123
};
2224
assert_eq!(result, Ok(6));
2325
24-
let result: Result<i32, ParseIntError> = do catch {
26+
let result: Result<i32, ParseIntError> = try {
2527
"1".parse::<i32>()?
2628
+ "foo".parse::<i32>()?
2729
+ "3".parse::<i32>()?

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,10 +3613,10 @@ impl<'a> LoweringContext<'a> {
36133613
hir::LoopSource::Loop,
36143614
)
36153615
}),
3616-
ExprKind::Catch(ref body) => {
3616+
ExprKind::TryBlock(ref body) => {
36173617
self.with_catch_scope(body.id, |this| {
36183618
let unstable_span =
3619-
this.allow_internal_unstable(CompilerDesugaringKind::Catch, body.span);
3619+
this.allow_internal_unstable(CompilerDesugaringKind::TryBlock, body.span);
36203620
let mut block = this.lower_block(body, true).into_inner();
36213621
let tail = block.expr.take().map_or_else(
36223622
|| {

src/librustc/ich/impls_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
412412
QuestionMark,
413413
ExistentialReturnType,
414414
ForLoop,
415-
Catch
415+
TryBlock
416416
});
417417

418418
impl_stable_hash_for!(enum ::syntax_pos::FileName {

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
#![feature(trace_macros)]
6666
#![feature(trusted_len)]
6767
#![feature(vec_remove_item)]
68-
#![feature(catch_expr)]
6968
#![feature(step_trait)]
7069
#![feature(integer_atomics)]
7170
#![feature(test)]

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)]
@@ -63,6 +62,14 @@ extern crate rustc_apfloat;
6362
extern crate byteorder;
6463
extern crate core;
6564

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

6875
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
@@ -4471,7 +4471,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
44714471
// In some cases, blocks have just one exit, but other blocks
44724472
// can be targeted by multiple breaks. This can happen both
44734473
// with labeled blocks as well as when we desugar
4474-
// a `do catch { ... }` expression.
4474+
// a `try { ... }` expression.
44754475
//
44764476
// Example 1:
44774477
//

src/libsyntax/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ impl Expr {
987987
ExprKind::Match(..) => ExprPrecedence::Match,
988988
ExprKind::Closure(..) => ExprPrecedence::Closure,
989989
ExprKind::Block(..) => ExprPrecedence::Block,
990-
ExprKind::Catch(..) => ExprPrecedence::Catch,
990+
ExprKind::TryBlock(..) => ExprPrecedence::TryBlock,
991991
ExprKind::Async(..) => ExprPrecedence::Async,
992992
ExprKind::Assign(..) => ExprPrecedence::Assign,
993993
ExprKind::AssignOp(..) => ExprPrecedence::AssignOp,
@@ -1108,8 +1108,8 @@ pub enum ExprKind {
11081108
/// created during lowering cannot be made the parent of any other
11091109
/// preexisting defs.
11101110
Async(CaptureBy, NodeId, P<Block>),
1111-
/// A catch block (`catch { ... }`)
1112-
Catch(P<Block>),
1111+
/// A try block (`try { ... }`)
1112+
TryBlock(P<Block>),
11131113

11141114
/// An assignment (`a = foo()`)
11151115
Assign(P<Expr>, P<Expr>),

src/libsyntax/feature_gate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ declare_features! (
333333
// `extern "x86-interrupt" fn()`
334334
(active, abi_x86_interrupt, "1.17.0", Some(40180), None),
335335

336-
// Allows the `catch {...}` expression
337-
(active, catch_expr, "1.17.0", Some(31436), None),
336+
// Allows the `try {...}` expression
337+
(active, try_blocks, "1.29.0", Some(31436), None),
338338

339339
// Used to preserve symbols (see llvm.used)
340340
(active, used, "1.18.0", Some(40289), None),
@@ -1734,8 +1734,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
17341734
e.span,
17351735
"yield syntax is experimental");
17361736
}
1737-
ast::ExprKind::Catch(_) => {
1738-
gate_feature_post!(&self, catch_expr, e.span, "`catch` expression is experimental");
1737+
ast::ExprKind::TryBlock(_) => {
1738+
gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
17391739
}
17401740
ast::ExprKind::IfLet(ref pats, ..) | ast::ExprKind::WhileLet(ref pats, ..) => {
17411741
if pats.len() > 1 {

0 commit comments

Comments
 (0)