Avoid parse error occurred when the field name generated by TH matches any of Haskell keywords#1476
Conversation
parsonsmatt
left a comment
There was a problem hiding this comment.
Thanks for writing this up! One minor performance quibble.
At a future point, it would be nice to customize this behavior.
Since anyone writing type as a field name currently will receive GHC error, I think we can safely call this a bugfix. Mind doing a patch version bump and adding a changelog notice?
persistent/Database/Persist/TH.hs
Outdated
| avoidKeyword name = if name `elem` keywords then name ++ "_" else name | ||
|
|
||
| keywords :: [Text] | ||
| keywords = | ||
| ["case","class","data","default","deriving","do","else" | ||
| ,"if","import","in","infix","infixl","infixr","instance","let","module" | ||
| ,"newtype","of","then","type","where","_" | ||
| ,"foreign" | ||
| ] |
There was a problem hiding this comment.
Lots of list lookups like this is pretty inefficient - would prefer to see a Set lookup
| avoidKeyword name = if name `elem` keywords then name ++ "_" else name | |
| keywords :: [Text] | |
| keywords = | |
| ["case","class","data","default","deriving","do","else" | |
| ,"if","import","in","infix","infixl","infixr","instance","let","module" | |
| ,"newtype","of","then","type","where","_" | |
| ,"foreign" | |
| ] | |
| avoidKeyword name = if name `Set.member` haskellKeywords then name ++ "_" else name | |
| haskellKeywords :: Set Text | |
| haskellKeywords = Set.fromList | |
| ["case","class","data","default","deriving","do","else" | |
| ,"if","import","in","infix","infixl","infixr","instance","let","module" | |
| ,"newtype","of","then","type","where","_" | |
| ,"foreign" | |
| ] |
Floating it to the top-level ensures we aren't constructing the Set for each call to mkRecordName, though I would guess GHC is capable of performing that optimization on it's own
There was a problem hiding this comment.
Thank you for your review, I changed the suggested part and added a notice to bump up a patch version in ChangeLog.
parsonsmatt
left a comment
There was a problem hiding this comment.
awesome, thanks! I'll release this today.
To avoid the problem that GHC fails compiling due to parse error when the field name generated by Template Haskell matches any of Haskell keywords, this PR fixes
mkRecordNameto suffix_if the field name matches any of keywords.Before submitting your PR, check that you've:
@sincedeclarations to the Haddockstylish-haskellon any changed files..editorconfigfile for details)After submitting your PR:
(unreleased)on the Changelog