@@ -689,9 +689,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
689
689
diag : & mut DiagnosticBuilder < ' tcx > ,
690
690
cause : & ObligationCause < ' tcx > ,
691
691
secondary_span : Option < ( Span , String ) > ,
692
- values : Option < ValuePairs < ' tcx > > ,
692
+ mut values : Option < ValuePairs < ' tcx > > ,
693
693
terr : & TypeError < ' tcx > )
694
694
{
695
+ // For some types of errors, expected-found does not make
696
+ // sense, so just ignore the values we were given.
697
+ match terr {
698
+ TypeError :: CyclicTy ( _) => { values = None ; }
699
+ _ => { }
700
+ }
701
+
695
702
let ( expected_found, exp_found, is_simple_error) = match values {
696
703
None => ( None , None , false ) ,
697
704
Some ( values) => {
@@ -780,17 +787,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
780
787
terr) ;
781
788
782
789
let span = trace. cause . span ;
783
- let failure_str = trace. cause . as_failure_str ( ) ;
784
- let mut diag = match trace . cause . code {
785
- ObligationCauseCode :: IfExpressionWithNoElse => {
790
+ let failure_code = trace. cause . as_failure_code ( terr ) ;
791
+ let mut diag = match failure_code {
792
+ FailureCode :: Error0317 ( failure_str ) => {
786
793
struct_span_err ! ( self . tcx. sess, span, E0317 , "{}" , failure_str)
787
794
}
788
- ObligationCauseCode :: MainFunctionType => {
795
+ FailureCode :: Error0580 ( failure_str ) => {
789
796
struct_span_err ! ( self . tcx. sess, span, E0580 , "{}" , failure_str)
790
797
}
791
- _ => {
798
+ FailureCode :: Error0308 ( failure_str ) => {
792
799
struct_span_err ! ( self . tcx. sess, span, E0308 , "{}" , failure_str)
793
800
}
801
+ FailureCode :: Error0644 ( failure_str) => {
802
+ struct_span_err ! ( self . tcx. sess, span, E0644 , "{}" , failure_str)
803
+ }
794
804
} ;
795
805
self . note_type_err ( & mut diag, & trace. cause , None , Some ( trace. values ) , terr) ;
796
806
diag
@@ -1040,23 +1050,40 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
1040
1050
}
1041
1051
}
1042
1052
1053
+ enum FailureCode {
1054
+ Error0317 ( & ' static str ) ,
1055
+ Error0580 ( & ' static str ) ,
1056
+ Error0308 ( & ' static str ) ,
1057
+ Error0644 ( & ' static str ) ,
1058
+ }
1059
+
1043
1060
impl < ' tcx > ObligationCause < ' tcx > {
1044
- fn as_failure_str ( & self ) -> & ' static str {
1061
+ fn as_failure_code ( & self , terr : & TypeError < ' tcx > ) -> FailureCode {
1062
+ use self :: FailureCode :: * ;
1045
1063
use traits:: ObligationCauseCode :: * ;
1046
1064
match self . code {
1047
- CompareImplMethodObligation { .. } => "method not compatible with trait" ,
1048
- MatchExpressionArm { source, .. } => match source {
1065
+ CompareImplMethodObligation { .. } => Error0308 ( "method not compatible with trait" ) ,
1066
+ MatchExpressionArm { source, .. } => Error0308 ( match source {
1049
1067
hir:: MatchSource :: IfLetDesugar { ..} => "`if let` arms have incompatible types" ,
1050
1068
_ => "match arms have incompatible types" ,
1051
- } ,
1052
- IfExpression => "if and else have incompatible types" ,
1053
- IfExpressionWithNoElse => "if may be missing an else clause" ,
1054
- EquatePredicate => "equality predicate not satisfied" ,
1055
- MainFunctionType => "main function has wrong type" ,
1056
- StartFunctionType => "start function has wrong type" ,
1057
- IntrinsicType => "intrinsic has wrong type" ,
1058
- MethodReceiver => "mismatched method receiver" ,
1059
- _ => "mismatched types" ,
1069
+ } ) ,
1070
+ IfExpression => Error0308 ( "if and else have incompatible types" ) ,
1071
+ IfExpressionWithNoElse => Error0317 ( "if may be missing an else clause" ) ,
1072
+ EquatePredicate => Error0308 ( "equality predicate not satisfied" ) ,
1073
+ MainFunctionType => Error0580 ( "main function has wrong type" ) ,
1074
+ StartFunctionType => Error0308 ( "start function has wrong type" ) ,
1075
+ IntrinsicType => Error0308 ( "intrinsic has wrong type" ) ,
1076
+ MethodReceiver => Error0308 ( "mismatched method receiver" ) ,
1077
+
1078
+ // In the case where we have no more specific thing to
1079
+ // say, also take a look at the error code, maybe we can
1080
+ // tailor to that.
1081
+ _ => match terr {
1082
+ TypeError :: CyclicTy ( ty) if ty. is_closure ( ) || ty. is_generator ( ) =>
1083
+ Error0644 ( "closure/generator type that references itself" ) ,
1084
+ _ =>
1085
+ Error0308 ( "mismatched types" ) ,
1086
+ }
1060
1087
}
1061
1088
}
1062
1089
0 commit comments