Skip to content

Commit 65251ea

Browse files
committed
address review comments
1 parent c60c0cb commit 65251ea

File tree

3 files changed

+37
-39
lines changed

3 files changed

+37
-39
lines changed

src/librustc/hir/lowering/expr.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -392,36 +392,34 @@ impl LoweringContext<'_> {
392392
)
393393
}
394394

395+
/// Desugar `try { <stmts>; <expr> }` into `{ <stmts>; ::std::ops::Try::from_ok(<expr>) }`,
396+
/// `try { <stmts>; }` into `{ <stmts>; ::std::ops::Try::from_ok(()) }`
397+
/// and save the block id to use it as a break target for desugaring of the `?` operator.
395398
fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind {
396399
self.with_catch_scope(body.id, |this| {
397400
let mut block = this.lower_block(body, true).into_inner();
398401

399-
let tail_expr = block.expr.take().map_or_else(
400-
|| {
401-
let unit_span = this.mark_span_with_reason(
402-
DesugaringKind::TryBlock,
403-
this.sess.source_map().end_point(body.span),
404-
None
405-
);
406-
this.expr_unit(unit_span)
407-
},
408-
|x: P<hir::Expr>| x.into_inner(),
409-
);
410-
411-
let from_ok_span = this.mark_span_with_reason(
402+
let try_span = this.mark_span_with_reason(
412403
DesugaringKind::TryBlock,
413-
tail_expr.span,
404+
body.span,
414405
this.allow_try_trait.clone(),
415406
);
416407

408+
// Final expression of the block (if present) or `()` with span at the end of block
409+
let tail_expr = block.expr.take().map_or_else(
410+
|| this.expr_unit(this.sess.source_map().end_point(try_span)),
411+
|x: P<hir::Expr>| x.into_inner(),
412+
);
413+
417414
let ok_wrapped_span = this.mark_span_with_reason(
418415
DesugaringKind::TryBlock,
419416
tail_expr.span,
420417
None
421418
);
422419

420+
// `::std::ops::Try::from_ok($tail_expr)`
423421
block.expr = Some(this.wrap_in_try_constructor(
424-
sym::from_ok, from_ok_span, tail_expr, ok_wrapped_span));
422+
sym::from_ok, try_span, tail_expr, ok_wrapped_span));
425423

426424
hir::ExprKind::Block(P(block), None)
427425
})

src/librustc_typeck/check/expr.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
148148
debug!(">> type-checking: expr={:?} expected={:?}",
149149
expr, expected);
150150

151-
// If when desugaring the try block we ok-wrapped an expression that diverges
152-
// (e.g. `try { return }`) then technically the ok-wrapping expression is unreachable.
153-
// But since it is autogenerated code the resulting warning is confusing for the user
154-
// so we want avoid generating it.
155-
// Ditto for the autogenerated `Try::from_ok(())` at the end of e.g. `try { return; }`.
156-
let (is_try_block_ok_wrapped_expr, is_try_block_generated_expr) = match expr.node {
157-
ExprKind::Call(_, ref args) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {
158-
(true, args.len() == 1 && args[0].span.is_desugaring(DesugaringKind::TryBlock))
159-
}
160-
_ => (false, false),
151+
// True if `expr` is a `Try::from_ok(())` that is a result of desugaring a try block
152+
// without the final expr (e.g. `try { return; }`). We don't want to generate an
153+
// unreachable_code lint for it since warnings for autogenerated code are confusing.
154+
let is_try_block_generated_unit_expr = match expr.node {
155+
ExprKind::Call(_, ref args) if expr.span.is_desugaring(DesugaringKind::TryBlock) =>
156+
args.len() == 1 && args[0].span.is_desugaring(DesugaringKind::TryBlock),
157+
158+
_ => false,
161159
};
162160

163161
// Warn for expressions after diverging siblings.
164-
if !is_try_block_generated_expr {
162+
if !is_try_block_generated_unit_expr {
165163
self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
166164
}
167165

@@ -174,15 +172,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
174172
let ty = self.check_expr_kind(expr, expected, needs);
175173

176174
// Warn for non-block expressions with diverging children.
177-
if !is_try_block_ok_wrapped_expr {
178-
match expr.node {
179-
ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {},
180-
ExprKind::Call(ref callee, _) =>
181-
self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
182-
ExprKind::MethodCall(_, ref span, _) =>
183-
self.warn_if_unreachable(expr.hir_id, *span, "call"),
184-
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
185-
}
175+
match expr.node {
176+
ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {},
177+
// If `expr` is a result of desugaring the try block and is an ok-wrapped
178+
// diverging expression (e.g. it arose from desugaring of `try { return }`),
179+
// we skip issuing a warning because it is autogenerated code.
180+
ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {},
181+
ExprKind::Call(ref callee, _) =>
182+
self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
183+
ExprKind::MethodCall(_, ref span, _) =>
184+
self.warn_if_unreachable(expr.hir_id, *span, "call"),
185+
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
186186
}
187187

188188
// Any expression that produces a value of type `!` must have diverged

src/test/ui/try-block/try-block-bad-type.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ LL | let res: Result<i32, i32> = try { };
3232
found type `()`
3333

3434
error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
35-
--> $DIR/try-block-bad-type.rs:17:25
35+
--> $DIR/try-block-bad-type.rs:17:23
3636
|
3737
LL | let res: () = try { };
38-
| ^ the trait `std::ops::Try` is not implemented for `()`
38+
| ^^^ the trait `std::ops::Try` is not implemented for `()`
3939
|
4040
= note: required by `std::ops::Try::from_ok`
4141

4242
error[E0277]: the trait bound `i32: std::ops::Try` is not satisfied
43-
--> $DIR/try-block-bad-type.rs:19:26
43+
--> $DIR/try-block-bad-type.rs:19:24
4444
|
4545
LL | let res: i32 = try { 5 };
46-
| ^ the trait `std::ops::Try` is not implemented for `i32`
46+
| ^^^^^ the trait `std::ops::Try` is not implemented for `i32`
4747
|
4848
= note: required by `std::ops::Try::from_ok`
4949

0 commit comments

Comments
 (0)