Skip to content

Commit b0fd67b

Browse files
committed
Auto merge of #23691 - richo:dedup-typeorigin-mergable, r=eddyb
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
2 parents d4ba1ca + c193fe4 commit b0fd67b

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

src/librustc/middle/infer/error_reporting.rs

+15-29
Original file line numberDiff line numberDiff line change
@@ -357,23 +357,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
357357
}
358358
};
359359

360-
let message_root_str = match trace.origin {
361-
infer::Misc(_) => "mismatched types",
362-
infer::MethodCompatCheck(_) => "method not compatible with trait",
363-
infer::ExprAssignable(_) => "mismatched types",
364-
infer::RelateTraitRefs(_) => "mismatched traits",
365-
infer::RelateSelfType(_) => "mismatched types",
366-
infer::RelateOutputImplTypes(_) => "mismatched types",
367-
infer::MatchExpressionArm(_, _) => "match arms have incompatible types",
368-
infer::IfExpression(_) => "if and else have incompatible types",
369-
infer::IfExpressionWithNoElse(_) => "if may be missing an else clause",
370-
infer::RangeExpression(_) => "start and end of range have incompatible types",
371-
infer::EquatePredicate(_) => "equality predicate not satisfied",
372-
};
373-
374360
span_err!(self.tcx.sess, trace.origin.span(), E0308,
375361
"{}: {} ({})",
376-
message_root_str,
362+
trace.origin,
377363
expected_found_str,
378364
ty::type_err_to_str(self.tcx, terr));
379365

@@ -1495,38 +1481,38 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
14951481
infer::Subtype(ref trace) => {
14961482
let desc = match trace.origin {
14971483
infer::Misc(_) => {
1498-
format!("types are compatible")
1484+
"types are compatible"
14991485
}
15001486
infer::MethodCompatCheck(_) => {
1501-
format!("method type is compatible with trait")
1487+
"method type is compatible with trait"
15021488
}
15031489
infer::ExprAssignable(_) => {
1504-
format!("expression is assignable")
1490+
"expression is assignable"
15051491
}
15061492
infer::RelateTraitRefs(_) => {
1507-
format!("traits are compatible")
1493+
"traits are compatible"
15081494
}
15091495
infer::RelateSelfType(_) => {
1510-
format!("self type matches impl self type")
1496+
"self type matches impl self type"
15111497
}
15121498
infer::RelateOutputImplTypes(_) => {
1513-
format!("trait type parameters matches those \
1514-
specified on the impl")
1499+
"trait type parameters matches those \
1500+
specified on the impl"
15151501
}
15161502
infer::MatchExpressionArm(_, _) => {
1517-
format!("match arms have compatible types")
1503+
"match arms have compatible types"
15181504
}
15191505
infer::IfExpression(_) => {
1520-
format!("if and else have compatible types")
1506+
"if and else have compatible types"
15211507
}
15221508
infer::IfExpressionWithNoElse(_) => {
1523-
format!("if may be missing an else clause")
1509+
"if may be missing an else clause"
15241510
}
15251511
infer::RangeExpression(_) => {
1526-
format!("start and end of range have compatible types")
1512+
"start and end of range have compatible types"
15271513
}
15281514
infer::EquatePredicate(_) => {
1529-
format!("equality where clause is satisfied")
1515+
"equality where clause is satisfied"
15301516
}
15311517
};
15321518

@@ -1666,8 +1652,8 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
16661652
infer::RelateRegionParamBound(span) => {
16671653
self.tcx.sess.span_note(
16681654
span,
1669-
&format!("...so that the declared lifetime parameter bounds \
1670-
are satisfied"));
1655+
"...so that the declared lifetime parameter bounds \
1656+
are satisfied");
16711657
}
16721658
infer::SafeDestructor(span) => {
16731659
self.tcx.sess.span_note(

src/librustc/middle/infer/mod.rs

+25
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use middle::ty::replace_late_bound_regions;
2929
use middle::ty::{self, Ty};
3030
use middle::ty_fold::{TypeFolder, TypeFoldable};
3131
use std::cell::{RefCell};
32+
use std::fmt;
3233
use std::rc::Rc;
3334
use syntax::ast;
3435
use syntax::codemap;
@@ -128,6 +129,30 @@ pub enum TypeOrigin {
128129
EquatePredicate(Span),
129130
}
130131

132+
impl TypeOrigin {
133+
fn as_str(&self) -> &'static str {
134+
match self {
135+
&TypeOrigin::Misc(_) |
136+
&TypeOrigin::RelateSelfType(_) |
137+
&TypeOrigin::RelateOutputImplTypes(_) |
138+
&TypeOrigin::ExprAssignable(_) => "mismatched types",
139+
&TypeOrigin::RelateTraitRefs(_) => "mismatched traits",
140+
&TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait",
141+
&TypeOrigin::MatchExpressionArm(_, _) => "match arms have incompatible types",
142+
&TypeOrigin::IfExpression(_) => "if and else have incompatible types",
143+
&TypeOrigin::IfExpressionWithNoElse(_) => "if may be missing an else clause",
144+
&TypeOrigin::RangeExpression(_) => "start and end of range have incompatible types",
145+
&TypeOrigin::EquatePredicate(_) => "equality predicate not satisfied",
146+
}
147+
}
148+
}
149+
150+
impl fmt::Display for TypeOrigin {
151+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error> {
152+
fmt::Display::fmt(self.as_str(), f)
153+
}
154+
}
155+
131156
/// See `error_reporting.rs` for more details
132157
#[derive(Clone, Debug)]
133158
pub enum ValuePairs<'tcx> {

0 commit comments

Comments
 (0)