Skip to content

Commit 2560646

Browse files
Diagnostic: "if let arm with incompatible type"
1 parent 153c533 commit 2560646

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

src/librustc/middle/infer/error_reporting.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,11 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
491491
self.check_and_note_conflicting_crates(terr, trace.origin.span());
492492

493493
match trace.origin {
494-
infer::MatchExpressionArm(_, arm_span) =>
495-
self.tcx.sess.span_note(arm_span, "match arm with an incompatible type"),
494+
infer::MatchExpressionArm(_, arm_span, source) => match source {
495+
hir::MatchSource::IfLetDesugar{..} =>
496+
self.tcx.sess.span_note(arm_span, "`if let` arm with an incompatible type"),
497+
_ => self.tcx.sess.span_note(arm_span, "match arm with an incompatible type"),
498+
},
496499
_ => ()
497500
}
498501
}
@@ -1659,7 +1662,7 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
16591662
"trait type parameters matches those \
16601663
specified on the impl"
16611664
}
1662-
infer::MatchExpressionArm(_, _) => {
1665+
infer::MatchExpressionArm(_, _, _) => {
16631666
"match arms have compatible types"
16641667
}
16651668
infer::IfExpression(_) => {

src/librustc/middle/infer/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub enum TypeOrigin {
135135
RelateOutputImplTypes(Span),
136136

137137
// Computing common supertype in the arms of a match expression
138-
MatchExpressionArm(Span, Span),
138+
MatchExpressionArm(Span, Span, hir::MatchSource),
139139

140140
// Computing common supertype in an if expression
141141
IfExpression(Span),
@@ -159,7 +159,10 @@ impl TypeOrigin {
159159
&TypeOrigin::ExprAssignable(_) => "mismatched types",
160160
&TypeOrigin::RelateTraitRefs(_) => "mismatched traits",
161161
&TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait",
162-
&TypeOrigin::MatchExpressionArm(_, _) => "match arms have incompatible types",
162+
&TypeOrigin::MatchExpressionArm(_, _, source) => match source {
163+
hir::MatchSource::IfLetDesugar{..} => "`if let` arms have incompatible types",
164+
_ => "match arms have incompatible types",
165+
},
163166
&TypeOrigin::IfExpression(_) => "if and else have incompatible types",
164167
&TypeOrigin::IfExpressionWithNoElse(_) => "if may be missing an else clause",
165168
&TypeOrigin::RangeExpression(_) => "start and end of range have incompatible types",
@@ -1534,7 +1537,7 @@ impl TypeOrigin {
15341537
RelateTraitRefs(span) => span,
15351538
RelateSelfType(span) => span,
15361539
RelateOutputImplTypes(span) => span,
1537-
MatchExpressionArm(match_span, _) => match_span,
1540+
MatchExpressionArm(match_span, _, _) => match_span,
15381541
IfExpression(span) => span,
15391542
IfExpressionWithNoElse(span) => span,
15401543
RangeExpression(span) => span,

src/librustc_typeck/check/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ pub fn check_match<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
499499
result_ty,
500500
),
501501
_ => (
502-
infer::MatchExpressionArm(expr.span, arm.body.span),
502+
infer::MatchExpressionArm(expr.span, arm.body.span, match_src),
503503
result_ty,
504504
bty,
505505
),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
if let Some(b) = None { //~ ERROR: `if let` arms have incompatible types
13+
()
14+
} else { //~ NOTE: `if let` arm with an incompatible type
15+
1
16+
};
17+
}

0 commit comments

Comments
 (0)