Skip to content

Commit 3f85ff4

Browse files
committed
fix spurious unreachable_code lints for try{} block ok-wrapping
1 parent 1d704e7 commit 3f85ff4

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

src/librustc/hir/lowering/expr.rs

+31-12
Original file line numberDiff line numberDiff line change
@@ -394,30 +394,49 @@ impl LoweringContext<'_> {
394394

395395
fn lower_expr_try_block(&mut self, body: &Block) -> hir::ExprKind {
396396
self.with_catch_scope(body.id, |this| {
397-
let unstable_span = this.mark_span_with_reason(
397+
let mut block = this.lower_block(body, true).into_inner();
398+
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(
398412
DesugaringKind::TryBlock,
399-
body.span,
413+
tail_expr.span,
400414
this.allow_try_trait.clone(),
401415
);
402-
let mut block = this.lower_block(body, true).into_inner();
403-
let tail = block.expr.take().map_or_else(
404-
|| this.expr_unit(this.sess.source_map().end_point(unstable_span)),
405-
|x: P<hir::Expr>| x.into_inner(),
416+
417+
let ok_wrapped_span = this.mark_span_with_reason(
418+
DesugaringKind::TryBlock,
419+
tail_expr.span,
420+
None
406421
);
407-
block.expr = Some(this.wrap_in_try_constructor(sym::from_ok, tail, unstable_span));
422+
423+
block.expr = Some(this.wrap_in_try_constructor(
424+
sym::from_ok, from_ok_span, tail_expr, ok_wrapped_span));
425+
408426
hir::ExprKind::Block(P(block), None)
409427
})
410428
}
411429

412430
fn wrap_in_try_constructor(
413431
&mut self,
414432
method: Symbol,
415-
e: hir::Expr,
416-
unstable_span: Span,
433+
method_span: Span,
434+
expr: hir::Expr,
435+
overall_span: Span,
417436
) -> P<hir::Expr> {
418437
let path = &[sym::ops, sym::Try, method];
419-
let from_err = P(self.expr_std_path(unstable_span, path, None, ThinVec::new()));
420-
P(self.expr_call(e.span, from_err, hir_vec![e]))
438+
let constructor = P(self.expr_std_path(method_span, path, None, ThinVec::new()));
439+
P(self.expr_call(overall_span, constructor, hir_vec![expr]))
421440
}
422441

423442
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {
@@ -1258,7 +1277,7 @@ impl LoweringContext<'_> {
12581277
self.expr_call_std_path(try_span, from_path, hir_vec![err_expr])
12591278
};
12601279
let from_err_expr =
1261-
self.wrap_in_try_constructor(sym::from_error, from_expr, unstable_span);
1280+
self.wrap_in_try_constructor(sym::from_error, unstable_span, from_expr, try_span);
12621281
let thin_attrs = ThinVec::from(attrs);
12631282
let catch_scope = self.catch_scopes.last().map(|x| *x);
12641283
let ret_expr = if let Some(catch_node) = catch_scope {

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ pub struct Block {
861861
pub span: Span,
862862
/// If true, then there may exist `break 'a` values that aim to
863863
/// break out of this block early.
864-
/// Used by `'label: {}` blocks and by `catch` statements.
864+
/// Used by `'label: {}` blocks and by `try {}` blocks.
865865
pub targeted_by_break: bool,
866866
}
867867

src/librustc_typeck/check/expr.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::util::nodemap::FxHashMap;
1818
use crate::astconv::AstConv as _;
1919

2020
use errors::{Applicability, DiagnosticBuilder, pluralise};
21+
use syntax_pos::hygiene::DesugaringKind;
2122
use syntax::ast;
2223
use syntax::symbol::{Symbol, kw, sym};
2324
use syntax::source_map::Span;
@@ -147,8 +148,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
147148
debug!(">> type-checking: expr={:?} expected={:?}",
148149
expr, expected);
149150

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),
161+
};
162+
150163
// Warn for expressions after diverging siblings.
151-
self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
164+
if !is_try_block_generated_expr {
165+
self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
166+
}
152167

153168
// Hide the outer diverging and has_errors flags.
154169
let old_diverges = self.diverges.get();
@@ -159,13 +174,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
159174
let ty = self.check_expr_kind(expr, expected, needs);
160175

161176
// Warn for non-block expressions with diverging children.
162-
match expr.node {
163-
ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {},
164-
ExprKind::Call(ref callee, _) =>
165-
self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
166-
ExprKind::MethodCall(_, ref span, _) =>
167-
self.warn_if_unreachable(expr.hir_id, *span, "call"),
168-
_ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
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+
}
169186
}
170187

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

0 commit comments

Comments
 (0)