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

Locate imports at their first use site #16

Merged
merged 1 commit into from
Mar 30, 2024
Merged

Locate imports at their first use site #16

merged 1 commit into from
Mar 30, 2024

Conversation

tfausak
Copy link
Owner

@tfausak tfausak commented Mar 30, 2024

This was motivated by #12 but ultimately unrelated to that.

This PR updates the generated imports to actually have a location. Previously imports would have no location. This is generally fine, but it meant that if you tried to import a module that didn't exist, you'd get an error at the top of the file like this:

{-# OPTIONS_GHC -fplugin=Imp #-}

main = DoesNotExist.print ()
Main.hs:1:1: error: [GHC-87110]
    Could not find module ‘DoesNotExist’.
    Use -v to see a list of the files searched for.
  |
1 | {-# OPTIONS_GHC -fplugin=Imp #-}
  | ^

That's not wrong since the import doesn't actually exist in the source file, but it's not very helpful either. After the changes in this PR, the imports are "located" at the same place as their first use site. That means that if you try to import a module that doesn't exist, you'll get an error at that use site:

Main.hs:3:8: error: [GHC-87110]
    Could not find module ‘DoesNotExist’.
    Use -v to see a list of the files searched for.
  |
3 | main = DoesNotExist.print ()
  |        ^^^^^^^^^^^^^^^^^^

This is much nicer and should make it easier to debug when things go wrong. Note that this also applies to modules that are hidden:

Main.hs:3:8: error: [GHC-87110]
    Could not load module ‘Data.Text.IO’.
    It is a member of the hidden package ‘text-2.1.1’.
    Perhaps you need to add ‘text’ to the build-depends in your .cabal file.
    Use -v to see a list of the files searched for.
  |
3 | main = Data.Text.IO.putStrLn ""
  |        ^^^^^^^^^^^^^^^^^^^^^

Unfortunately I couldn't think of a way to (easily) test this since the locations aren't included in the output.

@tfausak tfausak merged commit 4f51a7f into main Mar 30, 2024
11 checks passed
@tfausak tfausak deleted the 2024-03-30-located branch March 30, 2024 20:20
@tfausak tfausak added the enhancement New feature or request label Mar 31, 2024
@tfausak tfausak mentioned this pull request Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant