Add duplicate term checking in Unison file parser

This commit is contained in:
Chris Penner 2022-01-11 15:39:43 -06:00
parent bb4fa39ca2
commit 6c8eb09b7e
3 changed files with 29 additions and 0 deletions

View File

@ -38,6 +38,7 @@ when:
library:
source-dirs: src
dependencies:
- pretty-simple
- aeson
- ansi-terminal
- async

View File

@ -37,6 +37,9 @@ import qualified Unison.NamesWithHistory as NamesWithHistory
import qualified Unison.Names as Names
import qualified Unison.Names.ResolutionResult as Names
import qualified Unison.Name as Name
import Debug.Pretty.Simple (pTraceShowM)
import qualified Data.Foldable as Foldable
import qualified Unison.UnisonFile as UF
resolutionFailures :: Ord v => [Names.ResolutionFailure v Ann] -> P v x
resolutionFailures es = P.customFailure (ResolutionFailures es)
@ -123,8 +126,32 @@ file = do
]
uf = UnisonFileId (UF.datasId env) (UF.effectsId env) (terms <> join accessors)
(List.multimap watches)
let dupes = findDuplicateTermsAndConstructors uf
when (not . null $ dupes) $ do
let dupeList :: [(v, [Ann])]
dupeList = Map.toList $ fmap Set.toList dupes
P.customFailure (DuplicateTermNames dupeList)
pTraceShowM uf
pure uf
findDuplicateTermsAndConstructors :: forall v a. (Ord v, Ord a) => UnisonFile v a -> Map v (Set a)
findDuplicateTermsAndConstructors uf = duplicates
where
allConstructors :: [(v, a)]
allConstructors = Map.elems (dataDeclarationsId uf) <> (Map.elems . fmap (fmap DD.toDataDecl) $ (effectDeclarationsId uf))
& Foldable.toList
& fmap snd
& foldMap DD.constructors'
& fmap (\(ann, v, _typ) -> (v, ann))
allTerms :: [(v, a)]
allTerms = UF.terms uf
<&> (\(v, t) -> (v, ABT.annotation t))
merged :: Map v (Set a)
merged = Map.fromListWith (<>) . (fmap . fmap) Set.singleton $ allConstructors <> allTerms
duplicates :: Map v (Set a)
duplicates = Map.filter ((> 1) . Set.size) merged
-- A stanza is either a watch expression like:
-- > 1 + x
-- > z = x + 1

View File

@ -248,6 +248,7 @@ library
, optparse-applicative >=0.16.1.0
, pem
, prelude-extras
, pretty-simple
, primitive
, process
, random >=1.2.0