mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-12 20:16:49 +00:00
Implement catch
This commit is contained in:
parent
647fd4ff77
commit
27da80324c
47
src/expr.rs
47
src/expr.rs
@ -253,8 +253,16 @@ fn format_expr(expr: &ast::Expr,
|
||||
context.config.max_width(),
|
||||
shape)
|
||||
}
|
||||
// FIXME(#1537)
|
||||
ast::ExprKind::Catch(..) => unimplemented!(),
|
||||
ast::ExprKind::Catch(ref block) => {
|
||||
if let rewrite @ Some(_) = try_one_line_block(context, shape, "do catch ", block) {
|
||||
return rewrite;
|
||||
}
|
||||
// 9 = `do catch `
|
||||
let budget = shape.width.checked_sub(9).unwrap_or(0);
|
||||
Some(format!("{}{}",
|
||||
"do catch ",
|
||||
try_opt!(block.rewrite(&context, Shape::legacy(budget, shape.indent)))))
|
||||
}
|
||||
};
|
||||
match (attr_rw, expr_rw) {
|
||||
(Some(attr_str), Some(expr_str)) => {
|
||||
@ -268,6 +276,22 @@ fn format_expr(expr: &ast::Expr,
|
||||
}
|
||||
}
|
||||
|
||||
fn try_one_line_block(context: &RewriteContext,
|
||||
shape: Shape,
|
||||
prefix: &str,
|
||||
block: &ast::Block)
|
||||
-> Option<String> {
|
||||
if is_simple_block(block, context.codemap) {
|
||||
let expr_shape = Shape::legacy(shape.width - prefix.len(), shape.indent);
|
||||
let expr_str = try_opt!(block.stmts[0].rewrite(context, expr_shape));
|
||||
let result = format!("{}{{ {} }}", prefix, expr_str);
|
||||
if result.len() <= shape.width && !result.contains('\n') {
|
||||
return Some(result);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
|
||||
rhs: &RHS,
|
||||
prefix: &str,
|
||||
@ -622,9 +646,7 @@ fn rewrite_closure(capture: ast::CaptureBy,
|
||||
// means we must re-format.
|
||||
let block_shape = shape.block().with_max_width(context.config);
|
||||
let block_str = try_opt!(block.rewrite(&context, block_shape));
|
||||
Some(format!("{} {}",
|
||||
prefix,
|
||||
try_opt!(block_str.rewrite(context, block_shape))))
|
||||
Some(format!("{} {}", prefix, block_str))
|
||||
}
|
||||
}
|
||||
|
||||
@ -689,24 +711,13 @@ impl Rewrite for ast::Block {
|
||||
} else {
|
||||
"unsafe ".to_owned()
|
||||
};
|
||||
|
||||
if is_simple_block(self, context.codemap) && prefix.len() < shape.width {
|
||||
let expr_str =
|
||||
self.stmts[0].rewrite(context,
|
||||
Shape::legacy(shape.width - prefix.len(),
|
||||
shape.indent));
|
||||
let expr_str = try_opt!(expr_str);
|
||||
let result = format!("{}{{ {} }}", prefix, expr_str);
|
||||
if result.len() <= shape.width && !result.contains('\n') {
|
||||
return Some(result);
|
||||
}
|
||||
if let result @ Some(_) = try_one_line_block(context, shape, &prefix, self) {
|
||||
return result;
|
||||
}
|
||||
|
||||
prefix
|
||||
}
|
||||
ast::BlockCheckMode::Default => {
|
||||
visitor.last_pos = self.span.lo;
|
||||
|
||||
String::new()
|
||||
}
|
||||
};
|
||||
|
27
tests/source/catch.rs
Normal file
27
tests/source/catch.rs
Normal file
@ -0,0 +1,27 @@
|
||||
#![feature(catch_expr)]
|
||||
|
||||
fn main() {
|
||||
let x = do catch {
|
||||
foo()?
|
||||
};
|
||||
|
||||
let x = do catch /* Invisible comment */ { foo()? };
|
||||
|
||||
let x = do catch {
|
||||
unsafe { foo()? }
|
||||
};
|
||||
|
||||
let y = match (do catch {
|
||||
foo()?
|
||||
}) {
|
||||
_ => (),
|
||||
};
|
||||
|
||||
do catch {
|
||||
foo()?;
|
||||
};
|
||||
|
||||
do catch {
|
||||
// Regular do catch block
|
||||
};
|
||||
}
|
21
tests/target/catch.rs
Normal file
21
tests/target/catch.rs
Normal file
@ -0,0 +1,21 @@
|
||||
#![feature(catch_expr)]
|
||||
|
||||
fn main() {
|
||||
let x = do catch { foo()? };
|
||||
|
||||
let x = do catch /* Invisible comment */ { foo()? };
|
||||
|
||||
let x = do catch { unsafe { foo()? } };
|
||||
|
||||
let y = match (do catch { foo()? }) {
|
||||
_ => (),
|
||||
};
|
||||
|
||||
do catch {
|
||||
foo()?;
|
||||
};
|
||||
|
||||
do catch {
|
||||
// Regular do catch block
|
||||
};
|
||||
}
|
@ -109,8 +109,10 @@ fn foo() {
|
||||
|
||||
fn issue1405() {
|
||||
open_raw_fd(fd, b'r').and_then(|file| {
|
||||
Capture::new_raw(None, |_, err| unsafe { raw::pcap_fopen_offline(file, err) })
|
||||
});
|
||||
Capture::new_raw(None, |_, err| unsafe {
|
||||
raw::pcap_fopen_offline(file, err)
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn issue1466() {
|
||||
|
@ -326,11 +326,10 @@ fn issue1371() {
|
||||
sfEvtGainedFocus => GainedFocus,
|
||||
sfEvtTextEntered => {
|
||||
TextEntered {
|
||||
unicode:
|
||||
unsafe {
|
||||
::std::char::from_u32((*event.text.as_ref()).unicode)
|
||||
.expect("Invalid unicode encountered on TextEntered event")
|
||||
},
|
||||
unicode: unsafe {
|
||||
::std::char::from_u32((*event.text.as_ref()).unicode)
|
||||
.expect("Invalid unicode encountered on TextEntered event")
|
||||
},
|
||||
}
|
||||
}
|
||||
sfEvtKeyPressed => {
|
||||
|
Loading…
Reference in New Issue
Block a user