Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/CodeGen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Data.Traversable
import DataCon
import Development.IDE.GHC.Compat
import GHC.Exts
import GHC.SourceGen (RdrNameStr)
import GHC.SourceGen (recordCon, RdrNameStr)
import GHC.SourceGen.Binds
import GHC.SourceGen.Expr
import GHC.SourceGen.Overloaded
Expand Down Expand Up @@ -187,8 +187,8 @@ buildDataCon
-> DataCon -- ^ The data con to build
-> [Type] -- ^ Type arguments for the data con
-> RuleM (Trace, LHsExpr GhcPs)
buildDataCon jdg dc apps = do
let args = dataConInstOrigArgTys' dc apps
buildDataCon jdg dc tyapps = do
let args = dataConInstOrigArgTys' dc tyapps
(tr, sgs)
<- fmap unzipTrace
$ traverse ( \(arg, n) ->
Expand All @@ -210,6 +210,10 @@ mkCon dcon (fmap unLoc -> args)
| dataConIsInfix dcon
, (lhs : rhs : args') <- args =
noLoc $ foldl' (@@) (op lhs (coerceName dcon_name) rhs) args'
| Just fields <- getRecordFields dcon =
noLoc $ recordConE (coerceName dcon_name) $ do
(arg, (field, _)) <- zip args fields
pure (coerceName field, arg)
| otherwise =
noLoc $ foldl' (@@) (bvar' $ occName dcon_name) args
where
Expand Down
14 changes: 13 additions & 1 deletion plugins/hls-tactics-plugin/src/Ide/Plugin/Tactic/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Control.Monad.State
import qualified Data.Map as M
import Data.Maybe (isJust)
import Data.Traversable
import qualified DataCon as DataCon
import DataCon
import Development.IDE.GHC.Compat
import Generics.SYB (mkT, everywhere)
import Ide.Plugin.Tactic.Types
Expand Down Expand Up @@ -88,6 +88,18 @@ freshTyvars t = do
) t


------------------------------------------------------------------------------
-- | Given a datacon, extract its record fields' names and types. Returns
-- nothing if the datacon is not a record.
getRecordFields :: DataCon -> Maybe [(OccName, CType)]
getRecordFields dc =
case dataConFieldLabels dc of
[] -> Nothing
lbls -> for lbls $ \lbl -> do
(_, ty) <- dataConFieldType_maybe dc $ flLabel lbl
pure (mkVarOccFS $ flLabel lbl, CType ty)


------------------------------------------------------------------------------
-- | Is this an algebraic type?
algebraicTyCon :: Type -> Maybe TyCon
Expand Down
1 change: 1 addition & 0 deletions test/functional/Tactic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ tests = testGroup
, expectFail "GoldenFish.hs" 5 18 Auto ""
, goldenTest "GoldenArbitrary.hs" 25 13 Auto ""
, goldenTest "FmapBoth.hs" 2 12 Auto ""
, goldenTest "RecordCon.hs" 7 8 Auto ""
]


Expand Down
9 changes: 9 additions & 0 deletions test/testdata/tactic/RecordCon.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data MyRecord a = Record
{ field1 :: a
, field2 :: Int
}

blah :: (a -> Int) -> a -> MyRecord a
blah = _


9 changes: 9 additions & 0 deletions test/testdata/tactic/RecordCon.hs.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data MyRecord a = Record
{ field1 :: a
, field2 :: Int
}

blah :: (a -> Int) -> a -> MyRecord a
blah = (\ fai a -> Record {field1 = a, field2 = fai a})