A peer-to-peer stack for code collaboration
Modules complain on undefined exports and other error improvements.
James Henri Haydone2fbeebf45d7
diff --git a/src/Radicle.hs b/src/Radicle.hsindex 3474e7f..88d232b 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -42,6 +42,7 @@ module Radicle-- *** LangError, LangError(..), PatternMatchError(..)+ , ModuleError(..), LangErrorData(..), throwErrorHere, Reference(..)diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex 0664eb5..868256a 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -57,6 +57,14 @@ data PatternMatchErrorinstance Serialise PatternMatchError+data ModuleError+ = MissingDeclaration+ | InvalidDeclaration Text Value+ | UndefinedExports Ident [Ident]+ deriving (Eq, Show, Read, Generic)++instance Serialise ModuleError+-- | An error thrown during parsing or evaluating expressions in the language.data LangErrorData r =UnknownIdentifier Ident@@ -71,8 +79,7 @@ data LangErrorData r =-- args| WrongNumberOfArgs Text Int Int| NonHashableKey- | MissingModuleDeclaration- | InvalidModuleDeclaration Text Value+ | ModuleError ModuleError| OtherError Text| ParseError (Par.ParseError Char Void)-- | Raised if @(throw ident value)@ is evaluated. Arguments are@@ -136,8 +143,13 @@ errorDataToValue e = case e of, ("actual", Number $ fromIntegral actual)])NonHashableKey -> makeVal ( "non-hashable-key", [])- MissingModuleDeclaration -> makeVal ( "missing-module-declaration", [])- InvalidModuleDeclaration t v -> makeVal ( "invalid-module-declaration", [("info", String t), ("declaration", v)])+ ModuleError me -> case me of+ MissingDeclaration -> makeVal ( "missing-module-declaration", [])+ InvalidDeclaration t v -> makeVal ( "invalid-module-declaration", [("info", String t), ("declaration", v)])+ UndefinedExports n is -> makeVal ( "undefined-module-exports"+ , [ ("undefined-exports", Vec (Seq.fromList (Atom <$> is)))+ , ("module", Atom n) ]+ )OtherError i -> makeVal( "other-error", [("info", String i)]@@ -731,7 +743,11 @@ createModule = \case(m : forms) -> dom' <- baseEval m >>= metae <- withEnv identity $ traverse_ eval forms *> gets bindingsEnv- let env = envToRadicle $ Env $ Map.restrictKeys (fromEnv e) (Set.fromList (exports m'))+ let exportsSet = Set.fromList (exports m')+ let undefinedExports = Set.difference exportsSet (Map.keysSet (fromEnv e))+ env <- if null undefinedExports+ then pure . envToRadicle . Env $ Map.restrictKeys (fromEnv e) exportsSet+ else throwErrorHere (ModuleError (UndefinedExports (name m') (Set.toList undefinedExports)))let modu = Dict $ Map.fromList[ (Keyword (Ident "module"), Atom (name m')), (Keyword (Ident "env"), env)@@ -739,7 +755,7 @@ createModule = \case]defineAtom (name m') (Just (doc m')) modupure modu- _ -> throwErrorHere MissingModuleDeclaration+ _ -> throwErrorHere (ModuleError MissingDeclaration)wheremeta v@(Dict d) = doname <- modLookup v "module" d "missing `:module` key"@@ -747,11 +763,11 @@ createModule = \caseexports <- modLookup v "exports" d "Missing `:exports` key"case (name, doc, exports) of(Atom n, String ds, Vec es) -> do- is <- traverse isAtom es ?? toLangError (InvalidModuleDeclaration "`:exports` must be a vector of atoms" v)+ is <- traverse isAtom es ?? toLangError (ModuleError (InvalidDeclaration "`:exports` must be a vector of atoms" v))pure $ ModuleMeta n (toList is) ds- _ -> throwErrorHere (InvalidModuleDeclaration "`:module` must be an atom, `:doc` must be a string and `:exports` must be a vector" v)- meta v = throwErrorHere (InvalidModuleDeclaration "must be dict" v)- modLookup v k d e = kwLookup k d ?? toLangError (InvalidModuleDeclaration e v)+ _ -> throwErrorHere (ModuleError (InvalidDeclaration "`:module` must be an atom, `:doc` must be a string and `:exports` must be a vector" v))+ meta v = throwErrorHere (ModuleError (InvalidDeclaration "must be dict" v))+ modLookup v k d e = kwLookup k d ?? toLangError (ModuleError (InvalidDeclaration e v))-- * From/ ToRadiclediff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 159740f..73c859e 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -102,11 +102,16 @@ instance PrettyV r => PrettyV (LangErrorData r) where<+> "Expected:" <+> pretty x<+> "Got:" <+> pretty yNonHashableKey -> "Non-hashable key in dict."- MissingModuleDeclaration -> "Modules must start with a metadata declaration"- InvalidModuleDeclaration t decl -> vsep- [ "Invalid module declaration:" <+> pretty t <> ":"- , indent 2 $ prettyV decl- ]+ ModuleError me -> case me of+ MissingDeclaration -> "Modules must start with a metadata declaration"+ InvalidDeclaration t decl -> vsep+ [ "Invalid module declaration:" <+> pretty t <> ":"+ , indent 2 $ prettyV decl+ ]+ UndefinedExports n is ->+ "Module" <+> prettyV (asValue (Atom n))+ <+> "has exports which are not defined:"+ <+> sep (prettyV . asValue . Atom <$> is)OtherError t -> "Error:" <+> pretty tSpecialForm f t -> "Error using special form" <+> pretty f <> ":" <+> pretty t <> "."ParseError t -> "Parser error:" <+> pretty (parseErrorPretty t)diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 856cd6a..787d806 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -533,6 +533,10 @@ test_eval =(import foo :unqualified)x|]prog `failsWith` UnknownIdentifier [ident|x|]++ , testCase "Modules complain if exports are undefined" $ do+ let prog = [s|(module {:module 'foo :doc "" :exports '[x y]} (def x 0))|]+ prog `failsWith` ModuleError (UndefinedExports [ident|foo|] [[ident|y|]])]wherefailsWith src err = noStack (runPureCode src) @?= Left err