@@ -64,16 +64,17 @@ type Vars = Map Name VarInfo
6464performUsageAnalysis :: Idris [Name]
6565performUsageAnalysis = do
6666 ctx <- tt_ctxt <$> getIState
67+ mainName <- getMainName <$> getIState
6768
68− case lookupCtxt mainName (definitions ctx) of
69− [] -> return [] -- no main -> not compiling -> reachability irrelevant
70− _ -> do
69+ case mainName of
70+ Left reason -> return [] -- no main -> not compiling -> reachability irrelevant
71+ Right main -> do
7172 ci <- idris_classes <$> getIState
7273 cg <- idris_callgraph <$> getIState
7374 opt <- idris_optimisation <$> getIState
7475
7576 -- Build the dependency graph.
76− let depMap = buildDepMap ci ctx mainName
77+ let depMap = buildDepMap ci ctx main
7778
7879 -- Search for reachable nodes in the graph.
7980 let (residDeps, (reachableNames, minUse)) = minimalUsage depMap
@@ -100,7 +101,14 @@ performUsageAnalysis = do
100101
101102 return $ S.toList reachableNames
102103 where
103− mainName = sNS (sUN "main") ["Main"]
104+ getMainName :: IState -> Either Err Name
105+ getMainName ist = case lookupCtxtName n (idris_implicits ist) of
106+ [(n', _)] -> Right n'
107+ [] -> Left (NoSuchVariable n)
108+ more -> Left (CantResolveAlts (map (show . fst) more))
109+ where
110+ n = sNS (sUN "main") ["Main"]
111+
104112 indent = (" " ++)
105113
106114 fmtItem :: (Cond, DepSet) -> String
@@ -118,7 +126,7 @@ performUsageAnalysis = do
118126
119127 storeUsage :: Ctxt CGInfo -> (Name, IntMap (Set Reason)) -> Idris ()
120128 storeUsage cg (n, args)
121− | [x] <- lookupCtxt n cg
129+ | Just x <- lookupCtxtExact n cg
122130 = addToCG n x{ usedpos = flat } -- functions
123131
124132 | otherwise
@@ -128,7 +136,7 @@ performUsageAnalysis = do
128136
129137 checkAccessibility :: Ctxt OptInfo -> (Name, IntMap (Set Reason)) -> Idris ()
130138 checkAccessibility opt (n, reachable)
131− | [Optimise inaccessible dt] <- lookupCtxt n opt
139+ | Just (Optimise inaccessible dt) <- lookupCtxtExact n opt
132140 , eargs@(_:_) <- [fmt n (S.toList rs) | (i,n) <- inaccessible, rs <- maybeToList $ IM.lookup i reachable]
133141 = warn $ show n ++ ": inaccessible arguments reachable:\n " ++ intercalate "\n " eargs
134142
@@ -453,7 +461,7 @@ buildDepMap ci ctx mainName = addPostulates $ dfs S.empty M.empty [mainName]
453461 -- Get the number of arguments that might be considered for erasure.
454462 getArity :: Name -> Int
455463 getArity (SN (WhereN i' ctorName (MN i field)))
456− | [TyDecl (DCon _ _) ty] <- lookupDef ctorName ctx
464+ | Just (TyDecl (DCon _ _) ty) <- lookupDefExact ctorName ctx
457465 = let argTys = map snd $ getArgTys ty
458466 in if i <= length argTys
459467 then length $ getArgTys (argTys !! i)