From 385b5a3a7d2d96701ebabd7a48a6332a212d9c99 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Thu, 19 Mar 2015 14:49:28 -0700 Subject: [PATCH 1/3] infer: Move TypeOrigin formatting onto it's enum This doesn't actually solve the issue that prompted this, at: https://github.com/rust-lang/rust/blob/master/src/librustc/session/mod.rs#L262-271 But skimming the cfg it appears that all type information has been discarded long before that point. --- src/librustc/middle/infer/error_reporting.rs | 16 +--------------- src/librustc/middle/infer/mod.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/librustc/middle/infer/error_reporting.rs b/src/librustc/middle/infer/error_reporting.rs index 1ca56596a0147..590099b596612 100644 --- a/src/librustc/middle/infer/error_reporting.rs +++ b/src/librustc/middle/infer/error_reporting.rs @@ -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)); diff --git a/src/librustc/middle/infer/mod.rs b/src/librustc/middle/infer/mod.rs index 835964828d419..5ad37b0df6271 100644 --- a/src/librustc/middle/infer/mod.rs +++ b/src/librustc/middle/infer/mod.rs @@ -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; @@ -128,6 +129,25 @@ pub enum TypeOrigin { EquatePredicate(Span), } +impl fmt::Display for TypeOrigin { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error> { + let msg = match self { + &TypeOrigin::Misc(_) => "mismatched types", + &TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait", + &TypeOrigin::ExprAssignable(_) => "mismatched types", + &TypeOrigin::RelateTraitRefs(_) => "mismatched traits", + &TypeOrigin::RelateSelfType(_) => "mismatched types", + &TypeOrigin::RelateOutputImplTypes(_) => "mismatched types", + &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", + }; + fmt::Display::fmt(msg, f) + } +} + /// See `error_reporting.rs` for more details #[derive(Clone, Debug)] pub enum ValuePairs<'tcx> { From e15bebfefa34e155101663f2e110903911c37492 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Tue, 24 Mar 2015 20:55:02 -0700 Subject: [PATCH 2/3] infer: Refactor Display impl --- src/librustc/middle/infer/mod.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/librustc/middle/infer/mod.rs b/src/librustc/middle/infer/mod.rs index 5ad37b0df6271..c25862f41c4c6 100644 --- a/src/librustc/middle/infer/mod.rs +++ b/src/librustc/middle/infer/mod.rs @@ -129,22 +129,27 @@ pub enum TypeOrigin { EquatePredicate(Span), } -impl fmt::Display for TypeOrigin { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error> { - let msg = match self { - &TypeOrigin::Misc(_) => "mismatched types", - &TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait", +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::RelateSelfType(_) => "mismatched types", - &TypeOrigin::RelateOutputImplTypes(_) => "mismatched types", + &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", - }; - fmt::Display::fmt(msg, f) + } + } +} + +impl fmt::Display for TypeOrigin { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(),fmt::Error> { + fmt::Display::fmt(self.as_str(), f) } } From c193fe4f3c5a1e325ff5491b62094b7bf7036de7 Mon Sep 17 00:00:00 2001 From: Richo Healey Date: Tue, 24 Mar 2015 20:59:17 -0700 Subject: [PATCH 3/3] infer: Drop pointless format! calls --- src/librustc/middle/infer/error_reporting.rs | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/librustc/middle/infer/error_reporting.rs b/src/librustc/middle/infer/error_reporting.rs index 590099b596612..80e8ed47d3052 100644 --- a/src/librustc/middle/infer/error_reporting.rs +++ b/src/librustc/middle/infer/error_reporting.rs @@ -1481,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" } }; @@ -1652,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(