on master help sign in
Simplify Build.Interface API, do not read "interfaces.data" when the --no-prelude flag is used
commitc03049a960458a2bf9e285a7457eb4c8023ce980 authorEvan Czaplicki <info@elm-lang.org> dateSat 4 Jan 2014, 15:59 (13 years ago) parent1700065
3 files changed+46−46
compiler/Build/File.hs+2−3 compiler/Build/Interface.hs+28−28 compiler/Metadata/Prelude.hs+16−15
compiler/Build/File.hs+2−3
@@ -57,9 +57,8 @@ alreadyCompiled flags filePath = do
5757retrieve :: Flag.Flags -> Map.Map String M.ModuleInterface -> FilePath
5858 -> IO (String, M.ModuleInterface)
5959retrieve flags interfaces filePath = do
60− bytes <- Interface.load (Utils.elmi flags filePath)
61− let binary = Interface.decode (Utils.elmi flags filePath) =<< bytes
62− case Interface.isValid filePath =<< binary of
60+ iface <- Interface.load (Utils.elmi flags filePath)
61+ case Interface.isValid filePath iface of
6362 Right (name, interface) ->
6463 do when (Flag.print_types flags) (Print.interfaceTypes interfaces interface)
6564 return (name, interface)
compiler/Build/Interface.hs+28−28
@@ -1,46 +1,46 @@
11{-# OPTIONS_GHC -W #-}
2−module Build.Interface (load,decode,isValid) where
2+module Build.Interface (load,isValid) where
33
44import qualified Data.ByteString.Lazy as L
55import qualified Data.Binary as Binary
66
77import qualified Elm.Internal.Version as Version
88import System.Directory (doesFileExist)
9+import System.Exit
10+import System.IO
911
1012import SourceSyntax.Module
1113
12−load :: FilePath -> IO (Either String L.ByteString)
13−load filePath = do
14− exists <- doesFileExist filePath
15− if exists
16− then do
17− byteString <- L.readFile filePath
18− return $ Right byteString
19−
20− else
21− return $ Left $ "Unable to find file " ++ filePath ++
22− " for deserialization!"
23−
24−decode :: Binary.Binary a => FilePath -> L.ByteString -> Either String a
25−decode filePath bytes =
26− case Binary.decodeOrFail bytes of
27− Right (_, _, binaryInfo) -> Right binaryInfo
28−
29− Left (_, offset, err) ->
30− Left $ concat $
31− [ "Error reading build artifact: ", filePath, "\n"
32− , " The exact error was '", err, "' at offset ", show offset, ".\n"
33− , " The file was generated by a previous build and may be outdated or corrupt.\n"
34− , " Please remove the file and try again."
35− ]
14+load :: Binary.Binary a => FilePath -> IO a
15+load filePath =
16+ do exists <- doesFileExist filePath
17+ case exists of
18+ False -> failure $ "Unable to find file " ++ filePath ++ " for deserialization!"
19+ True -> do
20+ bytes <- L.readFile filePath
21+ case Binary.decodeOrFail bytes of
22+ Right (_, _, binaryInfo) -> return binaryInfo
23+ Left (_, offset, err) -> failure $ msg offset err
24+
25+ where
26+ failure err = do hPutStrLn stderr err
27+ exitFailure
28+
29+ msg offset err = concat
30+ [ "Error reading build artifact: ", filePath, "\n"
31+ , " '", err, "' at offset ", show offset, "\n"
32+ , " The file was generated by a previous build and may be outdated or corrupt.\n"
33+ , " Please remove the file and try again."
34+ ]
3635
3736isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
3837isValid filePath (name, interface) =
39− if iVersion interface == Version.elmVersion
38+ let version = iVersion interface in
39+ if version == Version.elmVersion
4040 then Right (name, interface)
4141 else Left $ concat
4242 [ "Error reading build artifact: ", filePath, "\n"
43− , " It was generated by a different version of the compiler: "
44− , show (iVersion interface), "\n"
43+ , " It was generated by version ", show version, " of the compiler,\n"
44+ , " but you are using version ", show Version.elmVersion, "\n"
4545 , " Please remove the file and try again.\n"
4646 ]
compiler/Metadata/Prelude.hs+16−15
@@ -28,30 +28,31 @@ prelude = text ++ map (\n -> (n, Hiding [])) modules
2828
2929interfaces :: Bool -> IO Interfaces
3030interfaces noPrelude =
31− do ifaces <- safeReadDocs =<< Path.getDataFileName "interfaces.data"
32− return $ if noPrelude then Map.empty else ifaces
31+ if noPrelude
32+ then return $ Map.empty
33+ else safeReadDocs =<< Path.getDataFileName "interfaces.data"
3334
3435safeReadDocs :: FilePath -> IO Interfaces
3536safeReadDocs name =
3637 E.catch (readDocs name) $ \err -> do
3738 let _ = err :: IOError
38− putStrLn $ unlines [ "Error reading types for standard library!"
39− , " The file should be at " ++ name
40− , " If you are using a stable version of Elm,"
41− , " please report an issue at github.com/evancz/Elm"
42− , " and specify your versions of Elm and your OS" ]
39+ hPutStrLn stderr $ unlines $
40+ [ "Error reading types for standard library from file " ++ name
41+ , " If you are using a stable version of Elm, please report an issue at"
42+ , " <http://github.com/evancz/Elm/issues> specifying version numbers for"
43+ , " Elm and your OS." ]
4344 exitFailure
4445
45−hasInterfaces :: [(String, ModuleInterface)] -> Either String [(String, ModuleInterface)]
46−hasInterfaces [] = Left "No interfaces found in serialized Prelude!"
47−hasInterfaces ifaces = Right ifaces
48−
4946readDocs :: FilePath -> IO Interfaces
5047readDocs filePath = do
51− bytes <- Interface.load filePath
52− let interfaces = Interface.decode filePath =<< bytes
53− case mapM (Interface.isValid filePath) =<< hasInterfaces =<< interfaces of
54− Right ifaces -> return $ Map.fromList ifaces
48+ interfaces <- Interface.load filePath
49+ case mapM (Interface.isValid filePath) (interfaces :: [(String, ModuleInterface)]) of
5550 Left err -> do
5651 hPutStrLn stderr err
5752 exitFailure
53+
54+ Right [] -> do
55+ hPutStrLn stderr "No interfaces found in serialized Prelude!"
56+ exitFailure
57+
58+ Right ifaces -> return $ Map.fromList ifaces