Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-sensical error message when failing to parse InlineYaml from precompiled module #2365

Closed
martijnbastiaan opened this issue Nov 22, 2022 · 1 comment · Fixed by #2663
Closed
Labels

Comments

@martijnbastiaan
Copy link
Member

martijnbastiaan commented Nov 22, 2022

If your (starter) project contains a primitive such as:

...
{-# NOINLINE foo #-}
{-# ANN foo hasBlackBox #-}
{-# ANN foo (InlineYamlPrimitive [minBound..] [__i|
 BlackBox:
   kind: Declaration
   name: Some.Incorrect.Name.bar
   template: |-
     // begin foo
     foo
     // end foo
|]) #-}

Clash will fail with a very misleading error message:

<no location info>: error:
    module ‘Some.Correct.Name’ cannot be found locally

Ultimately, this is caused by functions in LoadInterfaceFiles not propagating up their errors cleanly:

getUnresolvedPrimitives
:: MonadIO m
=> HDL
-> (Annotations.CoreAnnTarget, Primitive)
-> m [Either UnresolvedPrimitive FilePath]
getUnresolvedPrimitives hdl (target, prim) | hdl `elem` primHdls prim =
case prim of
Primitive _ fp -> pure [Right fp]
InlineYamlPrimitive _ contentOrFp ->
case target of
-- Module annotation, can house many primitives
Annotations.ModuleTarget _ ->
liftIO (decodeOrErrYaml contentOrFp <$> BL.readFile contentOrFp)
Annotations.NamedTarget targetName0 ->
let targetName1 = Text.unpack (qualifiedNameString' targetName0)
primOrErr = decodeOrErrYaml targetName1 (BLU.fromString contentOrFp)
primName = Text.unpack (name primOrErr) in
if primName /= targetName1
then inlineNameError targetName1 primName
else pure [Left primOrErr]
InlinePrimitive _ contentOrFp ->
case target of
-- Module annotation, can house many primitives
Annotations.ModuleTarget _ ->
liftIO (decodeOrErrJson contentOrFp <$> BL.readFile contentOrFp)
Annotations.NamedTarget targetName0 ->
let targetName1 = Text.unpack (qualifiedNameString' targetName0)
primOrErr =
case decodeOrErrJson targetName1 (BLU.fromString contentOrFp) of
[] -> error $ "No annotations found for " ++ targetName1
++ " even though it had an InlinePrimitive annotation."
[p] -> p
_ -> error $ "Multiple primitive definitions found in "
++ "InlinePrimitive annotation for " ++ targetName1 ++ ". "
++ "Expected a single one."
primName = Text.unpack (name primOrErr) in
if primName /= targetName1
then inlineNameError targetName1 primName
else pure [Left primOrErr]
where
inlineNameError targetName primName =
error $ concat
[ "Function " ++ targetName ++ " was annotated with an inline "
, "primitive for " ++ primName ++ ". These names "
, "should be the same." ]
primHdls = \case
Primitive hdls _ -> hdls
InlinePrimitive hdls _ -> hdls
InlineYamlPrimitive hdls _ -> hdls
getUnresolvedPrimitives _ _ = return []

The error Clash should have given is:

Function Some.Correct.Name.foo was annotated with an inline primitive for Some.Incorrect.Name.bar. These names should be the same.

@leonschoorl
Copy link
Member

There is another instance of this same sort of behaviour in loadImportAndInterpret, it's like

res <- loadModule FromPackageDB ...
case res of
  Success _ -> return res
  Failed _ -> loadModule FromLocalSourceFile ...

Where clash tries to load something from the (global) package database, and if anything goes wrong it assumes that it doesn't exist there, throws away the error message and tries again to load that module from a local source file, possible failing with the confusing error that the module can't be found locally.

We should probably fix those two instances in one go.

mergify bot pushed a commit that referenced this issue Feb 13, 2024
martijnbastiaan added a commit that referenced this issue Feb 13, 2024
Fixes #2365

(cherry picked from commit 2b76b07)

Co-authored-by: Martijn Bastiaan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants