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 a name shadowing warning (#1406) #1407

Merged
merged 3 commits into from
Jun 10, 2022
Merged

Conversation

parsonsmatt
Copy link
Collaborator

@parsonsmatt parsonsmatt commented Jun 10, 2022

This PR fixes #1406 which noticed that the code generated name shadowing warnings.

The specific problem si that we'd generate code like:

case someRecord of
  Constructor { fieldName = fieldName } -> ...

where fieldName shadowed fieldName.

Before submitting your PR, check that you've:

  • Documented new APIs with Haddock markup
  • Added @since declarations to the Haddock
  • Ran stylish-haskell on any changed files.
  • Adhered to the code style (see the .editorconfig file for details)

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

Comment on lines +1466 to +1473
fieldUpd con names record name new = do
pats <-
fmap mconcat $ forM names $ \k -> do
varName <- VarP <$> newName (nameBase k)
pure [(k, varName) | k /= name]

pure $ CaseE record
[ Match (RecP con pats) (NormalB body) []]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the function that was introducing the problem.

fieldUpd con names record name new = do
pats <-
fmap mconcat $ forM names $ \k -> do
varName <- VarP <$> newName (nameBase k)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And, this line is the real fix. By creating a newName, we're guaranteeing that the variable is fresh and uncapturable and isn't shadowing anything.

@fumieval This is one of those tricky things about TemplateHaskell - it's so easy to use and make names that can cause warnings and bugs! I do it all the time and I really ought to write up a lil' how to on the matter.

@@ -13,6 +13,8 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wname-shadowing -Werror=name-shadowing #-}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This warning is the 'test' for the feature.

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.

Incorrect shadowed name warning
1 participant