Skip to content

Commit

Permalink
Improve error message for ambiguous export (#3289)
Browse files Browse the repository at this point in the history
The error message now lists all possibilities. It also uses the correct
word to describe the namespace.
E.g.
Before
```
/home/jan/projects/anoma-applib/Applib.juvix:1:8-14: error:
The symbol ExternalIdentity is exported multiple times in the module Applib
```
After
```
/home/jan/projects/anoma-applib/Applib.juvix:1:8-14: error:
The module ExternalIdentity is exported multiple times in the module Applib
• Defined in /home/jan/projects/anoma-applib/Applib/Identities.juvix:13:8-24
• Defined in /home/jan/projects/anoma-applib/Anoma/Identity/External.juvix:7:6-22
```
  • Loading branch information
janmasrovira authored Jan 21, 2025
1 parent 2190ea3 commit e84bace
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/Juvix/Compiler/Concrete/Data/NameSpace.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ type family NameSpaceEntryType s = res | res -> s where
NameSpaceEntryType 'NameSpaceModules = ModuleSymbolEntry
NameSpaceEntryType 'NameSpaceFixities = FixitySymbolEntry

nameSpaceElemName :: (IsString str) => NameSpace -> str
nameSpaceElemName = \case
NameSpaceSymbols -> "symbol"
NameSpaceModules -> "module"
NameSpaceFixities -> "fixity"

entryName :: forall ns. (SingI ns) => Lens' (NameSpaceEntryType ns) S.Name
entryName = case sing :: SNameSpace ns of
SNameSpaceSymbols -> \f -> \case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,18 +925,17 @@ exportScope Scope {..} = do

err :: NonEmpty (NameSpaceEntryType ns) -> Sem r a
err es =
throw
( ErrMultipleExport
( MultipleExportConflict
_scopePath
s
( case sing :: SNameSpace ns of
SNameSpaceSymbols -> ExportEntriesSymbols es
SNameSpaceModules -> ExportEntriesModules es
SNameSpaceFixities -> ExportEntriesFixities es
)
)
)
throw $
ErrMultipleExport
MultipleExportConflict
{ _multipleExportModule = _scopePath,
_multipleExportSymbol = s,
_multipleExportNameSpace = fromSing (sing :: SNameSpace ns),
_multipleExportEntries = case sing :: SNameSpace ns of
SNameSpaceSymbols -> ExportEntriesSymbols es
SNameSpaceModules -> ExportEntriesModules es
SNameSpaceFixities -> ExportEntriesFixities es
}

getLocalModules :: (Member (State ScoperState) r) => ExportInfo -> Sem r (HashMap S.NameId ScopedModule)
getLocalModules ExportInfo {..} = do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ data ExportEntries
data MultipleExportConflict = MultipleExportConflict
{ _multipleExportModule :: S.AbsModulePath,
_multipleExportSymbol :: Symbol,
_multipleExportNameSpace :: NameSpace,
_multipleExportEntries :: ExportEntries
}
deriving stock (Show)
Expand All @@ -297,10 +298,21 @@ instance ToGenericError MultipleExportConflict where
opts' = fromGenericOptions opts
i = getLoc _multipleExportModule
msg =
"The symbol"
"The"
<+> nameSpaceElemName _multipleExportNameSpace
<+> ppCode opts' _multipleExportSymbol
<+> "is exported multiple times in the module"
<+> ppCode opts' _multipleExportModule
<> hardline
<> itemize
( case _multipleExportEntries of
ExportEntriesSymbols s -> ppEntry <$> s
ExportEntriesModules s -> ppEntry <$> s
ExportEntriesFixities s -> ppEntry <$> s
)
where
ppEntry :: (HasLoc e) => e -> Doc CodeAnn
ppEntry e = "Defined in" <+> annotate (AnnKind KNameTopModule) (pretty (getLoc e))

data NotInScope = NotInScope
{ _notInScopeSymbol :: Symbol,
Expand Down
3 changes: 3 additions & 0 deletions src/Juvix/Compiler/Store/Scoped/Data/SymbolEntry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ instance HasNameKind ModuleSymbolEntry where
instance HasLoc ModuleSymbolEntry where
getLoc (ModuleSymbolEntry s) = s ^. S.nameDefined

instance HasLoc FixitySymbolEntry where
getLoc (FixitySymbolEntry s) = s ^. S.nameDefined

symbolEntryNameId :: SymbolEntry -> NameId
symbolEntryNameId = (^. symbolEntry . S.nameId)

Expand Down

0 comments on commit e84bace

Please sign in to comment.