Skip to content

Commit

Permalink
Detect missing 'do' and provide a hint
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPenner committed Aug 30, 2024
1 parent 108ed55 commit 845c4e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
18 changes: 17 additions & 1 deletion parser-typechecker/src/Unison/PrintError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import Unison.Syntax.TermPrinter qualified as TermPrinter
import Unison.Term qualified as Term
import Unison.Type (Type)
import Unison.Type qualified as Type
import Unison.Typechecker qualified as Typechecker
import Unison.Typechecker.Context qualified as C
import Unison.Typechecker.TypeError
import Unison.Typechecker.TypeVar qualified as TypeVar
Expand Down Expand Up @@ -369,7 +370,7 @@ renderTypeError e env src = case e of
Mismatch {..} ->
mconcat
[ Pr.lines
[ "I found a value of type: " <> style Type1 (renderType' env foundLeaf),
[ "I found a value of type: " <> style Type1 (renderType' env foundLeaf),
"where I expected to find: " <> style Type2 (renderType' env expectedLeaf)
],
"\n\n",
Expand All @@ -387,6 +388,7 @@ renderTypeError e env src = case e of
src
[styleAnnotated Type1 foundLeaf]
[styleAnnotated Type2 expectedLeaf],
missingDelayHint,
unitHint,
intLiteralSyntaxTip mismatchSite expectedType,
debugNoteLoc
Expand All @@ -407,6 +409,20 @@ renderTypeError e env src = case e of
debugSummary note
]
where
missingDelayHint = case Typechecker.isMismatchMissingDelay foundType expectedType of
Nothing -> ""
Just (Left _) ->
Pr.lines
[ "I expected the expression to be delayed, but it was not.",
"Are you missing a `do`?"
]
Just (Right _) ->
Pr.lines
[ "",
"I didn't expect this expression to be delayed, but it was.",
"Are you using a `do` where you don't need one,",
"or are you missing a `()` to force an expression?"
]
unitHintMsg =
"\nHint: Actions within a block must have type "
<> style Type2 (renderType' env expectedLeaf)
Expand Down
13 changes: 13 additions & 0 deletions parser-typechecker/src/Unison/Typechecker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Unison.Typechecker
isEqual,
isSubtype,
fitsScheme,
isMismatchMissingDelay,
Env (..),
Notes (..),
Resolution (..),
Expand Down Expand Up @@ -38,6 +39,7 @@ import Data.Text qualified as Text
import Data.Tuple qualified as Tuple
import Unison.ABT qualified as ABT
import Unison.Blank qualified as B
import Unison.Builtin.Decls qualified as BuiltinDecls
import Unison.Codebase.BuiltinAnnotation (BuiltinAnnotation)
import Unison.Name qualified as Name
import Unison.Prelude
Expand All @@ -48,6 +50,7 @@ import Unison.Syntax.Name qualified as Name (unsafeParseText, unsafeParseVar)
import Unison.Term (Term)
import Unison.Term qualified as Term
import Unison.Type (Type)
import Unison.Type qualified as Type
import Unison.Typechecker.Context qualified as Context
import Unison.Typechecker.TypeLookup qualified as TL
import Unison.Typechecker.TypeVar qualified as TypeVar
Expand Down Expand Up @@ -405,3 +408,13 @@ wellTyped ppe env term = go <$> runResultT (synthesize ppe Context.PatternMatchC
-- `forall a b . a -> b -> a` to be different types
-- equals :: Var v => Type v -> Type v -> Bool
-- equals t1 t2 = isSubtype t1 t2 && isSubtype t2 t1

-- | Checks if the mismatch between two types is due to a missing delay, if so returns a tag for which type is
-- missing the delay
isMismatchMissingDelay :: (Var v) => Type v loc -> Type v loc -> Maybe (Either (Type v loc) (Type v loc))
isMismatchMissingDelay typeA typeB
| isSubtype (Type.arrow () (Type.ref () BuiltinDecls.unitRef) (typeA $> ())) (typeB $> ()) =
Just (Left typeA)
| isSubtype (ABT.tm (ABT.tm (Type.Ref BuiltinDecls.unitRef) `Type.Arrow` (typeB $> ()))) (typeA $> ()) =
Just (Right typeB)
| otherwise = Nothing
8 changes: 7 additions & 1 deletion unison-cli/src/Unison/LSP/FileAnalysis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import Unison.Syntax.Name qualified as Name
import Unison.Syntax.Parser qualified as Parser
import Unison.Syntax.TypePrinter qualified as TypePrinter
import Unison.Term qualified as Term
import Unison.Typechecker qualified as Typechecker
import Unison.Typechecker.Context qualified as Context
import Unison.Typechecker.TypeError qualified as TypeError
import Unison.UnisonFile qualified as UF
Expand Down Expand Up @@ -224,7 +225,12 @@ analyseNotes codebase fileUri ppe src notes = do
Result.TypeError errNote@(Context.ErrorNote {cause}) -> do
let typeErr = TypeError.typeErrorFromNote errNote
ranges = case typeErr of
TypeError.Mismatch {mismatchSite} -> leafNodeRanges "mismatch" mismatchSite
TypeError.Mismatch {mismatchSite, foundType, expectedType}
| -- If it's a delay mismatch, the error is likely with the block definition (e.g. missing 'do') so we highlight the whole block.
Just _ <- Typechecker.isMismatchMissingDelay foundType expectedType ->
singleRange $ ABT.annotation mismatchSite
-- Otherwise we highlight the leafe nodes of the block
| otherwise -> leafNodeRanges "mismatch" mismatchSite
TypeError.BooleanMismatch {mismatchSite} -> leafNodeRanges "mismatch" mismatchSite
TypeError.ExistentialMismatch {mismatchSite} -> leafNodeRanges "mismatch" mismatchSite
TypeError.FunctionApplication {f} -> singleRange $ ABT.annotation f
Expand Down

0 comments on commit 845c4e8

Please sign in to comment.