on master help sign in
Add logic for (::) type errors
commit1281933d11aac79bcffa30615df6ade6c5188228 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 19 Feb 2018, 12:47 (9 years ago) parent20e9c3c
1 file changed+74−17
src/Reporting/Error/Type.hs+74−17
src/Reporting/Error/Type.hs+74−17
@@ -1084,7 +1084,7 @@ opRightToDocs localizer category op tipe expected =
10841084 "==" -> badEquality localizer "==" tipe expected
10851085 "/=" -> badEquality localizer "/=" tipe expected
10861086
1087− "::" -> error "TODO"
1087+ "::" -> badConsRight localizer category tipe expected
10881088 "++" -> badAppendRight localizer category tipe expected
10891089
10901090 "<|" ->
@@ -1133,25 +1133,29 @@ opRightToDocs localizer category op tipe expected =
11331133 )
11341134
11351135 _ ->
1136− EmphRight
1136+ badOpRightFallback localizer category op tipe expected
1137+
1138+
1139+badOpRightFallback :: T.Localizer -> Category -> N.Name -> T.Type -> T.Type -> RightDocs
1140+badOpRightFallback localizer category op tipe expected =
1141+ EmphRight
1142+ (
1143+ H.reflow $
1144+ "The right argument of (" <> N.toString op <> ") is causing problems."
1145+ ,
1146+ typeComparison localizer tipe expected
11371147 (
1138− H.reflow $
1139− "The right argument of (" <> N.toString op <> ") is causing problems."
1148+ toDescription category "The right argument is"
11401149 ,
1141− typeComparison localizer tipe expected
1142− (
1143− toDescription category "The right argument is"
1144− ,
1145− "But (" <> N.toString op <> ") needs the right argument to be:"
1146− ,
1147− [ H.toSimpleHint $
1148− "With operators like (" ++ N.toString op ++ ") I always check the left\
1149− \ side first. If it seems fine, I assume it is correct and check the right\
1150− \ side. So the problem may be in how the left and right arguments interact!"
1151− ]
1152− )
1150+ "But (" <> N.toString op <> ") needs the right argument to be:"
1151+ ,
1152+ [ H.toSimpleHint $
1153+ "With operators like (" ++ N.toString op ++ ") I always check the left\
1154+ \ side first. If it seems fine, I assume it is correct and check the right\
1155+ \ side. So the problem may be in how the left and right arguments interact!"
1156+ ]
11531157 )
1154−
1158+ )
11551159
11561160
11571161isInt :: T.Type -> Bool
@@ -1195,6 +1199,59 @@ isList tipe =
11951199
11961200
11971201
1202+-- BAD CONS
1203+
1204+
1205+badConsRight :: T.Localizer -> Category -> T.Type -> T.Type -> RightDocs
1206+badConsRight localizer category tipe expected =
1207+ case tipe of
1208+ T.Type home1 name1 [actualElement] | T.isList home1 name1 ->
1209+ case expected of
1210+ T.Type home2 name2 [expectedElement] | T.isList home2 name2 ->
1211+ EmphBoth
1212+ (
1213+ H.reflow "I am having trouble with this (::) operator:"
1214+ ,
1215+ typeComparison localizer expectedElement actualElement
1216+ (
1217+ "The left side of (::) is an element of type:"
1218+ ,
1219+ "But the right side is a list filled with elements of type:"
1220+ ,
1221+ case expectedElement of
1222+ T.Type home name [_] | T.isList home name ->
1223+ [ H.toSimpleHint
1224+ "Are you trying to append two lists? The (++) operator\
1225+ \ appends lists, whereas the (::) operator is only for\
1226+ \ adding ONE element to a list."
1227+ ]
1228+
1229+ _ ->
1230+ [ H.reflow
1231+ "Lists need ALL elements to be the same type though."
1232+ ]
1233+ )
1234+ )
1235+
1236+ _ ->
1237+ badOpRightFallback localizer category "::" tipe expected
1238+
1239+ _ ->
1240+ EmphRight
1241+ (
1242+ H.reflow "The (::) operator can only add elements onto lists."
1243+ ,
1244+ loneType localizer tipe expected
1245+ (
1246+ H.reflow $ toDescription category "The right side is"
1247+ ,
1248+ [H.fillSep ["But","(::)","needs","a",H.dullyellow "List","on","the","right."]
1249+ ]
1250+ )
1251+ )
1252+
1253+
1254+
11981255-- BAD APPEND
11991256
12001257