on master help sign in
Add a rough implementation for pattern type errors
commit5342b58ae84f5ab1d21fd208ef4884e4ade8677a authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 9 Feb 2018, 22:49 (9 years ago) parentd7e3cd7
1 file changed+183−27
src/Reporting/Error/Type.hs+183−27
src/Reporting/Error/Type.hs+183−27
@@ -168,14 +168,195 @@ toReport source localizer err =
168168 toExprReport source localizer region category actualType expected
169169
170170 BadPattern region category tipe expected ->
171− error "TODO" region category tipe expected
171+ toPatternReport source localizer region category tipe expected
172172
173173 InfiniteType overallType ->
174174 error "TODO" source localizer overallType
175175
176176
177177
178−-- HELPERS
178+-- TO PATTERN REPORT
179+
180+
181+toPatternReport :: Code.Source -> T.Localizer -> R.Region -> PCategory -> T.Type -> PExpected T.Type -> Report.Report
182+toPatternReport source localizer patternRegion category tipe expected =
183+ Report.Report "TYPE MISMATCH" patternRegion [] $
184+ case expected of
185+ PNoExpectation expectedType ->
186+ Report.toCodeSnippet source patternRegion Nothing $
187+ (
188+ "This pattern is being used in an unexpected way:"
189+ ,
190+ patternTypeComparison localizer tipe expectedType
191+ (
192+ toPatternDescription category "It is"
193+ ,
194+ "But it needs to match values of type:"
195+ ,
196+ []
197+ )
198+ )
199+
200+ PFromContext region context expectedType ->
201+ Report.toCodeSnippet source region (Just patternRegion) $
202+ case context of
203+ PTypedArg name index ->
204+ (
205+ H.reflow $
206+ "The " <> H.ordinalize (Index.toHuman index) <> " argument to `" <> N.toString name <> "` is weird."
207+ ,
208+ patternTypeComparison localizer tipe expectedType
209+ (
210+ toPatternDescription category $
211+ "The argument is a pattern that matches"
212+ ,
213+ "But the type annotation on `" <> N.toString name <> "` says the " <> H.ordinalize (Index.toHuman index) <> " argument should be:"
214+ ,
215+ []
216+ )
217+ )
218+
219+ PCaseMatch index ->
220+ if index == Index.first then
221+ (
222+ H.reflow $
223+ "The 1st pattern in this `case` causing a mismatch:"
224+ ,
225+ patternTypeComparison localizer tipe expectedType
226+ (
227+ toPatternDescription category "The first pattern is trying to match"
228+ ,
229+ "But it is supposed to match an expression of type:"
230+ ,
231+ [ H.reflow $
232+ "Is the pattern the problem? Or is it the expression you are trying to match on?"
233+ ]
234+ )
235+ )
236+ else
237+ (
238+ H.reflow $
239+ "The " <> H.ordinalize (Index.toHuman index) <> " pattern in this `case` does not match the previous ones."
240+ ,
241+ patternTypeComparison localizer tipe expectedType
242+ (
243+ toPatternDescription category $
244+ "The " <> H.ordinalize (Index.toHuman index) <> " pattern is trying to match"
245+ ,
246+ "But all the previous patterns match values of type:"
247+ ,
248+ []
249+ )
250+ )
251+
252+ PCtorArg name index ->
253+ (
254+ H.reflow $
255+ "The " <> H.ordinalize (Index.toHuman index) <> " argument to `" <> N.toString name <> "` is weird."
256+ ,
257+ patternTypeComparison localizer tipe expectedType
258+ (
259+ toPatternDescription category "It is trying to match"
260+ ,
261+ "But `" <> N.toString name <> "` needs its " <> H.ordinalize (Index.toHuman index) <> " argument to be:"
262+ ,
263+ []
264+ )
265+ )
266+
267+ PListEntry index ->
268+ (
269+ H.reflow $
270+ "The " <> H.ordinalize (Index.toHuman index) <> " pattern in this list does not match all the previous ones:"
271+ ,
272+ patternTypeComparison localizer tipe expectedType
273+ (
274+ toPatternDescription category $
275+ "The " <> H.ordinalize (Index.toHuman index) <> " pattern is trying to match"
276+ ,
277+ "But all the previous patterns in the list are:"
278+ ,
279+ [ H.toSimpleHint $
280+ "Everything in the list needs to be the same type of value.\
281+ \ This way you never run into unexpected values partway through.\
282+ \ To mix different types in a single list, create a \"union type\" as\
283+ \ described in: <http://guide.elm-lang.org/types/union_types.html>"
284+ ]
285+ )
286+ )
287+
288+ PTail ->
289+ (
290+ H.reflow $
291+ "The pattern after (::) is causing issues."
292+ ,
293+ patternTypeComparison localizer tipe expectedType
294+ (
295+ toPatternDescription category $
296+ "The pattern after (::) is trying to match"
297+ ,
298+ "But it needs to match lists like this:"
299+ ,
300+ []
301+ )
302+ )
303+
304+
305+
306+-- PATTERN HELPERS
307+
308+
309+patternTypeComparison :: T.Localizer -> T.Type -> T.Type -> ( String, String, [H.Doc] ) -> H.Doc
310+patternTypeComparison localizer actual expected ( iAmSeeing, insteadOf, soHereAreSomeThoughts ) =
311+ let
312+ (actualDoc, expectedDoc) =
313+ error "TODO diff" localizer actual expected
314+ in
315+ H.stack $
316+ [ H.reflow iAmSeeing
317+ , H.indent 4 actualDoc
318+ , H.reflow insteadOf
319+ , H.indent 4 expectedDoc
320+ ]
321+ ++ soHereAreSomeThoughts
322+
323+
324+toPatternDescription :: PCategory -> String -> String
325+toPatternDescription category iAmTryingToMatch =
326+ iAmTryingToMatch <>
327+ case category of
328+ PRecord -> " record values of type:"
329+ PUnit -> " unit values:"
330+ PTuple -> " tuples of type:"
331+ PList -> " lists of type:"
332+ PCtor name -> " `" <> N.toString name <> "` values of type:"
333+ PInt -> " integers:"
334+ PStr -> " strings:"
335+ PChr -> " characters:"
336+
337+
338+
339+-- EXPR HELPERS
340+
341+
342+typeComparison :: T.Localizer -> T.Type -> T.Type -> ( String, String, [H.Doc] ) -> H.Doc
343+typeComparison localizer actual expected ( iAmSeeing, insteadOf, soHereAreSomeThoughts ) =
344+ let
345+ (actualDoc, expectedDoc) =
346+ error "TODO diff" localizer actual expected
347+ in
348+ H.stack $
349+ [ H.reflow iAmSeeing
350+ , H.indent 4 actualDoc
351+ , H.reflow insteadOf
352+ , H.indent 4 expectedDoc
353+ ]
354+ ++ soHereAreSomeThoughts
355+
356+
357+indentType :: T.Localizer -> T.Type -> H.Doc
358+indentType localizer tipe =
359+ H.indent 4 (H.dullyellow (T.toDoc localizer RT.None tipe))
179360
180361
181362toDescription :: Category -> String -> String
@@ -1069,28 +1250,3 @@ badEquality localizer op tipe expectedType =
10691250 ]
10701251 )
10711252 )
1072−
1073−
1074−
1075−-- DIFF TYPES
1076−
1077−
1078−typeComparison :: T.Localizer -> T.Type -> T.Type -> ( String, String, [H.Doc] ) -> H.Doc
1079−typeComparison localizer actual expected ( iAmSeeing, insteadOf, soHereAreSomeThoughts ) =
1080− let
1081− (actualDoc, expectedDoc) =
1082− error "TODO diff" localizer actual expected
1083− in
1084− H.stack $
1085− [ H.reflow iAmSeeing
1086− , H.indent 4 actualDoc
1087− , H.reflow insteadOf
1088− , H.indent 4 expectedDoc
1089− ]
1090− ++ soHereAreSomeThoughts
1091−
1092−
1093−indentType :: T.Localizer -> T.Type -> H.Doc
1094−indentType localizer tipe =
1095− H.indent 4 (H.dullyellow (T.toDoc localizer RT.None tipe))
1096−