Fix import/import public

Only make a note that a module is imported when processing the source
file that imports it. We were accidentally reexporting a lot!
This commit is contained in:
Edwin Brady 2020-02-09 21:45:01 +00:00
parent 413d09dad8
commit fa190ab1a9
3 changed files with 59 additions and 30 deletions

View File

@ -369,6 +369,10 @@ updateCGDirectives cgs
= do defs <- get Ctxt
put Ctxt (record { cgdirectives $= (cgs ++) } defs)
getNSas : (String, (List String, Bool, List String)) ->
(List String, List String)
getNSas (a, (b, c, d)) = (b, d)
-- Add definitions from a binary file to the current context
-- Returns the "extra" section of the file (user defined data), the interface
-- hash and the list of additional TTCs that need importing
@ -386,10 +390,12 @@ readFromTTC : TTC extra =>
List (List String, Bool, List String)))
readFromTTC loc reexp fname modNS importAs
= do defs <- get Ctxt
-- If it's already in the context, don't load it again
let False = (modNS, importAs) `elem` map snd (allImported defs)
-- If it's already in the context, with the same reexport flag,
-- don't load it again (we do need to load it again if we're reexporting
-- this time, because we need to reexport the dependencies.)
let False = (modNS, reexp, importAs) `elem` map snd (allImported defs)
| True => pure Nothing
put Ctxt (record { allImported $= ((fname, (modNS, importAs)) :: ) } defs)
put Ctxt (record { allImported $= ((fname, (modNS, reexp, importAs)) :: ) } defs)
Right buf <- coreLift $ readFromFile fname
| Left err => throw (InternalError (fname ++ ": " ++ show err))
@ -399,30 +405,38 @@ readFromTTC loc reexp fname modNS importAs
else Just importAs
ttc <- logTime ("Read file " ++ show modNS) $
readTTCFile modNS as bin
traverse (addGlobalDef modNS as) (context ttc)
traverse_ addUserHole (userHoles ttc)
setNS (currentNS ttc)
setNestedNS (nestedNS ttc)
-- Set up typeHints and autoHints based on the loaded data
traverse_ (addTypeHint loc) (typeHints ttc)
traverse_ addAutoHint (autoHints ttc)
-- Set up pair/rewrite etc names
updatePair (pairnames ttc)
updateRewrite (rewritenames ttc)
updatePrims (primnames ttc)
updateNameDirectives (reverse (namedirectives ttc))
updateCGDirectives (cgdirectives ttc)
when (not reexp) clearSavedHints
resetFirstEntry
-- If it's already imported, but without reexporting, then all we're
-- interested in is returning which other modules to load.
-- Otherwise, add the data
let ex = extraData ttc
if ((modNS, importAs) `elem` map getNSas (allImported defs))
then pure (Just (ex, ifaceHash ttc, imported ttc))
else do
traverse (addGlobalDef modNS as) (context ttc)
traverse_ addUserHole (userHoles ttc)
setNS (currentNS ttc)
setNestedNS (nestedNS ttc)
-- Set up typeHints and autoHints based on the loaded data
traverse_ (addTypeHint loc) (typeHints ttc)
traverse_ addAutoHint (autoHints ttc)
-- Set up pair/rewrite etc names
updatePair (pairnames ttc)
updateRewrite (rewritenames ttc)
updatePrims (primnames ttc)
updateNameDirectives (reverse (namedirectives ttc))
updateCGDirectives (cgdirectives ttc)
-- Finally, update the unification state with the holes from the
-- ttc
ust <- get UST
put UST (record { holes = fromList (holes ttc),
constraints = fromList (constraints ttc),
nextName = nextVar ttc } ust)
pure (Just (extraData ttc, ifaceHash ttc, imported ttc))
when (not reexp) clearSavedHints
resetFirstEntry
-- Finally, update the unification state with the holes from the
-- ttc
ust <- get UST
put UST (record { holes = fromList (holes ttc),
constraints = fromList (constraints ttc),
nextName = nextVar ttc } ust)
pure (Just (ex, ifaceHash ttc, imported ttc))
getImportHashes : Ref Bin Binary ->
Core (List (List String, Int))

View File

@ -749,7 +749,7 @@ record Defs where
-- ^ interface hashes of imported modules
imported : List (List String, Bool, List String)
-- ^ imported modules, whether to rexport, as namespace
allImported : List (String, (List String, List String))
allImported : List (String, (List String, Bool, List String))
-- ^ all imported filenames/namespaces, just to avoid loading something
-- twice unnecessarily (this is a record of all the things we've
-- called 'readFromTTC' with, in practice)
@ -1603,6 +1603,13 @@ setVisible nspace
= do defs <- get Ctxt
put Ctxt (record { gamma->visibleNS $= (nspace ::) } defs)
export
getVisible : {auto c : Ref Ctxt Defs} ->
Core (List (List String))
getVisible
= do defs <- get Ctxt
pure (visibleNS (gamma defs))
-- Return True if the given namespace is visible in the context (meaning
-- the namespace itself, and any namespace it's nested inside)
export

View File

@ -59,7 +59,7 @@ readModule : {auto c : Ref Ctxt Defs} ->
Core ()
readModule top loc vis reexp imp as
= do defs <- get Ctxt
let False = (imp, as) `elem` map snd (allImported defs)
let False = (imp, reexp, as) `elem` map snd (allImported defs)
| True => when vis (setVisible imp)
Right fname <- nsToPath loc imp
| Left err => throw err
@ -67,7 +67,6 @@ readModule top loc vis reexp imp as
readFromTTC {extra = SyntaxInfo}
loc vis fname imp as
| Nothing => when vis (setVisible imp) -- already loaded, just set visibility
addImported (imp, reexp, as)
extendAs imp as syn
defs <- get Ctxt
@ -87,7 +86,8 @@ readImport : {auto c : Ref Ctxt Defs} ->
{auto s : Ref Syn SyntaxInfo} ->
Import -> Core ()
readImport imp
= readModule True (loc imp) True (reexport imp) (path imp) (nameAs imp)
= do readModule True (loc imp) True (reexport imp) (path imp) (nameAs imp)
addImported (path imp, reexport imp, nameAs imp)
readHash : {auto c : Ref Ctxt Defs} ->
{auto u : Ref UST UState} ->
@ -129,11 +129,19 @@ readAsMain fname
replNestedNS <- getNestedNS
extendAs replNS replNS syn
-- Read the main file's top level imported modules, so we have access
-- to their names (and any of their public imports)
ustm <- get UST
traverse_ (\ mimp =>
do let m = fst mimp
let as = snd (snd mimp)
readModule False emptyFC True False m as) more
readModule False emptyFC True True m as
addImported (m, True, as)) more
-- also load the prelude, if required, so that we have access to it
-- at the REPL.
when (not (noprelude !getSession)) $
readModule False emptyFC True True ["Prelude"] ["Prelude"]
-- We're in the namespace from the first TTC, so use the next name
-- from that for the fresh metavariable name generation