Skip to content

Make the match arm type mismatch message point to the arm's span #14561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Jun 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn check_match(fcx: &FnCtxt,
result_ty =
infer::common_supertype(
fcx.infcx(),
infer::MatchExpression(expr.span),
infer::MatchExpressionArm(expr.span, arm.body.span),
true, // result_ty is "expected" here
result_ty,
bty);
Expand Down
10 changes: 8 additions & 2 deletions src/librustc/middle/typeck/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
infer::ExprAssignable(_) => "mismatched types",
infer::RelateTraitRefs(_) => "mismatched traits",
infer::RelateSelfType(_) => "mismatched types",
infer::MatchExpression(_) => "match arms have incompatible types",
infer::MatchExpressionArm(_, _) => "match arms have incompatible types",
infer::IfExpression(_) => "if and else have incompatible types",
};

Expand All @@ -356,6 +356,12 @@ impl<'a> ErrorReporting for InferCtxt<'a> {
message_root_str,
expected_found_str,
ty::type_err_to_str(self.tcx, terr)).as_slice());

match trace.origin {
infer::MatchExpressionArm(_, arm_span) =>
self.tcx.sess.span_note(arm_span, "match arm with an incompatible type"),
_ => ()
}
}

fn report_and_explain_type_error(&self,
Expand Down Expand Up @@ -1281,7 +1287,7 @@ impl<'a> ErrorReportingHelpers for InferCtxt<'a> {
infer::RelateSelfType(_) => {
format!("type matches impl")
}
infer::MatchExpression(_) => {
infer::MatchExpressionArm(_, _) => {
format!("match arms have compatible types")
}
infer::IfExpression(_) => {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/typeck/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ pub enum TypeOrigin {
// Relating trait refs when resolving vtables
RelateSelfType(Span),

// Computing common supertype in a match expression
MatchExpression(Span),
// Computing common supertype in the arms of a match expression
MatchExpressionArm(Span, Span),

// Computing common supertype in an if expression
IfExpression(Span),
Expand Down Expand Up @@ -831,7 +831,7 @@ impl TypeOrigin {
Misc(span) => span,
RelateTraitRefs(span) => span,
RelateSelfType(span) => span,
MatchExpression(span) => span,
MatchExpressionArm(match_span, _) => match_span,
IfExpression(span) => span,
}
}
Expand All @@ -853,8 +853,8 @@ impl Repr for TypeOrigin {
RelateSelfType(a) => {
format!("RelateSelfType({})", a.repr(tcx))
}
MatchExpression(a) => {
format!("MatchExpression({})", a.repr(tcx))
MatchExpressionArm(a, b) => {
format!("MatchExpressionArm({}, {})", a.repr(tcx), b.repr(tcx))
}
IfExpression(a) => {
format!("IfExpression({})", a.repr(tcx))
Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/issue-11319.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
match Some(10) {
//~^ ERROR match arms have incompatible types: expected `bool` but found `()`
Some(5) => false,
Some(2) => true,
None => (), //~ NOTE match arm with an incompatible type
_ => true
}
}