Commit 2f82da76 authored by bollmann's avatar bollmann Committed by Ben Gamari

Fix Template Haskell bug reported in #11809.

Record selectors of data types spliced in with Template Haskell are not
renamer-resolved correctly in GHC HEAD. The culprit is
`newRecordSelector` which violates notes `Note [Binders in Template
Haskell] in Convert.hs` and `Note [Looking up Exact RdrNames] in
RnEnv.hs`. This commit fixes `newRecordSelector` accordingly.

Test Plan: ./validate

Reviewers: thomie, mpickering, bgamari, austin, simonpj, goldfire

Reviewed By: goldfire

Differential Revision: https://phabricator.haskell.org/D2091

GHC Trac Issues: #11809
parent 6d7fda5e
......@@ -685,13 +685,20 @@ getLocalNonValBinders fixity_env
newRecordSelector :: Bool -> [Name] -> LFieldOcc RdrName -> RnM FieldLabel
newRecordSelector _ [] _ = error "newRecordSelector: datatype has no constructors!"
newRecordSelector overload_ok (dc:_) (L loc (FieldOcc (L _ fld) _)) =
do { sel_name <- newTopSrcBinder $ L loc $ mkRdrUnqual sel_occ
; return $ fl { flSelector = sel_name } }
newRecordSelector overload_ok (dc:_) (L loc (FieldOcc (L _ fld) _))
= do { selName <- newTopSrcBinder $ L loc $ field
; return $ qualFieldLbl { flSelector = selName } }
where
lbl = occNameFS $ rdrNameOcc fld
fl = mkFieldLabelOccs lbl (nameOccName dc) overload_ok
sel_occ = flSelector fl
fieldOccName = occNameFS $ rdrNameOcc fld
qualFieldLbl = mkFieldLabelOccs fieldOccName (nameOccName dc) overload_ok
field | isExact fld = fld
-- use an Exact RdrName as is to preserve the bindings
-- of an already renamer-resolved field and its use
-- sites. This is needed to correctly support record
-- selectors in Template Haskell. See Note [Binders in
-- Template Haskell] in Convert.hs and Note [Looking up
-- Exact RdrNames] in RnEnv.hs.
| otherwise = mkRdrUnqual (flSelector qualFieldLbl)
{-
Note [Looking up family names in family instances]
......
{-# LANGUAGE TemplateHaskell #-}
module T11809 where
{- Test splicing in a data type with records -}
[d|
data D a = MkD { unD :: a }
someD = MkD "Hello"
getD = unD someD -- unD should resolve to the record selector above!
|]
getD' = unD someD -- dito here outside of the splice!
......@@ -391,9 +391,9 @@ test('T10819',
test('T10820', normal, compile_and_run, ['-v0'])
test('T11341', normal, compile, ['-v0 -dsuppress-uniques'])
test('T11345', normal, compile_and_run, ['-v0 -dsuppress-uniques'])
test('TH_finalizer', normal, compile, ['-v0'])
test('T10603', normal, compile, ['-ddump-splices -dsuppress-uniques'])
test('T11452', normal, compile_fail, ['-v0'])
test('T9022', normal, compile_and_run, ['-v0'])
test('T11145', normal, compile_fail, ['-v0 -dsuppress-uniques'])
test('T11809', normal, compile, ['-v0'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment