tilia 0.2.0.0 → 0.2.0.1
raw patch · 10 files changed
+500/−407 lines, 10 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Tilia.Fixity.Interface: showIface :: FilePath -> IO (Maybe Text)
+ Tilia.Fixity.Interface: shownHiFile :: Text -> Maybe HiFile
Files
- AGENTS.md +8/−6
- CHANGELOG.md +9/−0
- src/Tilia/Fixity/Cache.hs +1/−1
- src/Tilia/Fixity/HiFile.hs +2/−2
- src/Tilia/Fixity/Interface.hs +105/−128
- src/Tilia/Fixity/Plan.hs +7/−1
- tests/Tilia/Fixity/HiFileSpec.hs +14/−19
- tests/Tilia/Fixity/InterfaceSpec.hs +157/−233
- tests/Tilia/Fixity/PlanSpec.hs +196/−16
- tilia.cabal +1/−1
AGENTS.md view
@@ -51,12 +51,14 @@ ## Fixities Resolve an import's fixities only among the build plan's library units,-matched by package name and version, through their exposed modules; the-project's own modules count only for the project's own imports. A module-several of the plan's packages hold is read from the first of them, and-no more of GHC's package machinery is modelled. Do not model which-`ghc-pkg` a run should ask, visibility per component, or cache keys finer-than a package's identity.+matched by package name and version, through their exposed modules. The+libraries of the project's own packages are among them, read from their+sources, but a dependency's import reaches them only where no other+package of the plan holds the module. A module several of the plan's+packages hold is read from the first of them, and no more of GHC's+package machinery is modelled. Do not model which `ghc-pkg` a run should+ask, visibility per component, or cache keys finer than a package's+identity. Fix a fixity finding only where a real project, from a Hackage scan or a user's report, shows a false decline or a wrong fixity, and only within
CHANGELOG.md view
@@ -1,3 +1,12 @@+## Tilia 0.2.0.1++* Interface files of GHC 9.8 and older are read with the namespace of+ every name, as newer ones are, so an operator reached through one is no+ longer declined. [PR 619](https://github.com/mrkkrp/tilia/pull/619).+* A dependency's import of a module only the project's own libraries+ hold is read from them, so the operators it brings in are no longer+ declined. [PR 620](https://github.com/mrkkrp/tilia/pull/620).+ ## Tilia 0.2.0.0 ### Settings
src/Tilia/Fixity/Cache.hs view
@@ -80,7 +80,7 @@ -- | Bumped whenever cache format changes. formatVersion :: FilePath-formatVersion = "v4"+formatVersion = "v5" -- | Open, creating the directory if need be. --
src/Tilia/Fixity/HiFile.hs view
@@ -56,8 +56,8 @@ | -- | Built-in syntax, such as @:@, which GHC writes without the module -- that defines it. BuiltInSyntax Namespace OpName- | -- | A name GHC writes as a key outside the table for its series, such- -- as a tuple's, which no fixity depends on.+ | -- | A name no fixity depends on, such as a tuple's, which GHC writes as+ -- a key outside the table for its series or prints bare. Unneeded deriving (Eq, Show)
src/Tilia/Fixity/Interface.hs view
@@ -6,17 +6,20 @@ module Tilia.Fixity.Interface ( Interface (..), readInterface,+ showIface, parseInterface,+ shownHiFile, fromHiFile, ) where import Control.DeepSeq (NFData)+import Data.Bifunctor (first) import Data.ByteString qualified as BS import Data.Char (isUpper) import Data.Map.Strict (Map) import Data.Map.Strict qualified as Map-import Data.Maybe (isNothing, mapMaybe)+import Data.Maybe (listToMaybe, mapMaybe) import Data.Set (Set) import Data.Set qualified as Set import Data.Text (Text)@@ -31,7 +34,7 @@ decodeHiFile, ) import Tilia.Process (readProgramOutput)-import Tilia.Utils (blocksOf, indented, quietly)+import Tilia.Utils (blocksOf, quietly) -- | What an interface says about the operators a module offers. data Interface = Interface@@ -44,8 +47,7 @@ -- | The members it exports with each name, which is what @T(..)@ in an -- import list stands for. interfaceMembers :: Map OpName (Set (Namespace, OpName)),- -- | What it certainly brings into scope, where the interface was decoded- -- rather than read off what @ghc --show-iface@ prints.+ -- | What it certainly brings into scope, where an interface file says. interfaceExports :: Maybe Certain } deriving (Eq, Show, Generic)@@ -54,8 +56,8 @@ -- | Read a module's interface file. ----- The file is decoded directly where it can be, and put to @ghc--- --show-iface@ otherwise.+-- The file is decoded directly where it can be, and read off 'showIface'+-- otherwise. readInterface :: -- | The module the file is supposed to hold. Text ->@@ -66,10 +68,7 @@ quietly Nothing $ decodeHiFile <$> BS.readFile path >>= \case Right hi -> pure (either (const Nothing) Just (fromHiFile modName hi))- Left _ ->- readProgramOutput "ghc" ["--show-iface", path] >>= \case- Nothing -> pure Nothing- Just out -> pure (parseInterface modName out)+ Left _ -> (parseInterface modName =<<) <$> showIface path -- | What a decoded interface file says, if it is this module's. fromHiFile :: Text -> HiFile -> Either Text Interface@@ -126,102 +125,40 @@ BuiltInSyntax namespace op -> Just (Nothing, namespace, op) Unneeded -> Nothing --- | Read what @ghc --show-iface@ printed, if it is this module's interface.------ What it prints does not say which namespace a member is in, so each is--- taken to be in both.+-- | What @ghc --show-iface@ prints for an interface file, with each name+-- annotated with its namespace.+showIface :: FilePath -> IO (Maybe Text)+showIface path = readProgramOutput "ghc" ["--show-iface", path, "-dppr-debug"]++-- | Read what 'showIface' printed, if it is this module's interface. parseInterface :: Text -> Text -> Maybe Interface-parseInterface modName out- | not (any holdsModule (T.lines out)) = Nothing- | otherwise =- Just- Interface- { interfaceDeclares =- namespaced- (typeNamesIn out)- ( Map.restrictKeys- fixities- (foldMap ownExports (sectionsNamed "exports:"))- ),- interfaceReexports =- [ (m, namespace, op)- | (m, op) <- concatMap reexports (sectionsNamed "exports:"),- namespace <- [InTypes, InTerms]- ],- interfaceMembers =- Map.unionsWith- Set.union- (fmap membersIn (sectionsNamed "exports:")),- interfaceExports = Nothing- }+parseInterface modName out =+ either (const Nothing) Just . fromHiFile modName =<< shownHiFile out++-- | What 'showIface' printed, as decoding the file would give it.+shownHiFile :: Text -> Maybe HiFile+shownHiFile out = do+ self <-+ listToMaybe+ [ m+ | l <- T.lines out,+ Just rest <- [T.stripPrefix "interface " l],+ m : _ <- [T.words rest]+ ]+ exports <- traverse exportEntries (sectionsNamed "exports:")+ pure+ HiFile+ { hiModule = self,+ hiExports = concat exports,+ hiFixities =+ mapMaybe+ fixityEntry+ (concatMap (T.splitOn ",") (sectionsNamed "fixities"))+ } where- holdsModule l = case T.words l of- ("interface" : m : _) -> m == modName- _ -> False sectionsNamed name = [body | (heading, body) <- sections out, heading == name]- fixities =- Map.fromListWith- (flip (<>))- [ (op, [fixity])- | (op, fixity) <- concatMap declared (sectionsNamed "fixities")- ]- declared = mapMaybe fixityEntry . T.splitOn ","- ownExports section =- Set.fromList- [OpName name | name <- exportedIn section, isNothing (moduleOf name)]- reexports section =- [ (m, OpName name)- | Just (m, name) <- fmap moduleOf (exportedIn section),- not (T.null name)- ]- exportedIn section =- [ name- | (parent, parentExported, kids) <- exportEntries section,- name <- [parent | parentExported] <> kids- ]- membersIn section =- Map.fromListWith- Set.union- [ ( nameOnly parent,- Set.fromList- [ (namespace, nameOnly kid)- | kid <- kids,- namespace <- [InTypes, InTerms]- ]- )- | (parent, _, kids@(_ : _)) <- exportEntries section- ] --- | The names an interface declares as types.-typeNamesIn :: Text -> Set OpName-typeNamesIn out =- Set.fromList- [ nameOnly (T.dropWhileEnd (== ')') (T.dropWhile (== '(') name))- | l <- T.lines out,- indented l,- (keyword : rest) <- [T.words l],- keyword `elem` (["data", "type", "newtype", "class"] :: [Text]),- name <-- take- 1- (dropWhile (`elem` (["family", "role", "instance"] :: [Text])) rest)- ]---- | Sort declared fixities into the namespaces they govern: of two for one--- name, GHC lists the term's first.-namespaced :: Set OpName -> Map OpName [Fixity] -> Fixities-namespaced types declared =- Map.fromList- [ ((namespace, op), fixity)- | (op, fixities) <- Map.toList declared,- (namespace, fixity) <- zip (governed op fixities) fixities- ]- where- governed op = \case- [_, _] -> [InTerms, InTypes]- _ -> [if Set.member op types then InTypes else InTerms]- -- | Split the output into sections. sections :: Text -> [(Text, Text)] sections out =@@ -230,44 +167,84 @@ let (heading, opening) = T.breakOn " " l ] --- | One entry of a @fixities@ line: @infixl 9 !@ and the like.-fixityEntry :: Text -> Maybe (OpName, Fixity)+-- | One entry of a @fixities@ line: @infixl 9 !{v}@ and the like.+fixityEntry :: Text -> Maybe (Namespace, OpName, Fixity) fixityEntry entry = case T.words entry of- [direction, precedence, op] -> do+ direction : precedence : written@(_ : _) -> do d <- case direction of "infixl" -> Just LeftAssoc "infixr" -> Just RightAssoc "infix" -> Just NoAssoc _ -> Nothing p <- readPrecedence precedence- pure (OpName op, Fixity d p)+ let (op, rest) = T.break (== '{') (T.unwords written)+ namespace <- namespaceOf =<< T.stripSuffix "}" =<< T.stripPrefix "{" rest+ pure (namespace, OpName op, Fixity d p) _ -> Nothing --- | Split an exports section into its entries, keeping the members an entry--- wears in braces with the name they belong to, and whether that name is--- exported itself, which a bar before the braces, as in @T|{...}@, says it--- is not.-exportEntries :: Text -> [(Text, Bool, [Text])]-exportEntries = go+-- | The entries of an exports section, unless one cannot be read.+--+-- An entry with members in braces is a type or class, exported itself+-- unless a bar before the braces, as in @T|{...}@, says it is not.+exportEntries :: Text -> Maybe [HiExport]+exportEntries text = case T.uncons (separated text) of+ Nothing -> Just []+ Just _ -> do+ (parent, rest) <- shownName (separated text)+ (entry, after) <- case T.uncons rest of+ Just ('{', inside) ->+ first (AvailTC parent . (parent :)) <$> members inside+ Just ('|', bar)+ | Just inside <- T.stripPrefix "{" bar ->+ first (AvailTC parent) <$> members inside+ _ -> Just (Avail parent, rest)+ (entry :) <$> exportEntries after where- go text = case T.uncons (T.dropWhile (== ' ') text) of- Nothing -> []- Just _ ->- let trimmed = T.dropWhile (== ' ') text- (name, rest) = T.break (\c -> c == ' ' || c == '{') trimmed- in case T.uncons rest of- Just ('{', inside) ->- let (kids, after) = T.break (== '}') inside- entry = case T.unsnoc (bare name) of- Just (parent, '|') -> (parent, False, T.words kids)- _ -> (bare name, True, T.words kids)- in entry : go (T.drop 1 after)- _ -> (bare name, True, []) : go rest- bare = T.dropWhileEnd (== ',')+ members t = case T.uncons (separated t) of+ Just ('}', after) -> Just ([], after)+ _ -> do+ (kid, rest) <- shownName (separated t)+ first (kid :) <$> members rest+ separated = T.dropWhile (== ' ') --- | An exported name without the module that declared it.-nameOnly :: Text -> OpName-nameOnly t = OpName (maybe t snd (moduleOf t))+-- | One name as 'showIface' prints it, and the text after it.+--+-- GHC 9.6 and older print a record field as its label with its selector in+-- braces and flags after them. A name printed bare, with no namespace, is+-- built-in syntax, such as @[]@ or @(,)@, whose members follow in braces+-- the way a field's selector does.+shownName :: Text -> Maybe (HiName, Text)+shownName t = case T.uncons rest of+ Just ('{', inside)+ | (note, after) <- T.break (\c -> c == '{' || c == '}') inside,+ Just remaining <- T.stripPrefix "}" after -> do+ namespace <- namespaceOf note+ (m, op) <- moduleOf written+ pure (HiName m namespace (OpName op), remaining)+ | T.take 1 written `elem` ["(", "["] -> Just (Unneeded, rest)+ | otherwise -> do+ (selector, after) <- shownName inside+ flagged <- T.stripPrefix "}" after+ case selector of+ HiName m _ _ ->+ pure+ ( HiName m InTerms (OpName written),+ T.dropWhile (\c -> c /= ' ' && c /= '}') flagged+ )+ _ -> Nothing+ _+ | T.null written -> Nothing+ | otherwise -> Just (Unneeded, rest)+ where+ (written, rest) = T.break (\c -> c == ' ' || c == '{' || c == '}') t++-- | The namespace an annotation such as @(w) tc 3c@ gives a name.+namespaceOf :: Text -> Maybe Namespace+namespaceOf note = case filter (not . T.isPrefixOf "(") (T.words note) of+ space : _+ | space == "v" || space == "d" || "fld" `T.isPrefixOf` space -> Just InTerms+ | space == "tc" -> Just InTypes+ _ -> Nothing -- | Split a name into the module that declared it and the name itself. moduleOf :: Text -> Maybe (Text, Text)
src/Tilia/Fixity/Plan.hs view
@@ -225,8 +225,14 @@ resolveModule (Set.insert key visiting) found where -- The first package to hold a module is named, so that the- -- module is read once however it is reached.+ -- module is read once however it is reached. A module no package+ -- of the plan holds is one of the project's own libraries, which+ -- the plan builds its dependencies against. found = case local target of+ Elsewhere package modName+ | isNothing (heldBy package modName holding),+ isNothing (heldBy package modName reexported) ->+ importable Nothing (package, modName) Elsewhere Nothing modName -> Elsewhere (holder modName) modName whereabouts -> whereabouts key = case found of
tests/Tilia/Fixity/HiFileSpec.hs view
@@ -21,7 +21,7 @@ import Test.Hspec import Tilia.Cabal.Plan (BuildPlan)-import Tilia.Fixity (Fixities, Namespace (..), OpName (..))+import Tilia.Fixity (Certain (..), Fixities, OpName (..)) import Tilia.Fixity.HiFile ( HiExport (..), HiFile (..),@@ -29,9 +29,13 @@ decodeHiFile, primopFixities, )-import Tilia.Fixity.Interface (Interface (..), fromHiFile, parseInterface)+import Tilia.Fixity.Interface+ ( Interface (..),+ fromHiFile,+ parseInterface,+ showIface,+ ) import Tilia.Fixity.PackageDb (readInstalledPackages)-import Tilia.Process (readProgramOutput) import Tilia.TestUtils (fingerprint) import Tilia.Utils (inParallel) import Tilia.WithProjectPlan@@ -86,9 +90,7 @@ Just hi | Right d <- fromHiFile m hi, not (Set.null (offered declared m d)) || passesOver hi || sampled m -> do- shown <-- maybe Nothing (parseInterface m)- <$> readProgramOutput "ghc" ["--show-iface", path]+ shown <- (parseInterface m =<<) <$> showIface path let found = case shown of Nothing -> [(m, "ghc --show-iface gave nothing to compare")] Just s -> [(m, x) | x <- disagreements declared m d s]@@ -114,10 +116,7 @@ sampled m = fingerprint (T.unpack m) `mod` 8 == 0 -- | Where a direct reading and the text disagree about what fixities depend--- on, beyond what the text cannot tell.------ A fixity the text puts among terms may be among types all the same: the--- text leaves out the declarations of the types GHC wires in.+-- on. disagreements :: -- | The fixities each module declares, where they can be read. (Text -> Maybe Fixities) ->@@ -129,20 +128,16 @@ Interface -> [Text] disagreements declared m direct shown =- [ "fixities"- | not- ( inTypes (interfaceDeclares shown)- `Map.isSubmapOf` interfaceDeclares direct- )- || byOperator (interfaceDeclares direct)- /= byOperator (interfaceDeclares shown)- ]+ ["fixities" | interfaceDeclares direct /= interfaceDeclares shown] <> [ "offered fixities" | offered declared m direct /= offered declared m shown ] <> ["members" | members direct /= members shown]+ <> ["certain names" | certain direct /= certain shown] where- inTypes = Map.filterWithKey (\(namespace, _) _ -> namespace == InTypes)+ certain i =+ Set.filter ((`Set.member` withFixities) . snd) . certainNames+ <$> interfaceExports i members i = Map.filter (not . Set.null) $ Map.map
tests/Tilia/Fixity/InterfaceSpec.hs view
@@ -1,13 +1,12 @@ {-# LANGUAGE OverloadedStrings #-} --- | Reading what @ghc --show-iface@ prints.+-- | Reading what @ghc --show-iface -dppr-debug@ prints. -- -- The samples below are cut from real output rather than invented, since -- the whole risk here is in the format: this is a pretty-printer's idea of -- an interface, not a documented one. module Tilia.Fixity.InterfaceSpec (spec) where -import Data.List (nub) import Data.Map.Strict qualified as Map import Data.Set qualified as Set import Data.Text (Text)@@ -19,193 +18,144 @@ spec :: Spec spec = do- describe "what a module declares" $ do- it "reads a fixity line" $- declares- "exports:\n !\n !?\n \\\\\nfixities infixl 9 !, infixl 9 !?, infixl 9 \\\\\n"- `shouldBe` [ (OpName "!", Fixity LeftAssoc 9),- (OpName "!?", Fixity LeftAssoc 9),- (OpName "\\\\", Fixity LeftAssoc 9)- ]-- it "reads one wrapped across lines" $- declares- "exports:\n $\n $!\n *>\n ++\n .\n :|\n <$\n\- \fixities infixr 0 $, infixr 0 $!, infixl 4 *>, infixr 5 ++,\n\- \ infixr 9 ., infixr 5 :|, infixl 4 <$\n"- `shouldBe` [ (OpName "$", Fixity RightAssoc 0),- (OpName "$!", Fixity RightAssoc 0),- (OpName "*>", Fixity LeftAssoc 4),- (OpName "++", Fixity RightAssoc 5),- (OpName ".", Fixity RightAssoc 9),- (OpName ":|", Fixity RightAssoc 5),- (OpName "<$", Fixity LeftAssoc 4)- ]-- it "reads every direction, and a name used in backticks" $- declares- "exports:\n div\n ===\n .&&.\nfixities infixl 7 div, infix 4 ===, infixr 1 .&&.\n"- `shouldBe` [ (OpName ".&&.", Fixity RightAssoc 1),- (OpName "===", Fixity NoAssoc 4),- (OpName "div", Fixity LeftAssoc 7)- ]-- it "reads the precedence GHC gives the function arrow" $- declares "exports:\n ->\nfixities infixr -1 ->\n"- `shouldBe` [(OpName "->", Fixity RightAssoc (-1))]-- it "reads it alongside ordinary ones" $- declares "exports:\n ->\n !\nfixities infixr -1 ->, infixl 9 !\n"- `shouldBe` [ (OpName "!", Fixity LeftAssoc 9),- (OpName "->", Fixity RightAssoc (-1))- ]-- it "passes over an entry it cannot read, and keeps the rest" $- declares- "exports:\n ?\n !\nfixities infixl notadigit ?, infixl 9 !, infixl\n"- `shouldBe` [(OpName "!", Fixity LeftAssoc 9)]-- it "says nothing for a module that declares nothing" $- declares "exports:\n member\n" `shouldBe` []-- describe "what a module passes on" $ do- it "names the module an operator was declared in" $- passesOn "exports:\n Data.Aeson.Types.FromJSON..:\n"- `shouldBe` [("Data.Aeson.Types.FromJSON", OpName ".:")]-- it "leaves out what the module declared itself" $- passesOn "exports:\n decode'\n <+>\n" `shouldBe` []-- it "takes the members of a class along with it" $- passesOn- "exports:\n\- \ Data.Aeson.Types.FromJSON.FromJSON{Data.Aeson.Types.FromJSON.parseJSON}\n"- `shouldBe` [ ("Data.Aeson.Types.FromJSON", OpName "FromJSON"),- ("Data.Aeson.Types.FromJSON", OpName "parseJSON")- ]-- it "takes a record field written after a bar, and not its type" $- passesOn- "exports:\n Data.Aeson.Encoding.Internal.Encoding'|{Data.Aeson.Encoding.Internal.fromEncoding}\n"- `shouldBe` [("Data.Aeson.Encoding.Internal", OpName "fromEncoding")]-- it "keeps a type apart from the module holding it" $- passesOn "exports:\n Data.Aeson.Types.Internal.Value\n"- `shouldBe` [("Data.Aeson.Types.Internal", OpName "Value")]-- it "reads an operator that is nothing but a dot" $- passesOn "exports:\n Data.Function..\n"- `shouldBe` [("Data.Function", OpName ".")]-- it "leaves a capitalised name this module declared alone" $- passesOn "exports:\n Value\n" `shouldBe` []-- describe "operators with a vertical bar in them" $ do- -- A bar also marks a type exported without itself, as in T|{...}, so- -- these are read in the same text as that mark.- it "reads one made of bars alone" $- passesOn "exports:\n GHC.Internal.Data.Type.Bool.||\n"- `shouldBe` [("GHC.Internal.Data.Type.Bool", OpName "||")]-- it "reads them among the members of a type exported without itself" $- passesOn- "exports:\n GHC.Internal.Bits.Bits|{GHC.Internal.Bits..&. GHC.Internal.Bits..|. GHC.Internal.Bits.complement}\n"- `shouldBe` [ ("GHC.Internal.Bits", OpName ".&."),- ("GHC.Internal.Bits", OpName ".|."),- ("GHC.Internal.Bits", OpName "complement")- ]-- it "takes them as the members of such a type" $- members- "exports:\n GHC.Internal.Base.Alternative|{GHC.Internal.Base.<|> GHC.Internal.Base.empty}\n"- `shouldBe` [(OpName "Alternative", [OpName "<|>", OpName "empty"])]+ describe "what a module exports" $ do+ it "reads each name with the namespace it is in" $+ exportsOf+ "exports:\n Control.Applicative.optional{v r1}\n GHC.Types.Type{(w) tc 32Q}\n"+ `shouldBe` Just+ [ Avail (HiName "Control.Applicative" InTerms (OpName "optional")),+ Avail (HiName "GHC.Types" InTypes (OpName "Type"))+ ] - describe "which namespace a fixity governs" $ do- it "gives one to types where the module declares a type of that name" $- declaresIn- "exports:\n :~:\nfixities infix 4 :~:\nab12\n data (:~:) a b where\n"- `shouldBe` [((InTypes, OpName ":~:"), Fixity NoAssoc 4)]+ it "reads a type with its members, itself first" $+ exportsOf+ "exports:\n GHC.Generics.:*:{tc 34A}{GHC.Generics.:*:{d r2}}\n"+ `shouldBe` Just+ [ AvailTC+ (inGenerics InTypes ":*:")+ [inGenerics InTypes ":*:", inGenerics InTerms ":*:"]+ ] - it "gives one to terms where nothing declares a type of that name" $- declaresIn- "exports:\n !\nfixities infixl 9 !\nab12\n (!) :: Int -> Int -> Int\n"- `shouldBe` [((InTerms, OpName "!"), Fixity LeftAssoc 9)]+ it "reads a type a bar says is exported without itself" $+ exportsOf "exports:\n GHC.Base.Functor{tc 2a}|{GHC.Base.<${v rq}}\n"+ `shouldBe` Just+ [ AvailTC+ (HiName "GHC.Base" InTypes (OpName "Functor"))+ [HiName "GHC.Base" InTerms (OpName "<$")]+ ] - it "reads a type synonym as a type" $- declaresIn- "exports:\n :+\nfixities infixr 5 :+\nab12\n type (:+) :: * -> * -> *\n"- `shouldBe` [((InTypes, OpName ":+"), Fixity RightAssoc 5)]+ it "reads members made of dots and bars" $+ exportsOf+ "exports:\n GHC.Internal.Bits.Bits{tc rb}{GHC.Internal.Bits..&.{v rc} GHC.Internal.Bits..|.{v rd}}\n"+ `shouldBe` Just+ [ AvailTC+ (inBits InTypes "Bits")+ [inBits InTypes "Bits", inBits InTerms ".&.", inBits InTerms ".|."]+ ] - it "reads a type family as a type" $- declaresIn- "exports:\n ==\nfixities infixl 6 ==\nab12\n type family (==) a b where\n"- `shouldBe` [((InTypes, OpName "=="), Fixity LeftAssoc 6)]+ it "reads a record field as GHC 9.8 prints it" $+ exportsOf+ "exports:\n M.T{tc r2}{M.+++{fld r3} M.T{d r4} M.foo{fld r5}}\n"+ `shouldBe` Just+ [ AvailTC+ (inM InTypes "T")+ [inM InTypes "T", inM InTerms "+++", inM InTerms "T", inM InTerms "foo"]+ ] - it "reads a class as a type" $- declaresIn- "exports:\n <%>\nfixities infixl 4 <%>\nab12\n class (<%>) a where\n"- `shouldBe` [((InTypes, OpName "<%>"), Fixity LeftAssoc 4)]+ it "reads a record field as GHC 9.6 prints it, by its label" $+ exportsOf+ "exports:\n M.T{tc r2}{M.T{d r3} +++{M.$sel:+++:T{v r4}}+dup+sel foo{M.$sel:foo:T{v r5}}+dup+sel}\n GHC.Generics.M1{tc 34w}{GHC.Generics.M1{d ru} unM1{GHC.Generics.unM1{v rv}}-dup+sel}\n"+ `shouldBe` Just+ [ AvailTC+ (inM InTypes "T")+ [inM InTypes "T", inM InTerms "T", inM InTerms "+++", inM InTerms "foo"],+ AvailTC+ (inGenerics InTypes "M1")+ [ inGenerics InTypes "M1",+ inGenerics InTerms "M1",+ inGenerics InTerms "unM1"+ ]+ ] - it "takes a role declaration as saying the name is a type" $- declaresIn- "exports:\n !\nfixities infixl 9 !\nab12\n type role (!) nominal\n"- `shouldBe` [((InTypes, OpName "!"), Fixity LeftAssoc 9)]+ it "passes over built-in syntax, which it prints bare" $+ exportsOf "exports:\n []\n GHC.Types.~{(w) tc 31i}\n"+ `shouldBe` Just+ [Avail Unneeded, Avail (HiName "GHC.Types" InTypes (OpName "~"))] - it "is not misled by declarations of other names" $- declaresIn- "exports:\n !\nfixities infixl 9 !\nab12\n data Other a b where\n (!) :: Int\n"- `shouldBe` [((InTerms, OpName "!"), Fixity LeftAssoc 9)]+ it "reads a tuple with the commas in its name" $+ exportsOf+ "exports:\n (,){GHC.Internal.Tuple.(,){(w) d 55_6}}\n GHC.Internal.Tuple.getSolo{v r1}\n"+ `shouldBe` Just+ [ AvailTC+ Unneeded+ [Unneeded, HiName "GHC.Internal.Tuple" InTerms (OpName "(,)")],+ Avail (HiName "GHC.Internal.Tuple" InTerms (OpName "getSolo"))+ ] - it "gives a type and a constructor of one name each its own" $- declaresIn- "exports:\n :.{:.}\n\- \fixities infixr 5 :., infixl 4 :.\n\- \b4c8\n type (:.) :: * -> * -> *\n data (:.) a b = a :. b\n"- `shouldBe` [ ((InTypes, OpName ":."), Fixity LeftAssoc 4),- ((InTerms, OpName ":."), Fixity RightAssoc 5)- ]+ it "reads nothing where one entry cannot be read" $ do+ exportsOf "exports:\n M.f{v r1}\n M.g{zz r2}\n" `shouldBe` Nothing+ exportsOf "exports:\n M.f{v r1}\n g{v r2}\n" `shouldBe` Nothing - it "gives one fixity for a type and a constructor of one name to both" $- declaresIn- "exports:\n :-{:-}\n\- \fixities infix 8 :-, infix 8 :-\n\- \eeee\n type (:-) :: * -> * -> *\n data (:-) a b = a :- b\n"- `shouldBe` [ ((InTypes, OpName ":-"), Fixity NoAssoc 8),- ((InTerms, OpName ":-"), Fixity NoAssoc 8)+ describe "what a module declares" $ do+ it "reads a fixity line wrapped across lines, by namespace" $+ fixitiesOf+ "fixities infixr -1 ->{tc}, infixr 5 :{d}, infix 4 ~{tc},\n\+ \ infix 4 ~~{tc}\n"+ `shouldBe` [ (InTypes, OpName "->", Fixity RightAssoc (-1)),+ (InTerms, OpName ":", Fixity RightAssoc 5),+ (InTypes, OpName "~", Fixity NoAssoc 4),+ (InTypes, OpName "~~", Fixity NoAssoc 4) ] - describe "the members of a name" $ do- it "takes the members an entry wears in braces" $- members "exports:\n GHC.Internal.Base.NonEmpty{GHC.Internal.Base.:|}\n"- `shouldBe` [(OpName "NonEmpty", [OpName ":|"])]-- it "takes every one of them" $- members- "exports:\n\- \ GHC.Internal.Base.Applicative{GHC.Internal.Base.*> GHC.Internal.Base.<*> GHC.Internal.Base.pure}\n"- `shouldBe` [ ( OpName "Applicative",- [OpName "*>", OpName "<*>", OpName "pure"]- )+ it "keeps a type and a constructor of one name apart" $+ fixitiesOf+ "fixities infixr 6 :*:{d}, infixr 6 :*:{tc}, infixr 5 :+:{tc}\n"+ `shouldBe` [ (InTerms, OpName ":*:", Fixity RightAssoc 6),+ (InTypes, OpName ":*:", Fixity RightAssoc 6),+ (InTypes, OpName ":+:", Fixity RightAssoc 5) ] - it "takes them from a partial export, which still says what it has" $- members "exports:\n GHC.Internal.Base.Functor|{GHC.Internal.Base.<$}\n"- `shouldBe` [(OpName "Functor", [OpName "<$"])]+ it "passes over an entry it cannot read, and keeps the rest" $+ fixitiesOf+ "fixities infixl notadigit ?{v}, infixl 9 !{v}, infixl 9 ?, infixl\n"+ `shouldBe` [(InTerms, OpName "!", Fixity LeftAssoc 9)] - it "takes a name this module declared, written without a module" $- members "exports:\n WrappedArrow{WrapArrow unwrapArrow}\n"- `shouldBe` [ ( OpName "WrappedArrow",- [OpName "WrapArrow", OpName "unwrapArrow"]- )- ]+ describe "what it brings in, read off ghc --show-iface" $ do+ it "is every name it exports, by namespace" $+ fmap+ (fmap certainNames . interfaceExports)+ ( interfaceOf+ "Control.Applicative"+ "exports:\n GHC.Base.Alternative{tc rj}{GHC.Base.<|>{v rk} GHC.Base.empty{v rl}}\n GHC.Base.Functor{tc 2a}|{GHC.Base.<${v rq}}\n"+ )+ `shouldBe` Just+ ( Just+ ( Set.fromList+ [ (InTypes, OpName "Alternative"),+ (InTerms, OpName "<|>"),+ (InTerms, OpName "empty"),+ (InTerms, OpName "<$")+ ]+ )+ ) - it "keeps entries apart where several sit on one line" $- members "exports:\n A{B} C{D}\n"- `shouldBe` [(OpName "A", [OpName "B"]), (OpName "C", [OpName "D"])]+ it "passes on what other modules declare" $+ fmap+ interfaceReexports+ ( interfaceOf+ "Control.Applicative"+ "exports:\n Control.Applicative.optional{v r1}\n Data.Functor.<$>{v r3}\n"+ )+ `shouldBe` Just [("Data.Functor", InTerms, OpName "<$>")] - it "has nothing to say about a name without members" $- members "exports:\n decode'\n Data.Aeson.Types.FromJSON..:\n"- `shouldBe` []+ it "declares the fixities of what it exports itself" $+ fmap+ (Map.toList . interfaceDeclares)+ ( interfaceOf+ "M"+ "exports:\n M.<+>{v r1}\n M.T{tc r2}{M.T{d r3}}\nfixities infixl 6 <+>{v}, infixr 2 <~>{v}\n"+ )+ `shouldBe` Just [((InTerms, OpName "<+>"), Fixity LeftAssoc 6)] describe "what a decoded interface brings in" $ do it "is every name it exports, by namespace, and the members of each type" $@@ -292,39 +242,23 @@ ) `shouldBe` Right [] - it "is left out of what ghc --show-iface prints" $- declares- "exports:\n <+>\n parser\nfixities infixl 3 <+>, infixr 2 <~>\n"- `shouldBe` [(OpName "<+>", Fixity LeftAssoc 3)]-- it "is left out for a type a bar says is exported without itself" $- declares "exports:\n :<:|{inj}\nfixities infixr 4 :<:\n" `shouldBe` []-- it "is left out where the module passes on another name spelled the same" $- declares "exports:\n GHC.Internal.Base.>>=\nfixities infixr 2 >>=\n"- `shouldBe` []- describe "sections it has no use for" $ it "is not confused by the rest of the file" $ do let out = "Magic: Wanted 33214052,\n\ \ got 33214052\n\- \interface Data.Aeson 9103\n\- \ interface hash: 6b4f\n\+ \Version: Wanted 9067,\n\+ \ got 9067\n\+ \interface Data.Bits 9067\n\+ \ interface hash: efd3cbc01244a91d72b3a670a9191f4b\n\ \exports:\n\- \ !\n\- \ Data.Aeson.Types.FromJSON..:\n\- \fixities infixl 9 !\n\- \direct package dependencies: base-4.20.2.0 bytestring-0.12.2.0\n\- \orphans: Data.Orphans\n\+ \ Data.Bits.!<<.{v r1}\n\+ \direct module dependencies:\n\+ \fixities infixl 8 !<<.{v}\n\+ \instance GHC.Base.Alternative{tc rj} [Control.Applicative.ZipList{tc rd}]\n\ \trusted: none\n"- fmap (Map.toList . interfaceDeclares) (parseInterface "Data.Aeson" out)- `shouldBe` Just [((InTerms, OpName "!"), Fixity LeftAssoc 9)]- fmap interfaceReexports (parseInterface "Data.Aeson" out)- `shouldBe` Just- [ ("Data.Aeson.Types.FromJSON", InTypes, OpName ".:"),- ("Data.Aeson.Types.FromJSON", InTerms, OpName ".:")- ]+ fmap (Map.toList . interfaceDeclares) (parseInterface "Data.Bits" out)+ `shouldBe` Just [((InTerms, OpName "!<<."), Fixity LeftAssoc 8)] describe "output it will not read" $ do it "refuses what does not name a module at all" $@@ -334,48 +268,30 @@ it "refuses an interface for a different module" $ parseInterface "Data.Map.Strict"- (header "Data.Map.Lazy" <> "fixities infixl 9 !\n")+ (header "Data.Map.Lazy" <> "fixities infixl 9 !{v}\n") `shouldBe` Nothing it "reads one that names the module asked for" $- declares "exports:\n !\nfixities infixl 9 !\n"- `shouldBe` [(OpName "!", Fixity LeftAssoc 9)]+ fmap+ (Map.toList . interfaceDeclares)+ (interfaceOf "M" "exports:\n M.!{v r1}\nfixities infixl 9 !{v}\n")+ `shouldBe` Just [((InTerms, OpName "!"), Fixity LeftAssoc 9)] header :: Text -> Text-header modName = "interface " <> modName <> " 9103\n"---- | The fixities an interface of this shape declares, by name alone.-declares :: Text -> [(OpName, Fixity)]-declares =- fmap (\((_, op), fixity) -> (op, fixity))- . maybe [] (Map.toList . interfaceDeclares)- . parseInterface "M"- . (header "M" <>)+header modName = "interface " <> modName <> " 9067\n" --- | The same, keeping the namespace each governs.-declaresIn :: Text -> [((Namespace, OpName), Fixity)]-declaresIn =- maybe [] (Map.toList . interfaceDeclares)- . parseInterface "M"- . (header "M" <>)+-- | What an interface of this shape exports, as decoding it would give it.+exportsOf :: Text -> Maybe [HiExport]+exportsOf = fmap hiExports . shownHiFile . (header "M" <>) --- | What a module of this shape passes on, namespace aside.-passesOn :: Text -> [(Text, OpName)]-passesOn =- nub- . fmap (\(m, _, op) -> (m, op))- . maybe [] interfaceReexports- . parseInterface "M"- . (header "M" <>)+-- | The fixities an interface of this shape declares, as decoding it would+-- give them.+fixitiesOf :: Text -> [(Namespace, OpName, Fixity)]+fixitiesOf = maybe [] hiFixities . shownHiFile . (header "M" <>) --- | The members of each exported name, in a settled order.-members :: Text -> [(OpName, [OpName])]-members =- maybe- []- (fmap (fmap (Set.toList . Set.map snd)) . Map.toList . interfaceMembers)- . parseInterface "M"- . (header "M" <>)+-- | What an interface of this shape says, for the module named.+interfaceOf :: Text -> Text -> Maybe Interface+interfaceOf modName = parseInterface modName . (header modName <>) -- | A name the module @M@ declares. inM :: Namespace -> Text -> HiName@@ -384,3 +300,11 @@ -- | A name another module, N, declares. inN :: Namespace -> Text -> HiName inN namespace = HiName "N" namespace . OpName++-- | A name @GHC.Generics@ declares.+inGenerics :: Namespace -> Text -> HiName+inGenerics namespace = HiName "GHC.Generics" namespace . OpName++-- | A name @GHC.Internal.Bits@ declares.+inBits :: Namespace -> Text -> HiName+inBits namespace = HiName "GHC.Internal.Bits" namespace . OpName
tests/Tilia/Fixity/PlanSpec.hs view
@@ -68,6 +68,7 @@ storePackageCache, ) import Tilia.Fixity.HiFile (HiExport (..), HiFile (..), HiName (..))+import Tilia.Fixity.Interface (shownHiFile) import Tilia.Fixity.PackageDb ( Installed (..), InstalledPackage (..),@@ -100,6 +101,7 @@ generatedModuleSpec askedAtOnce givenInterfaces+ shownInterfaces readBefore namedAlike heldByThePlan@@ -1505,6 +1507,60 @@ inClasses namespace = HiName "Classes" namespace . OpName inBoth namespace = HiName "Both" namespace . OpName +-- | A resolver over interfaces read off what @ghc --show-iface@ printed+-- under GHC 9.6, as every interface of GHC 9.8 and older is read.+shownInterfaces :: Spec+shownInterfaces = describe "a resolver over interfaces ghc --show-iface printed" $ do+ it "settles an operator handed on through an import that hides another" $ do+ resolver <- newResolverFrom (pure . shown)+ answer <-+ withReexports+ (askModule resolver Nothing)+ "M"+ ( summarize+ (Is #implicitPrelude)+ ( pmModule+ ( parsed+ "module M ((<|>)) where\nimport Control.Applicative hiding (asum)\n"+ )+ )+ )+ unsettledThrough answer (InTerms, OpName "<|>") `shouldBe` []+ Map.lookup (InTerms, OpName "<|>") (establishedFixities answer)+ `shouldBe` Just (Fixity LeftAssoc 3)++ it "decides an operator of the Prelude over an import it cannot read" $ do+ resolver <- newResolverFrom (pure . shown)+ let m =+ parsed "module M where\nimport Unread\nx = mempty `mappend` mempty\n"+ scope <- scopeFor resolver Nothing (Is #implicitPrelude) (pure (pmModule m))+ lookupFixity scope InTerms Nothing (OpName "mappend")+ `shouldBe` Resolved defaultFixity ReportDefault+ where+ shown m = shownHiFile =<< Map.lookup m printed+ printed =+ Map.fromList+ [ ( "Control.Applicative",+ "interface Control.Applicative [family instance module] 9067\n\+ \exports:\n\+ \ Control.Applicative.optional{v r1}\n\+ \ Data.Foldable.asum{v r2}\n\+ \ GHC.Base.Alternative{tc rj}{GHC.Base.<|>{v rk} GHC.Base.empty{v rl} GHC.Base.many{v rm} GHC.Base.some{v rn}}\n"+ ),+ ( "GHC.Base",+ "interface GHC.Base [orphan module] 9067\n\+ \exports:\n\+ \ GHC.Base.Alternative{tc r1B}{GHC.Base.<|>{v r1C} GHC.Base.empty{v r1D} GHC.Base.many{v r1E} GHC.Base.some{v r1F}}\n\+ \ GHC.Base.Monoid{tc 2M}{GHC.Base.mappend{v 08Y} GHC.Base.mconcat{v 08Z} GHC.Base.mempty{v 08X}}\n\+ \fixities infixl 3 <|>{v}\n"+ ),+ ( "Prelude",+ "interface Prelude 9067\n\+ \exports:\n\+ \ GHC.Base.Monoid{tc 2M}{GHC.Base.mappend{v 08Y} GHC.Base.mconcat{v 08Z} GHC.Base.mempty{v 08X}}\n"+ )+ ]+ -- | A module of the project's own, which an earlier run has read. readBefore :: Spec readBefore = describe "a module of the project's own, read by an earlier run" $ do@@ -1751,6 +1807,91 @@ fixityIn rs (project </> "M.hs") "Dep" "<$>" `shouldReturn` Just (Fixity LeftAssoc 4) + it "is the project's own, for a dependency's import no other package holds"+ $ withPackages+ [ ("fake.cabal", library "fake" "0.1.0.0" ["M", "Ops"] []),+ ("M.hs", "module M where\nimport Dep ((<+>))\n"),+ ( "Ops.hs",+ "module Ops ((<+>)) where\ninfixl 3 <+>\n\+ \(<+>) :: Int -> Int -> Int\na <+> _ = a\n"+ )+ ]+ [ ( "dep",+ "1.0",+ [ ("dep.cabal", library "dep" "1.0" ["Dep"] []),+ ("Dep.hs", "module Dep ((<+>)) where\nimport Ops\n")+ ]+ )+ ]+ $ \rs project ->+ fixityIn rs (project </> "M.hs") "Dep" "<+>"+ `shouldReturn` Just (Fixity LeftAssoc 3)++ it "is the plan's package, for a dependency's import the project holds too"+ $ withPackages+ [ ("fake.cabal", library "fake" "0.1.0.0" ["M", "Ops"] []),+ ("M.hs", "module M where\nimport Dep ((<+>))\n"),+ ( "Ops.hs",+ "module Ops ((<+>)) where\ninfixl 3 <+>\n\+ \(<+>) :: Int -> Int -> Int\na <+> _ = a\n"+ )+ ]+ [ ( "dep",+ "1.0",+ [ ("dep.cabal", library "dep" "1.0" ["Dep"] []),+ ("Dep.hs", "module Dep ((<+>)) where\nimport Ops\n")+ ]+ ),+ ( "ops",+ "1.0",+ [ ("ops.cabal", library "ops" "1.0" ["Ops"] []),+ ( "Ops.hs",+ "module Ops ((<+>)) where\ninfixr 7 <+>\n\+ \(<+>) :: Int -> Int -> Int\na <+> _ = a\n"+ )+ ]+ )+ ]+ $ \rs project ->+ fixityIn rs (project </> "M.hs") "Dep" "<+>"+ `shouldReturn` Just (Fixity RightAssoc 7)++ it+ "is the project's own, where a package reaches another through a dependency"+ $ withProject+ [ ( "ops",+ "ops",+ "1.0",+ [ ("ops.cabal", library "ops" "1.0" ["Ops"] []),+ ( "Ops.hs",+ "module Ops ((<+>)) where\ninfixl 3 <+>\n\+ \(<+>) :: Int -> Int -> Int\na <+> _ = a\n"+ )+ ]+ ),+ ( "ops-tests",+ "ops-tests",+ "1.0",+ [ ("ops-tests.cabal", suite "ops-tests" "1.0"),+ ("tests/Main.hs", "module Main (main) where\nimport Dep ((<+>))\n")+ ]+ )+ ]+ [ ( "dep",+ "1.0",+ [ ("dep.cabal", library "dep" "1.0" ["Dep"] []),+ ("Dep.hs", "module Dep ((<+>)) where\nimport Ops\n")+ ]+ )+ ]+ $ \rs project ->+ fixityIn+ rs+ (project </> "ops-tests" </> "tests" </> "Main.hs")+ "Dep"+ "<+>"+ `shouldReturn` Just (Fixity LeftAssoc 3)+ it "is the plan's version of a package, where another is installed" $ withPackages [ ("fake.cabal", library "fake" "0.1.0.0" ["M"] []),@@ -1818,6 +1959,17 @@ <> [ " other-modules: " <> T.intercalate ", " hidden | not (null hidden) ]+ suite name version =+ T.unlines+ [ "cabal-version: 2.4",+ "name: " <> name,+ "version: " <> version,+ "test-suite tests",+ " type: exitcode-stdio-1.0",+ " main-is: Main.hs",+ " hs-source-dirs: tests",+ " default-language: Haskell2010"+ ] -- | A project of one package, @fake@, whose files are given, and of -- packages from Hackage, each with its tarball where @cabal@ would have put@@ -1831,13 +1983,37 @@ -- | Given a resolver for the plan and the project's directory. (Resolver -> FilePath -> IO a) -> IO a-withPackages files fetched act =+withPackages files = withProject [(".", "fake", "0.1.0.0", files)]++-- | 'withPackages' for a project of the packages given, each in its own+-- directory, which its @cabal.project@ lists.+withProject ::+ -- | The project's packages, each by its directory, name and version,+ -- with its files.+ [(FilePath, Text, Text, [(FilePath, Text)])] ->+ -- | The packages from Hackage, each by its name and version, with its+ -- files.+ [(Text, Text, [(FilePath, Text)])] ->+ -- | Given a resolver for the plan and the project's directory.+ (Resolver -> FilePath -> IO a) ->+ IO a+withProject own fetched act = withSystemTempDirectory "tilia-packages" $ \dir -> do root <- canonicalizePath dir let project = root </> "project"- for_ files $ \(path, text) -> do- createDirectoryIfMissing True (takeDirectory (project </> path))- writeUtf8File (project </> path) text+ for_ own $ \(sub, _, _, files) ->+ for_ files $ \(path, text) -> do+ let file = project </> sub </> path+ createDirectoryIfMissing True (takeDirectory file)+ writeUtf8File file text+ writeUtf8File (project </> "cabal.project") $+ "packages: " <> T.unwords [T.pack sub | (sub, _, _, _) <- own] <> "\n"+ placed <-+ traverse+ ( \(sub, name, version, _) ->+ (name,version,) <$> canonicalizePath (project </> sub)+ )+ own for_ fetched $ \(name, version, inside) -> do let held = T.unpack (name <> "-" <> version) tarball =@@ -1855,19 +2031,23 @@ "{\"compiler-id\":\"ghc-0.0\",\"install-plan\":[" <> T.intercalate ","- ( ( "{\"pkg-name\":\"fake\",\"pkg-version\":\"0.1.0.0\",\- \\"pkg-src\":{\"type\":\"local\",\"path\":\""- <> T.pack project+ ( [ "{\"pkg-name\":\""+ <> name+ <> "\",\"pkg-version\":\""+ <> version+ <> "\",\"pkg-src\":{\"type\":\"local\",\"path\":\""+ <> T.pack path <> "\"}}"- )- : [ "{\"pkg-name\":\""- <> name- <> "\",\"pkg-version\":\""- <> version- <> "\",\"pkg-src\":{\"type\":\"repo-tar\"}}"- | (name, version, _) <- fetched- ]- <> installed+ | (name, version, path) <- placed+ ]+ <> [ "{\"pkg-name\":\""+ <> name+ <> "\",\"pkg-version\":\""+ <> version+ <> "\",\"pkg-src\":{\"type\":\"repo-tar\"}}"+ | (name, version, _) <- fetched+ ]+ <> installed ) <> "]}" withEnvironment
tilia.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: tilia-version: 0.2.0.0+version: 0.2.0.1 license: BSD-3-Clause license-file: LICENSE.md author: Mark Karpov <markkarpov92@gmail.com>