ApplicativeDo: Handle terminal `pure` statements
authorBen Gamari <bgamari.foss@gmail.com>
Thu, 25 Feb 2016 13:47:34 +0000 (14:47 +0100)
committerBen Gamari <ben@smart-cactus.org>
Thu, 25 Feb 2016 14:41:55 +0000 (15:41 +0100)
ApplicativeDo handled terminal `return` statements properly, but not
`pure`.

Test Plan: Validate with included testcase

Reviewers: austin, simonmar

Reviewed By: austin, simonmar

Subscribers: simonpj, thomie

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

GHC Trac Issues: #11607

compiler/rename/RnExpr.hs
docs/users_guide/glasgow_exts.rst
testsuite/tests/ado/T11607.hs [new file with mode: 0644]
testsuite/tests/ado/T11607.stdout [new file with mode: 0644]
testsuite/tests/ado/all.T

index 9d1200a..ce113b4 100644 (file)
@@ -1678,7 +1678,7 @@ isReturnApp (L _ (HsApp f arg))
   | otherwise = Nothing
  where
   is_return (L _ (HsPar e)) = is_return e
-  is_return (L _ (HsVar (L _ r))) = r == returnMName
+  is_return (L _ (HsVar (L _ r))) = r == returnMName || r == pureAName
        -- TODO: I don't know how to get this right for rebindable syntax
   is_return _ = False
 isReturnApp _ = Nothing
index fc7ec52..59e8b62 100644 (file)
@@ -906,7 +906,8 @@ is as follows. If the do-expression has the following form: ::
 
 where none of the variables defined by ``p1...pn`` are mentioned in ``E1...En``,
 then the expression will only require ``Applicative``. Otherwise, the expression
-will require ``Monad``.
+will require ``Monad``. The block may return a pure expression ``E`` depending
+upon the results ``p1...pn`` with either ``return`` or ``pure``.
 
 
 .. _applicative-do-pitfall:
diff --git a/testsuite/tests/ado/T11607.hs b/testsuite/tests/ado/T11607.hs
new file mode 100644 (file)
index 0000000..f2bb341
--- /dev/null
@@ -0,0 +1,10 @@
+{-# LANGUAGE ApplicativeDo #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
+
+newtype MaybeA a = MaybeA (Maybe a)
+                 deriving (Show, Functor, Applicative)
+
+main :: IO ()
+main = print $ do
+    x <- MaybeA $ Just 42
+    pure x
diff --git a/testsuite/tests/ado/T11607.stdout b/testsuite/tests/ado/T11607.stdout
new file mode 100644 (file)
index 0000000..1e6c1e1
--- /dev/null
@@ -0,0 +1 @@
+MaybeA (Just 42)
index 2ec3e34..e1efdf2 100644 (file)
@@ -5,3 +5,4 @@ test('ado004', normal, compile, [''])
 test('ado005', normal, compile_fail, [''])
 test('ado006', normal, compile, [''])
 test('ado007', normal, compile, [''])
+test('T11607', normal, compile_and_run, [''])