Commit 8936ab69 authored by Tao He's avatar Tao He Committed by Ben Gamari

Raise parse error for `data T where`.

Empty GADTs data declarations can't be identified in type checker. This
patch adds additional checks in parser and raise a parse error when
encounter empty GADTs declarations but extension `GADTs` is not enabled.

Only empty declarations are checked in parser to avoid affecting
existing
error messages related to missing GADTs extension.

This patch should fix issue 8258.
Signed-off-by: Tao He's avatarHE, Tao <sighingnow@gmail.com>

Test Plan: make test TEST="T8258 T8258NoGADTs"

Reviewers: bgamari, mpickering, alanz, RyanGlScott

Reviewed By: bgamari, RyanGlScott

Subscribers: adamse, RyanGlScott, rwbarton, thomie, mpickering, carter

GHC Trac Issues: #8258

Differential Revision: https://phabricator.haskell.org/D4350
parent 0c9777b7
......@@ -2087,14 +2087,17 @@ both become a HsTyVar ("Zero", DataName) after the renamer.
gadt_constrlist :: { Located ([AddAnn]
,[LConDecl GhcPs]) } -- Returned in order
: 'where' '{' gadt_constrs '}' { L (comb2 $1 $3)
([mj AnnWhere $1
,moc $2
,mcc $4]
, unLoc $3) }
| 'where' vocurly gadt_constrs close { L (comb2 $1 $3)
([mj AnnWhere $1]
, unLoc $3) }
: 'where' '{' gadt_constrs '}' {% checkEmptyGADTs $
L (comb2 $1 $3)
([mj AnnWhere $1
,moc $2
,mcc $4]
, unLoc $3) }
| 'where' vocurly gadt_constrs close {% checkEmptyGADTs $
L (comb2 $1 $3)
([mj AnnWhere $1]
, unLoc $3) }
| {- empty -} { noLoc ([],[]) }
gadt_constrs :: { Located [LConDecl GhcPs] }
......
......@@ -55,6 +55,7 @@ module RdrHsSyn (
checkValSigLhs,
checkDoAndIfThenElse,
checkRecordSyntax,
checkEmptyGADTs,
parseErrorSDoc, hintBangPat,
splitTilde, splitTildeApps,
......@@ -783,6 +784,21 @@ checkRecordSyntax lr@(L loc r)
(text "Illegal record syntax (use TraditionalRecordSyntax):" <+>
ppr r)
-- | Check if the gadt_constrlist is empty. Only raise parse error for
-- `data T where` to avoid affecting existing error message, see #8258.
checkEmptyGADTs :: Located ([AddAnn], [LConDecl GhcPs])
-> P (Located ([AddAnn], [LConDecl GhcPs]))
checkEmptyGADTs gadts@(L span (_, [])) -- Empty GADT declaration.
= do opts <- fmap options getPState
if LangExt.GADTSyntax `extopt` opts -- GADTs implies GADTSyntax
then return gadts
else parseErrorSDoc span $ vcat
[ text "Illegal keyword 'where' in data declaration"
, text "Perhaps you intended to use GADTs or a similar language"
, text "extension to enable syntax: data T where"
]
checkEmptyGADTs gadts = return gadts -- Ordinary GADT declaration.
checkTyClHdr :: Bool -- True <=> class header
-- False <=> type header
-> LHsType GhcPs
......
......@@ -22,6 +22,14 @@ Full details
Language
~~~~~~~~
- Data declarations with empty ``where`` clauses are no longer valid without the
extension :extension:`GADTSyntax` enabled. For instance, consider the
following, ::
data T where
The grammar is invalid in Haskell2010. Previously it could be compiled successfully
without ``GADTs``. As of GHC 8.6.1, this is a parse error.
Compiler
~~~~~~~~
......
{-# LANGUAGE GADTs #-}
module T8258 where
data T where
......@@ -100,6 +100,7 @@ test('T7118', normal, compile, [''])
test('T7776', normal, compile, [''])
test('RdrNoStaticPointers01', [], compile, [''])
test('T5682', normal, compile, [''])
test('T8258', normal, compile, [''])
test('T9723a', normal, compile, [''])
test('T9723b', normal, compile, [''])
test('T10188', normal, compile, [''])
......
module T8258NoGADTs where
data T where
T8258NoGADTs.hs:3:8: error:
Illegal keyword 'where' in data declaration
Perhaps you intended to use GADTs or a similar language
extension to enable syntax: data T where
......@@ -87,6 +87,7 @@ test('T5425', normal, compile_fail, [''])
test('T984', normal, compile_fail, [''])
test('T7848', normal, compile_fail, ['-dppr-user-length=100'])
test('ExportCommaComma', normal, compile_fail, [''])
test('T8258NoGADTs', normal, compile_fail, [''])
test('T8430', literate, compile_fail, [''])
test('T8431', compile_timeout_multiplier(0.05),
compile_fail, ['-XAlternativeLayoutRule'])
......
{-# LANGUAGE RankNTypes, TypeInType #-}
{-# LANGUAGE GADTs, RankNTypes, TypeInType #-}
module T11640 where
......
{-# LANGUAGE EmptyDataDecls, KindSignatures #-}
{-# LANGUAGE GADTs, EmptyDataDecls, KindSignatures #-}
module ShouldCompile where
......
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