Looking up names in an "opportunistic" type checker plugin

Erdi, Gergo Gergo.Erdi at sc.com
Mon Oct 3 07:08:48 UTC 2022


PUBLIC

Hi,

I'm writing a type checker plugin that should do things whenever used in the context of a module that already has some definitions loaded. I've hacked together the following function using internal details that I feel I should not be using:

lookupOrigMaybe :: Module -> OccName -> TcPluginM (Maybe Name)
lookupOrigMaybe mod occ = do
    hsc_env <- getTopEnv
    unsafeTcPluginTcM $ liftIO $ updateNameCache (hsc_NC hsc_env) mod occ $ \cache0 -> do
        let name = lookupOrigNameCache cache0 mod occ
        return (cache0, name)

I would like to do this in a nicer way instead. It should be enough to check that the given module is already loaded, since for my use case, if the module is there but the name isn't, then all bets are off and my plugin might as well just error out. So I was hoping I could use `findImportedModule` followed by `lookupOrig`, but alas, `findImportedModule` fails me in one of two ways:


  1.  If I don't specify the package (i.e. use `NoPkgQual`), `findImportedModule` seems to return a `Found{}` result even when the module isn't actually loaded yet, leading to `lookupOrig` failing:

    attempting to use module 'main:Cortex.Stem.Relation.Types' (Cortex.Stem.Relation.Types) which is not loaded


  1.  If I do specify the package (using `ThisPkg`), then `findImportedModule` chokes in situations when the given unit would come "later" in the dependency graph than the currenty processed one:

ghc-mu-core-to-exp: panic! (the 'impossible' happened)
  GHC version 9.5.20220830:
        findImportModule

Cortex.Stem.Relation.Types
"main"
Just ghc-bignum
main
[ghc-bignum, cortex-prim]
Call stack:
    CallStack (from HasCallStack):
      callStackDoc, called at compiler/GHC/Utils/Panic.hs:188:37 in ghc-lib-0.20220830-FvKByKwp6ri5ICk6wiQO8W:GHC.Utils.Panic
      pprPanic, called at compiler/GHC/Unit/Finder.hs:160:32 in ghc-lib-0.20220830-FvKByKwp6ri5ICk6wiQO8W:GHC.Unit.Finder

Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug

So my question then, is what is the recommended way of looking up a loaded definition opportunistically from a type checker plugin?

Thanks,
            Gergo

This email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please delete all copies and notify the sender immediately. You may wish to refer to the incorporation details of Standard Chartered PLC, Standard Chartered Bank and their subsidiaries at https: //www.sc.com/en/our-locations

Where you have a Financial Markets relationship with Standard Chartered PLC, Standard Chartered Bank and their subsidiaries (the "Group"), information on the regulatory standards we adhere to and how it may affect you can be found in our Regulatory Compliance Statement at https: //www.sc.com/rcs/ and Regulatory Compliance Disclosures at http: //www.sc.com/rcs/fm

Insofar as this communication is not sent by the Global Research team and contains any market commentary, the market commentary has been prepared by the sales and/or trading desk of Standard Chartered Bank or its affiliate. It is not and does not constitute research material, independent research, recommendation or financial advice. Any market commentary is for information purpose only and shall not be relied on for any other purpose and is subject to the relevant disclaimers available at https: //www.sc.com/en/regulatory-disclosures/#market-disclaimer.

Insofar as this communication is sent by the Global Research team and contains any research materials prepared by members of the team, the research material is for information purpose only and shall not be relied on for any other purpose, and is subject to the relevant disclaimers available at https: //research.sc.com/research/api/application/static/terms-and-conditions. 

Insofar as this e-mail contains the term sheet for a proposed transaction, by responding affirmatively to this e-mail, you agree that you have understood the terms and conditions in the attached term sheet and evaluated the merits and risks of the transaction. We may at times also request you to sign the term sheet to acknowledge the same.

Please visit https: //www.sc.com/en/regulatory-disclosures/dodd-frank/ for important information with respect to derivative products.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20221003/1b41223b/attachment.html>


More information about the ghc-devs mailing list