Skip to content

Commit

Permalink
Disallow . character in module/type names (#420)
Browse files Browse the repository at this point in the history
* Disallow `.` character in module/type names

* revert unneeded change
  • Loading branch information
pschachte authored Nov 10, 2023
1 parent af6d464 commit f6361d4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ module AST (
emptyInterface, emptyImplementation,
getParams, getPrimParams, getDetism, getProcDef, getProcPrimProto,
mkTempName, updateProcDef, updateProcDefM,
ModSpec, maybeModPrefix, ProcImpln(..), ProcDef(..), procInline, procCallCount,
ModSpec, validModuleName, maybeModPrefix,
ProcImpln(..), ProcDef(..), procInline, procCallCount,
transformModuleProcs,
getProcGlobalFlows,
primImpurity, flagsImpurity, flagsDetism,
Expand Down Expand Up @@ -1699,6 +1700,12 @@ instance Show TypeVarName where
type ModSpec = [Ident]


-- |Check that a module name component is valid (ie, does not contain invalid
-- characters). Currently only period (.) and hash (#) are considered invalid.
validModuleName :: Ident -> Bool
validModuleName = not . any (`elem` ['.','#'])


-- |The uses one module makes of another; first the public imports,
-- then the privates. Each is either Nothing, meaning all exported
-- names are imported, or Just a set of the specific names to import.
Expand Down
24 changes: 21 additions & 3 deletions src/Normalise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ normalise items = do
normaliseItem :: Item -> Compiler ()
normaliseItem (TypeDecl vis (TypeProto name params) mods
(TypeRepresentation rep) items pos) = do
validateModuleName "type" pos name
let items' = RepresentationDecl params mods rep pos : items
unless (List.null params)
$ errmsg pos "types defined by representation cannot have type parameters"
normaliseSubmodule name vis pos items'
normaliseItem (TypeDecl vis (TypeProto name params) mods
(TypeCtors ctorVis ctors) items pos) = do
validateModuleName "type" pos name
let items' = ConstructorDecl ctorVis params mods ctors pos : items
normaliseSubmodule name vis pos items'
normaliseItem (ModuleDecl vis name items pos) =
normaliseItem (ModuleDecl vis name items pos) = do
validateModuleName "module" pos name
normaliseSubmodule name vis pos items
normaliseItem (RepresentationDecl params mods rep pos) = do
updateTypeModifiers mods
Expand All @@ -76,9 +79,12 @@ normaliseItem (ConstructorDecl vis params mods ctors pos) = do
Public -> mapM_ (addConstructor Public . snd) ctors
Private -> mapM_ (uncurry addConstructor) ctors
normaliseItem (ImportMods vis modspecs pos) =
mapM_ (\spec -> addImport spec (importSpec Nothing vis)) modspecs
mapM_ (\spec -> validateModSpec pos spec >>
addImport spec (importSpec Nothing vis)
) modspecs
normaliseItem (ImportItems vis modspec imports pos) =
addImport modspec (importSpec (Just imports) vis)
validateModSpec pos modspec
>> addImport modspec (importSpec (Just imports) vis)
normaliseItem (ImportForeign files _) =
mapM_ addForeignImport files
normaliseItem (ImportForeignLib files _) =
Expand Down Expand Up @@ -277,6 +283,18 @@ completeTypeSCC (CyclicSCC modTypeDefs) = do
mapM_ (uncurry completeType) modTypeDefs


-- |Check that the specified module name is valid, reporting and error if not.
validateModuleName :: String -> OptPos -> Ident -> Compiler ()
validateModuleName what pos name =
unless (validModuleName name)
$ errmsg pos $ "invalid character in " ++ what ++ " name `" ++ name ++ "`"


-- |Check that the specified module name is valid, reporting and error if not.
validateModSpec :: OptPos -> ModSpec -> Compiler ()
validateModSpec pos = mapM_ (validateModuleName "module" pos)


-- | Information about a non-constant constructor
data CtorInfo = CtorInfo {
ctorInfoName :: ProcName, -- ^ this constructor's name
Expand Down
3 changes: 3 additions & 0 deletions test-cases/final-dump/badmodname.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Error detected during preliminary processing of module badmodname
final-dump/badmodname.wybe:6:1: invalid character in module name `a.b`

9 changes: 9 additions & 0 deletions test-cases/final-dump/badmodname.wybe
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module a {
pub module b {
!print("in a.b")
}
}
module `a.b` {
!print("in `a.b`")
}
pass

0 comments on commit f6361d4

Please sign in to comment.