diff options
author | Vladislav Zavialov <vlad.z.4096@gmail.com> | 2020-07-01 22:56:15 +0300 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-07-27 07:06:56 -0400 |
commit | aee45d9ea8c6cf4ebad4d5c732748923c7865cbe (patch) | |
tree | 3cb1952015aeacdc886ba142f23c97f9b29acae7 /testsuite/tests/parser | |
parent | 52685cf7c077c51e3719e3c4dd5ca8257a99c4ea (diff) | |
download | haskell-aee45d9ea8c6cf4ebad4d5c732748923c7865cbe.tar.gz |
Improve NegativeLiterals (#18022, GHC Proposal #344)
Before this patch, NegativeLiterals used to parse x-1 as x (-1).
This may not be what the user expects, and now it is fixed:
x-1 is parsed as (-) x 1.
We achieve this by the following requirement:
* When lexing a negative literal,
it must not be preceded by a 'closing token'.
This also applies to unboxed literals, e.g. -1#.
See GHC Proposal #229 for the definition of a closing token.
A nice consequence of this change is that -XNegativeLiterals becomes a
subset of -XLexicalNegation. In other words, enabling both of those
extensions has the same effect as enabling -XLexicalNegation alone.
Diffstat (limited to 'testsuite/tests/parser')
4 files changed, 98 insertions, 18 deletions
diff --git a/testsuite/tests/parser/should_compile/LexNegVsNegLit.hs b/testsuite/tests/parser/should_compile/LexNegVsNegLit.hs deleted file mode 100644 index 665893e95b..0000000000 --- a/testsuite/tests/parser/should_compile/LexNegVsNegLit.hs +++ /dev/null @@ -1,17 +0,0 @@ -{-# LANGUAGE NegativeLiterals, LexicalNegation #-} - -module LexNegVsNegLit where - --- NegativeLiterals specifies that we parse x-1 as x (-1), even though it's --- considered a shortcoming. --- --- LexicalNegation does not change that. --- -b :: Bool -b = even-1 -- parsed as: even (-1) - -- so it is well-typed. - -- - -- with LexicalNegation alone, we'd get (-) even 1, - -- but NegativeLiterals takes precedence here. - --- See also: GHC Proposal #344 diff --git a/testsuite/tests/parser/should_compile/NegativeLiterals.hs b/testsuite/tests/parser/should_compile/NegativeLiterals.hs new file mode 100644 index 0000000000..62d14ab678 --- /dev/null +++ b/testsuite/tests/parser/should_compile/NegativeLiterals.hs @@ -0,0 +1,57 @@ +{-# LANGUAGE NegativeLiterals, MagicHash, BinaryLiterals #-} + +module NegativeLiterals where + +import GHC.Exts + +------------------------------------ +-- Prefix occurrence of the minus -- +------------------------------------ + +p1 :: Bool +p1 = even -2 -- parsed as: even (-2) + +p2 :: Int +p2 = I# -1# -- parsed as: I# (-1#) + +p3 :: Int +p3 = floor -2.4 -- parsed as: floor (-2.4) + +p4 :: Float +p4 = F# -0.01# -- parsed as: F# (-0.01#) + +p5 :: Double +p5 = D# -0.01## -- parsed as: D# (-0.01##) + +p6 :: Bool +p6 = even -0b10 -- parsed as: even (-2) + || even -0o10 -- parsed as: even (-8) + || even -0x10 -- parsed as: even (-16) + +----------------------------------------- +-- Tight infix occurrence of the minus -- +----------------------------------------- + +ti1 :: Integer -> Integer +ti1 x = x-2 -- parsed as: (-) x 1 + +ti2 :: Int# -> Int# +ti2 x = x-1# -- parsed as: (-) x 1# + where (-) = (-#) + +ti3 :: Double -> Double +ti3 x = x-2.4 -- parsed as: (-) x 2.4 + +ti4 :: Float# -> Float# +ti4 x = x-0.1# -- parsed as: (-) x 0.1# + where (-) = minusFloat# + +ti5 :: Double# -> Double# +ti5 x = x-0.1## -- parsed as: (-) x 0.1## + where (-) = (-##) + +ti6 :: Integer -> [Integer] +ti6 x = + [ x-0b10, -- parsed as: (-) x 2 + x-0o10, -- parsed as: (-) x 8 + x-0x10 ] -- parsed as: (-) x 16 diff --git a/testsuite/tests/parser/should_compile/NegativeLiteralsNoExt.hs b/testsuite/tests/parser/should_compile/NegativeLiteralsNoExt.hs new file mode 100644 index 0000000000..9c23514d7d --- /dev/null +++ b/testsuite/tests/parser/should_compile/NegativeLiteralsNoExt.hs @@ -0,0 +1,39 @@ +{-# LANGUAGE NoNegativeLiterals, MagicHash, BinaryLiterals #-} + +-- Even when NegativeLiterals are disabled, +-- we parse unboxed literals appropriately. +module NegativeLiteralsNoExt where + +import GHC.Exts + +------------------------------------ +-- Prefix occurrence of the minus -- +------------------------------------ + +p2 :: Int +p2 = I# -1# -- parsed as: I# (-1#) + +p4 :: Float +p4 = F# -0.01# -- parsed as: F# (-0.01#) + +p5 :: Double +p5 = D# -0.01## -- parsed as: D# (-0.01##) + +----------------------------------------- +-- Tight infix occurrence of the minus -- +----------------------------------------- + +ti2 :: Int# -> Int# +ti2 x = x-1# -- parsed as: (-) x 1# + where (-) = (-#) + +ti3 :: Double -> Double +ti3 x = x-2.4 -- parsed as: (-) x 2.4 + +ti4 :: Float# -> Float# +ti4 x = x-0.1# -- parsed as: (-) x 0.1# + where (-) = minusFloat# + +ti5 :: Double# -> Double# +ti5 x = x-0.1## -- parsed as: (-) x 0.1## + where (-) = (-##) diff --git a/testsuite/tests/parser/should_compile/all.T b/testsuite/tests/parser/should_compile/all.T index fb2bd6b587..48e1136daa 100644 --- a/testsuite/tests/parser/should_compile/all.T +++ b/testsuite/tests/parser/should_compile/all.T @@ -153,7 +153,8 @@ test('proposal-229b', normal, compile, ['']) test('proposal-229d', normal, compile, ['']) test('proposal-229e', normal, compile, ['']) test('LexicalNegation', normal, compile, ['']) -test('LexNegVsNegLit', normal, compile, ['']) +test('NegativeLiterals', normal, compile, ['']) +test('NegativeLiteralsNoExt', normal, compile, ['']) # We omit 'profasm' because it fails with: # Cannot load -prof objects when GHC is built with -dynamic |