Skip to content

Commit

Permalink
Auto merge of #23691 - richo:dedup-typeorigin-mergable, r=eddyb
Browse files Browse the repository at this point in the history
I've started on refactoring the error handling code to avoid the need to reparse generated errors in `span_*`, but would rather land this incrementally as one monolithic PR (and have un-fond memories of merge conflicts from various other monoliths)

r? @eddyb
  • Loading branch information
bors committed Mar 26, 2015
2 parents d4ba1ca + c193fe4 commit b0fd67b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/librustc/middle/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
}
};

let message_root_str = match trace.origin {
infer::Misc(_) => "mismatched types",
infer::MethodCompatCheck(_) => "method not compatible with trait",
infer::ExprAssignable(_) => "mismatched types",
infer::RelateTraitRefs(_) => "mismatched traits",
infer::RelateSelfType(_) => "mismatched types",
infer::RelateOutputImplTypes(_) => "mismatched types",
infer::MatchExpressionArm(_, _) => "match arms have incompatible types",
infer::IfExpression(_) => "if and else have incompatible types",
infer::IfExpressionWithNoElse(_) => "if may be missing an else clause",
infer::RangeExpression(_) => "start and end of range have incompatible types",
infer::EquatePredicate(_) => "equality predicate not satisfied",
};

span_err!(self.tcx.sess, trace.origin.span(), E0308,
"{}: {} ({})",
message_root_str,
trace.origin,
expected_found_str,
ty::type_err_to_str(self.tcx, terr));

Expand Down Expand Up @@ -1495,38 +1481,38 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
infer::Subtype(ref trace) => {
let desc = match trace.origin {
infer::Misc(_) => {
format!("types are compatible")
"types are compatible"
}
infer::MethodCompatCheck(_) => {
format!("method type is compatible with trait")
"method type is compatible with trait"
}
infer::ExprAssignable(_) => {
format!("expression is assignable")
"expression is assignable"
}
infer::RelateTraitRefs(_) => {
format!("traits are compatible")
"traits are compatible"
}
infer::RelateSelfType(_) => {
format!("self type matches impl self type")
"self type matches impl self type"
}
infer::RelateOutputImplTypes(_) => {
format!("trait type parameters matches those \
specified on the impl")
"trait type parameters matches those \
specified on the impl"
}
infer::MatchExpressionArm(_, _) => {
format!("match arms have compatible types")
"match arms have compatible types"
}
infer::IfExpression(_) => {
format!("if and else have compatible types")
"if and else have compatible types"
}
infer::IfExpressionWithNoElse(_) => {
format!("if may be missing an else clause")
"if may be missing an else clause"
}
infer::RangeExpression(_) => {
format!("start and end of range have compatible types")
"start and end of range have compatible types"
}
infer::EquatePredicate(_) => {
format!("equality where clause is satisfied")
"equality where clause is satisfied"
}
};

Expand Down Expand Up @@ -1666,8 +1652,8 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
infer::RelateRegionParamBound(span) => {
self.tcx.sess.span_note(
span,
&format!("...so that the declared lifetime parameter bounds \
are satisfied"));
"...so that the declared lifetime parameter bounds \
are satisfied");
}
infer::SafeDestructor(span) => {
self.tcx.sess.span_note(
Expand Down
25 changes: 25 additions & 0 deletions src/librustc/middle/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use middle::ty::replace_late_bound_regions;
use middle::ty::{self, Ty};
use middle::ty_fold::{TypeFolder, TypeFoldable};
use std::cell::{RefCell};
use std::fmt;
use std::rc::Rc;
use syntax::ast;
use syntax::codemap;
Expand Down Expand Up @@ -128,6 +129,30 @@ pub enum TypeOrigin {
EquatePredicate(Span),
}

impl TypeOrigin {
fn as_str(&self) -> &'static str {
match self {
&TypeOrigin::Misc(_) |
&TypeOrigin::RelateSelfType(_) |
&TypeOrigin::RelateOutputImplTypes(_) |
&TypeOrigin::ExprAssignable(_) => "mismatched types",
&TypeOrigin::RelateTraitRefs(_) => "mismatched traits",
&TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait",
&TypeOrigin::MatchExpressionArm(_, _) => "match arms have incompatible types",
&TypeOrigin::IfExpression(_) => "if and else have incompatible types",
&TypeOrigin::IfExpressionWithNoElse(_) => "if may be missing an else clause",
&TypeOrigin::RangeExpression(_) => "start and end of range have incompatible types",
&TypeOrigin::EquatePredicate(_) => "equality predicate not satisfied",
}
}
}

impl fmt::Display for TypeOrigin {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error> {
fmt::Display::fmt(self.as_str(), f)
}
}

/// See `error_reporting.rs` for more details
#[derive(Clone, Debug)]
pub enum ValuePairs<'tcx> {
Expand Down

0 comments on commit b0fd67b

Please sign in to comment.