-
Notifications
You must be signed in to change notification settings - Fork 292
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
Conversation
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) []] |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 #-} |
There was a problem hiding this comment.
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.
This PR fixes #1406 which noticed that the code generated name shadowing warnings.
The specific problem si that we'd generate code like:
where
fieldName
shadowedfieldName
.Before submitting your PR, check that you've:
@since
declarations to the Haddockstylish-haskell
on any changed files..editorconfig
file for details)After submitting your PR:
(unreleased)
on the Changelog