-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,8 +119,8 @@ import Language.Haskell.TH.Syntax | |
import Web.HttpApiData (FromHttpApiData(..), ToHttpApiData(..)) | ||
import Web.PathPieces (PathPiece(..)) | ||
|
||
import Database.Persist.Class.PersistEntity | ||
import Database.Persist | ||
import Database.Persist.Class.PersistEntity | ||
import Database.Persist.Quasi | ||
import Database.Persist.Quasi.Internal | ||
import Database.Persist.Sql | ||
|
@@ -1462,9 +1462,15 @@ fieldUpd :: Name -- ^ constructor name | |
-> Exp -- ^ record value | ||
-> Name -- ^ field name to update | ||
-> Exp -- ^ new value | ||
-> Exp | ||
fieldUpd con names record name new = CaseE record | ||
[ Match (RecP con pats) (NormalB body) []] | ||
-> Q Exp | ||
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) []] | ||
Comment on lines
+1466
to
+1473
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the function that was introducing the problem. |
||
where | ||
body = RecConE con | ||
[ if k == name then (name, new) else (k, VarE k) | ||
|
@@ -1486,23 +1492,26 @@ mkLensClauses mps entDef genDataType = do | |
let idClause = normalClause | ||
[conp (keyIdName entDef) []] | ||
(lens' `AppE` getId `AppE` setId) | ||
return $ idClause : if unboundEntitySum entDef | ||
then fmap (toSumClause lens' keyVar valName xName) (getUnboundFieldDefs entDef) | ||
else zipWith (toClause lens' getVal dot keyVar valName xName) (getUnboundFieldDefs entDef) fieldNames | ||
(idClause :) <$> if unboundEntitySum entDef | ||
then pure $ fmap (toSumClause lens' keyVar valName xName) (getUnboundFieldDefs entDef) | ||
else zipWithM (toClause lens' getVal dot keyVar valName xName) (getUnboundFieldDefs entDef) fieldNames | ||
where | ||
fieldNames = fieldDefToRecordName mps entDef <$> getUnboundFieldDefs entDef | ||
toClause lens' getVal dot keyVar valName xName fieldDef fieldName = normalClause | ||
[conp (filterConName mps entDef fieldDef) []] | ||
(lens' `AppE` getter `AppE` setter) | ||
toClause lens' getVal dot keyVar valName xName fieldDef fieldName = do | ||
setter <- mkSetter | ||
pure $ normalClause | ||
[conp (filterConName mps entDef fieldDef) []] | ||
(lens' `AppE` getter `AppE` setter) | ||
where | ||
defName = mkEntityDefName entDef | ||
getter = InfixE (Just $ fieldSel defName fieldName) dot (Just getVal) | ||
setter = LamE | ||
[ conp 'Entity [VarP keyVar, VarP valName] | ||
, VarP xName | ||
] | ||
$ ConE 'Entity `AppE` VarE keyVar | ||
`AppE` fieldUpd defName fieldNames (VarE valName) fieldName (VarE xName) | ||
mkSetter = do | ||
updExpr <- fieldUpd defName fieldNames (VarE valName) fieldName (VarE xName) | ||
pure $ LamE | ||
[ conp 'Entity [VarP keyVar, VarP valName] | ||
, VarP xName | ||
] | ||
$ ConE 'Entity `AppE` VarE keyVar `AppE` updExpr | ||
|
||
toSumClause lens' keyVar valName xName fieldDef = normalClause | ||
[conp (filterConName mps entDef fieldDef) []] | ||
|
@@ -2167,6 +2176,7 @@ mkLenses mps entityMap ent = fmap mconcat $ forM (getUnboundFieldDefs ent `zip` | |
t1 `arrow` t2 = ArrowT `AppT` t1 `AppT` t2 | ||
vars = mkForallTV fT | ||
: (if mpsGeneric mps then [mkForallTV backend1{-, PlainTV backend2-}] else []) | ||
fieldUpdClause <- fieldUpd (mkEntityDefName ent) fieldNames a fieldName y | ||
return | ||
[ SigD lensName $ ForallT vars [mkClassP ''Functor [VarT fT]] $ | ||
(aT `arrow` (VarT fT `AppT` bT)) `arrow` | ||
|
@@ -2179,7 +2189,7 @@ mkLenses mps entityMap ent = fmap mconcat $ forM (getUnboundFieldDefs ent `zip` | |
[ FunD needleN [normalClause [] (fieldSel (mkEntityDefName ent) fieldName `AppE` a)] | ||
, FunD setterN $ return $ normalClause | ||
[VarP yN] | ||
(fieldUpd (mkEntityDefName ent) fieldNames a fieldName y) | ||
fieldUpdClause | ||
] | ||
] | ||
where | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. This warning is the 'test' for the feature. |
||
|
||
module Database.Persist.TH.OverloadedLabelSpec where | ||
|
||
import TemplateTestImports | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.