on master help sign in
Fix error message when you have a `number` but need a `String`

The earlier error message always assumed you had a String and needed a number. Now it accounts for both directions.

commit4ed70cc516e39804ec2750cef26761c7597c4879 authorEvan Czaplicki <evancz@users.noreply.github.com> dateSat 18 Aug 2018, 14:18 (8 years ago) parent1f53550
2 files changed+27−31
compiler/src/Reporting/Error/Type.hs+20−28 compiler/src/Type/Error.hs+7−3
compiler/src/Reporting/Error/Type.hs+20−28
@@ -438,20 +438,20 @@ problemToHint problem =
438438 T.MissingArgs _ -> []
439439 T.ReturnMismatch -> []
440440
441− T.BadFlexSuper super _ tipe ->
441+ T.BadFlexSuper direction super _ tipe ->
442442 case tipe of
443− T.Lambda _ _ _ -> badFlexSuper super tipe
443+ T.Lambda _ _ _ -> badFlexSuper direction super tipe
444444 T.Infinite -> []
445445 T.Error -> []
446446 T.FlexVar _ -> []
447447 T.FlexSuper s _ -> badFlexFlexSuper super s
448448 T.RigidVar y -> badRigidVar y (toASuperThing super)
449449 T.RigidSuper s _ -> badRigidSuper s (toASuperThing super)
450− T.Type _ _ _ -> badFlexSuper super tipe
451− T.Record _ _ -> badFlexSuper super tipe
452− T.Unit -> badFlexSuper super tipe
453− T.Tuple _ _ _ -> badFlexSuper super tipe
454− T.Alias _ _ _ _ -> badFlexSuper super tipe
450+ T.Type _ _ _ -> badFlexSuper direction super tipe
451+ T.Record _ _ -> badFlexSuper direction super tipe
452+ T.Unit -> badFlexSuper direction super tipe
453+ T.Tuple _ _ _ -> badFlexSuper direction super tipe
454+ T.Alias _ _ _ _ -> badFlexSuper direction super tipe
455455
456456 T.BadRigidVar x tipe ->
457457 case tipe of
@@ -535,7 +535,7 @@ badDoubleRigid x y =
535535 [ D.toSimpleHint $
536536 "Your type annotation uses `" ++ N.toString x ++ "` and `" ++ N.toString y ++
537537 "` as separate type variables. Your code seems to be saying they are the\
538− \ same though! Maybe they should be the same in your type annotation?\
538+ \ same though. Maybe they should be the same in your type annotation?\
539539 \ Maybe your code uses them in a weird way?"
540540 , D.reflowLink "Read" "type-annotations" "for more advice!"
541541 ]
@@ -554,30 +554,16 @@ toASuperThing super =
554554-- BAD SUPER HINTS
555555
556556
557−badFlexSuper :: T.Super -> T.Type -> [D.Doc]
558−badFlexSuper super tipe =
557+badFlexSuper :: T.Direction -> T.Super -> T.Type -> [D.Doc]
558+badFlexSuper direction super tipe =
559559 case super of
560560 T.Comparable ->
561561 [ D.toSimpleHint "Only ints, floats, chars, strings, lists, and tuples are comparable."
562562 ]
563563
564564 T.Appendable ->
565− case tipe of
566− T.Type home name _ | T.isInt home name ->
567− [ D.toFancyHint ["Try","using",D.green"String.fromInt","to","convert","it","to","a","string?"]
568− ]
569−
570− T.Type home name _ | T.isFloat home name ->
571− [ D.toFancyHint ["Try","using",D.green"String.fromFloat","to","convert","it","to","a","string?"]
572− ]
573−
574− T.FlexSuper T.Number _ ->
575− [ D.toFancyHint ["Try","using",D.green"String.fromInt","to","convert","it","to","a","string?"]
576− ]
577−
578− _ ->
579− [ D.toFancyHint ["Only","strings","and","lists","are","appendable.","Put","it","in",D.green "[]","to","make","it","a","list?"]
580− ]
565+ [ D.toSimpleHint "Only strings and lists are appendable."
566+ ]
581567
582568 T.CompAppend ->
583569 [ D.toSimpleHint "Only strings and lists are both comparable and appendable."
@@ -586,8 +572,14 @@ badFlexSuper super tipe =
586572 T.Number ->
587573 case tipe of
588574 T.Type home name _ | T.isString home name ->
589− [ D.toFancyHint ["Try","using",D.green"String.toInt","to","convert","it","to","a","number?"]
590− ]
575+ case direction of
576+ T.Have ->
577+ [ D.toFancyHint ["Try","using",D.green "String.fromInt","to","convert","it","to","a","string?"]
578+ ]
579+
580+ T.Need ->
581+ [ D.toFancyHint ["Try","using",D.green "String.toInt","to","convert","it","to","an","integer?"]
582+ ]
591583
592584 _ ->
593585 [ D.toFancyHint ["Only",D.green "Int","and",D.green "Float","values","work","as","numbers."]
compiler/src/Type/Error.hs+7−3
@@ -7,6 +7,7 @@ module Type.Error
77 , iteratedDealias
88 , toDoc
99 , Problem(..)
10+ , Direction(..)
1011 , toComparison
1112 , isInt
1213 , isFloat
@@ -177,13 +178,16 @@ data Problem
177178 | AnythingToList
178179 | MissingArgs Int
179180 | ReturnMismatch
180− | BadFlexSuper Super N.Name Type
181+ | BadFlexSuper Direction Super N.Name Type
181182 | BadRigidVar N.Name Type
182183 | BadRigidSuper Super N.Name Type
183184 | FieldTypo N.Name [N.Name]
184185 | FieldsMissing [N.Name]
185186
186187
188+data Direction = Have | Need
189+
190+
187191instance Functor Diff where
188192 fmap func (Diff a b status) =
189193 Diff (func a) (func b) status
@@ -342,10 +346,10 @@ toDiff localizer ctx tipe1 tipe2 =
342346 different doc1 doc2 $
343347 case pair of
344348 (RigidVar x, other) -> Bag.one $ BadRigidVar x other
345− (FlexSuper s x, other) -> Bag.one $ BadFlexSuper s x other
349+ (FlexSuper s x, other) -> Bag.one $ BadFlexSuper Have s x other
346350 (RigidSuper s x, other) -> Bag.one $ BadRigidSuper s x other
347351 (other, RigidVar x) -> Bag.one $ BadRigidVar x other
348− (other, FlexSuper s x) -> Bag.one $ BadFlexSuper s x other
352+ (other, FlexSuper s x) -> Bag.one $ BadFlexSuper Need s x other
349353 (other, RigidSuper s x) -> Bag.one $ BadRigidSuper s x other
350354
351355 (Type home1 name1 [], Type home2 name2 [])