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

fix isClassNodeIdentifier in hls-class-plugin #4020

Conversation

soulomoon
Copy link
Collaborator

@soulomoon soulomoon commented Jan 27, 2024

Partially fix #3942, by handling isClassNodeIdentifier correctly.

@soulomoon soulomoon linked an issue Jan 27, 2024 that may be closed by this pull request
@soulomoon soulomoon changed the title fix isClassNodeIdentifier fix isClassNodeIdentifier in hls-class-plugin Jan 27, 2024
@soulomoon soulomoon marked this pull request as ready for review January 27, 2024 14:22
@@ -198,8 +198,9 @@ codeAction recorder state plId (CodeActionParams _ _ docId _ context) = do
_ -> fail "Ide.Plugin.Class.findClassFromIdentifier"
findClassFromIdentifier _ (Left _) = throwError (PluginInternalError "Ide.Plugin.Class.findClassIdentifier")

isClassNodeIdentifier :: IdentifierDetails a -> Bool
isClassNodeIdentifier ident = (isNothing . identType) ident && Use `Set.member` identInfo ident
isClassNodeIdentifier :: Identifier -> IdentifierDetails a -> Bool
Copy link
Collaborator

@jhrcek jhrcek Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you able to reproduce the bug? It would be great (and shouldn't be that hard) to add a test that reproduces the original issue and confirms this fix works.

Copy link
Collaborator Author

@soulomoon soulomoon Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many holes in this plugin.

  1. bug in isClassNodeIdentifier that do not classify idenfitifer correctly.
  2. also there is another bug, we should not invoke such code action in deriving expression, which require us to back trace to its parents to see if it is derving expression.

I should mark this pull request as a partial fix, which solve 1.
Currently, I do not see a way to trigger the bug in 1 without 2.

isClassNodeIdentifier :: IdentifierDetails a -> Bool
isClassNodeIdentifier ident = (isNothing . identType) ident && Use `Set.member` identInfo ident
isClassNodeIdentifier :: Identifier -> IdentifierDetails a -> Bool
isClassNodeIdentifier (Right i) ident | 'C':':':_ <- unpackFS $ occNameFS $ occName i = (isNothing . identType) ident && Use `Set.member` identInfo ident
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could use something like: https://hackage.haskell.org/package/ghc-9.8.1/docs/src/GHC.Types.Name.Occurrence.html#isTcOcc on the occName to check if it's a name of a type class, rather than all the unpacking..

Copy link
Collaborator Author

@soulomoon soulomoon Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see there is a isTcOcc equivalent function of checking data constructor for class. 🤔。eventhough DataName appears so, but it should be for all data constructors and not only data constructors for class

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little obscure - worth a comment. Perhaps @wz1000 can verify if this is indeed the best way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nag @wz1000

@soulomoon
Copy link
Collaborator Author

Thank you for your reviews @jhrcek, I have made some responses.

@jhrcek

This comment has been minimized.

@jhrcek
Copy link
Collaborator

jhrcek commented Jan 28, 2024

Ooops, it turns out I was testing the local builds incorrectly.
I was mistakenly pasting path to hls executable into "GHCUP path" instead of "hls path" field in vscode 🤦 so all the time I was using hls built from master

I apologize for the confabulated attempts at explanations why this PR doesn't work - this PR actually DOES prevent the error from being thrown in UI. Though I must admin I don't understand HOW the fix makes the error go away 😄

@soulomoon
Copy link
Collaborator Author

soulomoon commented Jan 28, 2024

No worries @jhrcek thank you for testing this out.
The error happened because hls-class-plugin wants a data constructor for a class. While the isClassIdentifier failed to provide the needed check for whether an identifier is actually a data constructor for a class or not. In turn, it try to build an malformed action(trying to build a class) on a non-class-data-constructor identifier. Which would throw an error.

This Pr fix this situation. Hence, the error goes away. (Data constructor for a type class is generated by ghc, it is the way ghc handle type classes)

@michaelpj
Copy link
Collaborator

Can we add an example from the ticket as a test to ensure this keeps working?

@soulomoon
Copy link
Collaborator Author

soulomoon commented Jan 28, 2024

Can we add an example from the ticket as a test to ensure this keeps working?

See #4020 (comment)
I failed to construct an example trigger this bug alone without triggering another bug that is not fixed in this pr.
It a bit tricky.

@soulomoon
Copy link
Collaborator Author

I managed to come up with a test that focus on the error by catching it.
and added a few comments.
See if this look good? @jhrcek @michaelpj

…ss-causes-internal-error-inittcwithgbl-failed' into 3942-deriveanyclass-causes-internal-error-inittcwithgbl-failed
Copy link
Collaborator

@michaelpj michaelpj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Co-authored-by: Michael Peyton Jones <me@michaelpj.com>
@soulomoon soulomoon enabled auto-merge (squash) February 7, 2024 11:00
@soulomoon soulomoon merged commit cd959ae into master Feb 7, 2024
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeriveAnyClass causes "Internal Error: initTcWithGbl failed"
3 participants