@@ -159,11 +159,19 @@ findImport ::
159159 (name, P.ModuleName) ->
160160 m (P.ModuleName, name)
161161findImport imps (name, orig) =
162− case filter (\(qual, mn) -> P.disqualify qual == name && mn == orig) imps of
163− [(P.Qualified (Just importedFrom) _, _)] ->
164− pure (importedFrom, name)
165− other ->
166− internalErrorInModule ("findImport: unexpected result: " ++ show other)
162+ let
163+ matches (qual, mn) = P.disqualify qual == name && mn == orig
164+ matching = filter matches imps
165+ getQualified (P.Qualified mname _) = mname
166+ in
167+ case mapMaybe (getQualified . fst) matching of
168+ -- A value can occur more than once if it is imported twice (eg, if it is
169+ -- exported by A, re-exported from A by B, and C imports it from both A
170+ -- and B). In this case, we just take its first appearance.
171+ (importedFrom:_) ->
172+ pure (importedFrom, name)
173+ [] ->
174+ internalErrorInModule ("findImport: not found: " ++ show (name, orig))
167175
168176lookupValueDeclaration ::
169177 (Applicative m,