Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More typechecking error message improvements #1308

Merged
merged 17 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restyled by fourmolu (#1309)
Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
restyled-io[bot] and restyled-commits authored Jun 8, 2023
commit 2a0cdfd789cc9f72c19f7ec0bc7675d222d81eba
8 changes: 4 additions & 4 deletions src/Swarm/Language/LSP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import Language.LSP.Server
import Language.LSP.Types (Hover (Hover))
import Language.LSP.Types qualified as J
import Language.LSP.Types.Lens qualified as J
import Language.LSP.VFS (VirtualFile(..), virtualFileText)
import Swarm.Language.Typecheck (ContextualTypeErr(..))
import Language.LSP.VFS (VirtualFile (..), virtualFileText)
import Swarm.Language.LSP.Hover qualified as H
import Swarm.Language.LSP.VarUsage qualified as VU
import Swarm.Language.Parse
import Swarm.Language.Pipeline
import Swarm.Language.Pretty (prettyText)
import Swarm.Language.Syntax (SrcLoc(..))
import Swarm.Language.Syntax (SrcLoc (..))
import Swarm.Language.Typecheck (ContextualTypeErr (..))
import System.IO (stderr)
import Witch

Expand Down Expand Up @@ -62,7 +62,7 @@ lspMain =
diagnosticSourcePrefix :: Text
diagnosticSourcePrefix = "swarm-lsp"

debug :: MonadIO m => Text -> m ()
debug :: (MonadIO m) => Text -> m ()
debug msg = liftIO $ Text.hPutStrLn stderr $ "[swarm-lsp] " <> msg

validateSwarmCode :: J.NormalizedUri -> J.TextDocumentVersion -> Text -> LspM () ()
Expand Down
6 changes: 3 additions & 3 deletions src/Swarm/Language/Pipeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import Witch
-- pipeline. Put a 'Term' in, and get one of these out.
data ProcessedTerm
= ProcessedTerm
-- | The elaborated + type-annotated term, plus types of any embedded definitions
TModule
-- ^ The elaborated + type-annotated term, plus types of any embedded definitions
-- | Requirements of the term
Requirements
-- ^ Requirements of the term
-- | Capability context for any definitions embedded in the term
ReqCtx
-- ^ Capability context for any definitions embedded in the term
deriving (Data, Show, Eq, Generic)

processTermEither :: Text -> Either String ProcessedTerm
Expand Down
34 changes: 17 additions & 17 deletions src/Swarm/Language/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ class PrettyPrec a where
prettyPrec :: Int -> a -> Doc ann -- can replace with custom ann type later if desired

-- | Pretty-print a thing, with a context precedence level of zero.
ppr :: PrettyPrec a => a -> Doc ann
ppr :: (PrettyPrec a) => a -> Doc ann
ppr = prettyPrec 0

-- | Render a pretty-printed document as @Text@.
docToText :: Doc a -> Text
docToText = RT.renderStrict . layoutPretty defaultLayoutOptions

-- | Pretty-print something and render it as @Text@.
prettyText :: PrettyPrec a => a -> Text
prettyText :: (PrettyPrec a) => a -> Text
prettyText = docToText . ppr

-- | Render a pretty-printed document as a @String@.
docToString :: Doc a -> String
docToString = RS.renderString . layoutPretty defaultLayoutOptions

-- | Pretty-print something and render it as a @String@.
prettyString :: PrettyPrec a => a -> String
prettyString :: (PrettyPrec a) => a -> String
prettyString = docToString . ppr

-- | Optionally surround a document with parentheses depending on the
Expand All @@ -77,7 +77,7 @@ data BulletList i = BulletList
, bulletListItems :: [i]
}

instance PrettyPrec i => PrettyPrec (BulletList i) where
instance (PrettyPrec i) => PrettyPrec (BulletList i) where
prettyPrec _ (BulletList hdr items) =
nest 2 . vcat $ hdr : map (("-" <+>) . ppr) items

Expand All @@ -100,14 +100,14 @@ instance PrettyPrec BaseTy where
instance PrettyPrec IntVar where
prettyPrec _ = pretty . mkVarName "u"

instance PrettyPrec (t (Fix t)) => PrettyPrec (Fix t) where
instance (PrettyPrec (t (Fix t))) => PrettyPrec (Fix t) where
prettyPrec p = prettyPrec p . unFix

instance (PrettyPrec (t (UTerm t v)), PrettyPrec v) => PrettyPrec (UTerm t v) where
prettyPrec p (UTerm t) = prettyPrec p t
prettyPrec p (UVar v) = prettyPrec p v

instance PrettyPrec t => PrettyPrec (TypeF t) where
instance (PrettyPrec t) => PrettyPrec (TypeF t) where
prettyPrec _ (TyBaseF b) = ppr b
prettyPrec _ (TyVarF v) = pretty v
prettyPrec p (TySumF ty1 ty2) =
Expand All @@ -131,7 +131,7 @@ instance PrettyPrec UPolytype where
prettyPrec _ (Forall [] t) = ppr t
prettyPrec _ (Forall xs t) = hsep ("∀" : map pretty xs) <> "." <+> ppr t

instance PrettyPrec t => PrettyPrec (Ctx t) where
instance (PrettyPrec t) => PrettyPrec (Ctx t) where
prettyPrec _ Empty = emptyDoc
prettyPrec _ (assocs -> bs) = brackets (hsep (punctuate "," (map prettyBinding bs)))

Expand Down Expand Up @@ -245,10 +245,10 @@ prettyTypeErrText code = docToText . prettyTypeErr code
-- | Format a 'ContextualTypeError' for the user.
prettyTypeErr :: Text -> ContextualTypeErr -> Doc ann
prettyTypeErr code (CTE l tcStack te) =
vcat
[ teLoc <> ppr te
, ppr (BulletList "" tcStack)
]
vcat
[ teLoc <> ppr te
, ppr (BulletList "" tcStack)
]
where
teLoc = case l of
SrcLoc s e -> (showLoc . fst $ getLocRange code (s, e)) <> ": "
Expand All @@ -258,17 +258,17 @@ prettyTypeErr code (CTE l tcStack te) =
instance PrettyPrec TypeErr where
prettyPrec _ (UnifyErr ty1 ty2) =
"Can't unify" <+> ppr ty1 <+> "and" <+> ppr ty2
prettyPrec _ (Mismatch Nothing (getJoin -> (ty1,ty2))) =
prettyPrec _ (Mismatch Nothing (getJoin -> (ty1, ty2))) =
"Type mismatch: expected" <+> ppr ty1 <> ", but got" <+> ppr ty2
prettyPrec _ (Mismatch (Just t) (getJoin -> (ty1,ty2))) =
prettyPrec _ (Mismatch (Just t) (getJoin -> (ty1, ty2))) =
nest 2 . vcat $
[ "Type mismatch:"
, "From context, expected" <+> bquote (ppr t) <+> "to have type" <+> bquote (ppr ty1) <> ","
, "but it actually has type" <+> bquote (ppr ty2)
]
prettyPrec _ (LambdaArgMismatch (getJoin -> (ty1,ty2))) =
prettyPrec _ (LambdaArgMismatch (getJoin -> (ty1, ty2))) =
"Lambda argument has type annotation" <+> ppr ty2 <> ", but expected argument type" <+> ppr ty1
prettyPrec _ (FieldsMismatch (getJoin -> (expFs,actFs))) = fieldMismatchMsg expFs actFs
prettyPrec _ (FieldsMismatch (getJoin -> (expFs, actFs))) = fieldMismatchMsg expFs actFs
prettyPrec _ (EscapedSkolem x) =
"Skolem variable" <+> pretty x <+> "would escape its scope"
prettyPrec _ (UnboundVar x) =
Expand Down Expand Up @@ -309,5 +309,5 @@ instance PrettyPrec LocatedTCFrame where

instance PrettyPrec TCFrame where
prettyPrec _ (TCDef x) = "While checking the definition of" <+> pretty x
prettyPrec _ TCBindL = "While checking the left-hand side of a semicolon"
prettyPrec _ TCBindR = "While checking the right-hand side of a semicolon"
prettyPrec _ TCBindL = "While checking the left-hand side of a semicolon"
prettyPrec _ TCBindR = "While checking the right-hand side of a semicolon"
4 changes: 2 additions & 2 deletions src/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Sourced a = (Source, a)
-- | A "join" where an expected thing meets an actual thing.
data Join a = Join (Source -> a)

instance Show a => Show (Join a) where
instance (Show a) => Show (Join a) where
show (getJoin -> (e, a)) = "(expected: " <> show e <> ", actual: " <> show a <> ")"

type TypeJoin = Join UType
Expand Down Expand Up @@ -213,7 +213,7 @@ instance FreeVars UType where
freeVars ut = fmap S.fromList . lift . lift . lift $ getFreeVars ut

-- | We can also get the free variables of a polytype.
instance FreeVars t => FreeVars (Poly t) where
instance (FreeVars t) => FreeVars (Poly t) where
freeVars (Forall _ t) = freeVars t

-- | We can get the free variables in any polytype in a context.
Expand Down
1 change: 0 additions & 1 deletion test/unit/TestLanguagePipeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ testLanguagePipeline =
)
]
]

where
valid = flip process ""

Expand Down