[Git][ghc/ghc][wip/int-index/list-tuple-cleanup] 3 commits: Fix .stdout-mingw32

Vladislav Zavialov (@int-index) gitlab at gitlab.haskell.org
Wed Jan 22 09:42:06 UTC 2025



Vladislav Zavialov pushed to branch wip/int-index/list-tuple-cleanup at Glasgow Haskell Compiler / GHC


Commits:
a60830e3 by Vladislav Zavialov at 2025-01-21T15:43:57+03:00
Fix .stdout-mingw32

- - - - -
77c42729 by Vladislav Zavialov at 2025-01-22T00:23:07+03:00
Check namespaces and arity ranges

- - - - -
b2ece451 by Vladislav Zavialov at 2025-01-22T00:23:39+03:00
Comments only

- - - - -


3 changed files:

- compiler/GHC/Builtin/Types.hs
- compiler/GHC/Types/Name/Cache.hs
- testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32


Changes:

=====================================
compiler/GHC/Builtin/Types.hs
=====================================
@@ -847,12 +847,20 @@ Note [isBuiltInOcc_maybe]
 syntax, i.e. unqualified names that can be unambiguously resolved even without
 knowing what's currently in scope (such names also can't be imported, exported,
 or redefined in another module).
+More on that in Note [Built-in syntax and the OrigNameCache] in GHC.Types.Name.Cache.
 
-* Use case #1: making TH's `mkName` work with built-in syntax,
-  e.g. $(conT (mkName "[]")) is the same as []
+In GHC, there are two use cases for `isBuiltInOcc_maybe`:
 
-* Use case #2: detecting bulit-in syntax in `infix` declarations,
-  e.g. users can't write `infixl 6 :` (#15233)
+1. Making TH's `mkName` work with built-in syntax,
+   e.g. $(conT (mkName "[]")) is the same as []
+
+2. Detecting bulit-in syntax in `infix` declarations,
+   e.g. users can't write `infixl 6 :` (#15233)
+
+The parser takes a shortcut and produces Exact RdrNames directly,
+so it doesn't need to match on an OccName with isBuiltInOcc_maybe.
+
+And here are the properties of `isBuiltInOcc_maybe`:
 
 * The set of names recognized by `isBuiltInOcc_maybe` is essentialy the
   same as the set of names that the parser resolves to Exact RdrNames,
@@ -889,16 +897,23 @@ or redefined in another module).
 * The /input/ to `isBuiltInOcc_maybe` needs to be built-in syntax for the
   predicate to match, but the /output/ is not necessarily built-in syntax.
   For example,
-    1) listTyConName
-          input:  mkOccName tcClsName "[]"   ("[]" is built-in syntax)
-          output: GHC.Types.List             ("List" is a normal name)
-    2) tupleTyConName
-          input:  mkOccName tcClsName "(,)"  ("(,)" is built-in syntax)
-          output: GHC.Tuple.Tuple2           ("Tuple2" is a normal name)
-    3) unboxedSumTyConName
-          input:  mkOccName tcClsName "(#|#)" ("(#|#)" is built-in syntax)
-          output: GHC.Types.Sum2#
-  Therefore, `GHC.Types.Name.isBuiltInSyntax` may not hold for the name
+
+    1) input:   mkTcOcc "[]"          -- built-in syntax
+       output:  Just listTyConName    -- user syntax (GHC.Types.List)
+
+    2) input:   mkDataOcc "[]"        -- built-in syntax
+       output:  Just nilDataConName   -- built-in syntax []
+
+    3) input:   mkTcOcc "List"        -- user syntax
+       output:  Nothing               -- no match
+
+    4) input:   mkTcOcc "(,)"                       -- built-in syntax
+       output:  Just (tupleTyConName BoxedTuple 2)  -- user syntax (GHC.Types.Tuple2)
+
+    5) input:   mkTcOcc "(#|#)"               -- built-in syntax
+       output:  Just (unboxedSumTyConName 2)  -- user syntax (GHC.Types.Sum2#)
+
+  Therefore, `GHC.Types.Name.isBuiltInSyntax` may or may not hold for the name
   returned by `isBuiltInOcc_maybe`.
 -}
 
@@ -1037,20 +1052,21 @@ sbs_unboxed !sbs =
     n = SBS.length sbs -- O(1)
 
 -- (sbs_Sum sbs) checks if the string has form "SumN#" or "SumNM#",
--- where N,M are decimal digits ['0'..'9'], and return the corresponding arity.
+-- where "N" or "NM" is a decimal numeral in the [2..mAX_SUM_SIZE] range.
 sbs_Sum :: SBS.ShortByteString -> Maybe Arity
 sbs_Sum !sbs
   | n >= 3 && SBS.unsafeIndex sbs 0 == 83   -- ord 'S'
            && SBS.unsafeIndex sbs 1 == 117  -- ord 'u'
            && SBS.unsafeIndex sbs 2 == 109  -- ord 'm'
   , Just (Unboxed, arity) <- sbs_arity_boxity sbs 3
+  , arity >= 2, arity <= mAX_SUM_SIZE
   = Just arity
   | otherwise = Nothing
   where
     n = SBS.length sbs -- O(1)
 
 -- (sbs_Tuple sbs) checks if the string has form "TupleN", "TupleNM", "TupleN#" or "TupleNM#",
--- where N,M are decimal digits ['0'..'9'], and return the corresponding boxity and arity.
+-- where "N" or "NM" is a decimal numeral in the [2..mAX_TUPLE_SIZE] range.
 sbs_Tuple :: SBS.ShortByteString -> Maybe (Boxity, Arity)
 sbs_Tuple !sbs
   | n >= 5 && SBS.unsafeIndex sbs 0 == 84   -- ord 'T'
@@ -1058,13 +1074,15 @@ sbs_Tuple !sbs
            && SBS.unsafeIndex sbs 2 == 112  -- ord 'p'
            && SBS.unsafeIndex sbs 3 == 108  -- ord 'l'
            && SBS.unsafeIndex sbs 4 == 101  -- ord 'e'
-  = sbs_arity_boxity sbs 5
+  , Just r@(_, arity) <- sbs_arity_boxity sbs 5
+  , arity >= 2, arity <= mAX_TUPLE_SIZE
+  = Just r
   | otherwise = Nothing
   where
     n = SBS.length sbs -- O(1)
 
 -- (sbs_CTuple sbs) checks if the string has form "CTupleN" or "CTupleNM",
--- where N,M are decimal digits ['0'..'9'], and return the corresponding arity.
+-- where "N" or "NM" is a decimal numeral in the [2..mAX_CTUPLE_SIZE] range.
 sbs_CTuple :: SBS.ShortByteString -> Maybe Arity
 sbs_CTuple !sbs
   | n >= 6 && SBS.unsafeIndex sbs 0 == 67   -- ord 'C'
@@ -1074,13 +1092,14 @@ sbs_CTuple !sbs
            && SBS.unsafeIndex sbs 4 == 108  -- ord 'l'
            && SBS.unsafeIndex sbs 5 == 101  -- ord 'e'
   , Just (Boxed, arity) <- sbs_arity_boxity sbs 6
+  , arity >= 2, arity <= mAX_CTUPLE_SIZE
   = Just arity
   | otherwise = Nothing
   where
     n = SBS.length sbs -- O(1)
 
 -- (sbs_arity_boxity sbs i) parses bytes from position `i` to the end,
--- matching single- and double-digit decimal numbers (i.e. from 0 to 99)
+-- matching single- and double-digit decimals numerals (i.e. from 0 to 99)
 -- possibly followed by '#'. See Note [Small Ints parsing]
 sbs_arity_boxity :: SBS.ShortByteString -> Int -> Maybe (Boxity, Arity)
 sbs_arity_boxity !sbs !i =
@@ -1119,39 +1138,94 @@ sbs_arity_boxity !sbs !i =
       = Just (Unboxed, from_digit x1 * 10 + from_digit x2)
     parse3 _ _ _ = Nothing
 
+-- Identify original names of boxed and unboxed tuple type constructors.
+-- Examples:
+--   0b) isTupleTyOrigName_maybe GHC.Tuple (mkTcOcc "Unit")    =  Just <wired-in Name for 0-tuples>
+--   1b) isTupleTyOrigName_maybe GHC.Tuple (mkTcOcc "Solo")    =  Just <wired-in Name for 1-tuples>
+--   2b) isTupleTyOrigName_maybe GHC.Tuple (mkTcOcc "Tuple2")  =  Just <wired-in Name for 2-tuples>
+--   0u) isTupleTyOrigName_maybe GHC.Types (mkTcOcc "Unit#")   =  Just <wired-in Name for unboxed 0-tuples>
+--   1u) isTupleTyOrigName_maybe GHC.Types (mkTcOcc "Solo#")   =  Just <wired-in Name for unboxed 1-tuples>
+--   2u) isTupleTyOrigName_maybe GHC.Types (mkTcOcc "Tuple2#") =  Just <wired-in Name for unboxed 2-tuples>
+--   ...
+--   64b) isTupleTyOrigName_maybe GHC.Tuple (mkTcOcc "Tuple64")  =  Just <wired-in Name for 64-tuples>
+--   64u) isTupleTyOrigName_maybe GHC.Types (mkTcOcc "Tuple64#") =  Just <wired-in Name for unboxed 64-tuples>
+--
+-- Non-examples: "()", "(##)", "(,)", "(#,#)", "(,,)", "(#,,#)", etc.
+-- As far as tuple /types/ are concerned, these are not the original names
+-- but rather punned names under ListTuplePuns.
+--
+-- Also non-examples: "Tuple0", "Tuple0#", "Tuple1", and "Tuple1#".
+-- These are merely type synonyms for "Unit", "Unit#", "Solo", and "Solo#".
 isTupleTyOrigName_maybe :: Module -> OccName -> Maybe Name
 isTupleTyOrigName_maybe mod occ
-  | mod == gHC_INTERNAL_TUPLE || mod == gHC_TYPES
-  = match_occ
+  | mod == gHC_INTERNAL_TUPLE = match_occ_boxed
+  | mod == gHC_TYPES          = match_occ_unboxed
   where
     fs  = occNameFS occ
+    ns  = occNameSpace occ
     sbs = fastStringToShortByteString fs   -- O(1) field access
-    match_occ
+
+    match_occ_boxed
       | occ == occName unitTyConName = Just unitTyConName
       | occ == occName soloTyConName = Just soloTyConName
+      | isTcClsNameSpace ns, Just (boxity at Boxed, n) <- sbs_Tuple sbs, n >= 2
+      = Just (tyConName (tupleTyCon boxity n))
+      | otherwise = Nothing
+
+    match_occ_unboxed
       | occ == occName unboxedUnitTyConName = Just unboxedUnitTyConName
       | occ == occName unboxedSoloTyConName = Just unboxedSoloTyConName
-
-      | Just (boxity, n) <- sbs_Tuple sbs, n >= 2
+      | isTcClsNameSpace ns, Just (boxity at Unboxed, n) <- sbs_Tuple sbs, n >= 2
       = Just (tyConName (tupleTyCon boxity n))
-
       | otherwise = Nothing
+
 isTupleTyOrigName_maybe _ _ = Nothing
 
+-- Identify original names of boxed and unboxed tuple data constructors.
+-- Examples:
+--   0b) isTupleDataOrigName_maybe GHC.Tuple (mkDataOcc "()")      =  Just <wired-in Name for 0-tuples>
+--   1b) isTupleDataOrigName_maybe GHC.Tuple (mkDataOcc "MkSolo")  =  Just <wired-in Name for 1-tuples>
+--   2b) isTupleDataOrigName_maybe GHC.Tuple (mkDataOcc "(,)")     =  Just <wired-in Name for 2-tuples>
+--   ...
+--   0u) isTupleDataOrigName_maybe GHC.Types (mkDataOcc "(##)")    =  Just <wired-in Name for unboxed 0-tuples>
+--   1u) isTupleDataOrigName_maybe GHC.Types (mkDataOcc "MkSolo#") =  Just <wired-in Name for unboxed 1-tuples>
+--   2u) isTupleDataOrigName_maybe GHC.Types (mkDataOcc "(#,#)")   =  Just <wired-in Name for unboxed 2-tuples>
+--   ...
+--
+-- Non-examples: Tuple<n> or Tuple<n>#, as this is the name format of tuple /type/ constructors.
 isTupleDataOrigName_maybe :: Module -> OccName -> Maybe Name
 isTupleDataOrigName_maybe mod occ
-  | mod == gHC_INTERNAL_TUPLE || mod == gHC_TYPES
-  = match_occ
+  | mod == gHC_INTERNAL_TUPLE = match_occ_boxed
+  | mod == gHC_TYPES          = match_occ_unboxed
   where
-    match_occ
+    match_occ_boxed
       | occ == occName soloDataConName = Just soloDataConName
+      | isDataConNameSpace ns, Just n <- (is_boxed_tup_syntax fs)
+      = Just (tupleDataConName Boxed n)
+      | otherwise = Nothing
+    match_occ_unboxed
       | occ == occName unboxedSoloDataConName = Just unboxedSoloDataConName
-      | Just n <- (is_boxed_tup_syntax fs) = Just (tupleDataConName Boxed n)
-      | Just n <- (is_unboxed_tup_syntax fs) = Just (tupleDataConName Unboxed n)
+      | isDataConNameSpace ns, Just n <- (is_unboxed_tup_syntax fs)
+      = Just (tupleDataConName Unboxed n)
       | otherwise = Nothing
     fs = occNameFS occ
+    ns = occNameSpace occ
 isTupleDataOrigName_maybe _ _ = Nothing
 
+-- Identify original names of constraint tuples.
+-- Examples:
+--   0) isCTupleOrigName_maybe GHC.Classes (mkClsOcc "CUnit")    =  Just <wired-in Name for 0-ctuples>
+--   1) isCTupleOrigName_maybe GHC.Classes (mkClsOcc "CSolo")    =  Just <wired-in Name for 1-ctuples>
+--   2) isCTupleOrigName_maybe GHC.Classes (mkClsOcc "CTuple2")  =  Just <wired-in Name for 2-ctuples>
+--   ...
+--   64) isCTupleOrigName_maybe GHC.Classes (mkClsOcc "CTuple64")  =  Just <wired-in Name for 64-ctuples>
+--
+-- Non-examples: "()", "(,)", "(,,)", etc.
+-- As far as constraint tuples are concerned, these are not the original names
+-- but rather punned names under ListTuplePuns.
+--
+-- Also non-examples: "CTuple0" and "CTuple1".
+-- These are merely type synonyms for "CUnit" and "CSolo".
 isCTupleOrigName_maybe :: Module -> OccName -> Maybe Name
 isCTupleOrigName_maybe mod occ
   | mod == gHC_CLASSES
@@ -1170,22 +1244,47 @@ isCTupleOrigName_maybe mod occ
 
 isCTupleOrigName_maybe _ _ = Nothing
 
+-- Identify original names of unboxed sum type constructors.
+-- Examples:
+--   2) isSumTyOrigName_maybe GHC.Types (mkTcOcc "Sum2#") =  Just <wired-in Name for unboxed 2-sums>
+--   3) isSumTyOrigName_maybe GHC.Types (mkTcOcc "Sum3#") =  Just <wired-in Name for unboxed 3-sums>
+--   4) isSumTyOrigName_maybe GHC.Types (mkTcOcc "Sum4#") =  Just <wired-in Name for unboxed 4-sums>
+--   ...
+--   64) isSumTyOrigName_maybe GHC.Types (mkTcOcc "Sum64#") =  Just <wired-in Name for unboxed 64-sums>
+--
+-- Non-examples: "(#|#)", "(#||#)", "(#|||#)", etc. These are not valid syntax.
+-- Also non-examples: "Sum0#", "Sum1#". These do not exist.
 isSumTyOrigName_maybe :: Module -> OccName -> Maybe Name
 isSumTyOrigName_maybe mod occ
   | mod == gHC_TYPES
-  , Just n <- sbs_Sum sbs, n >= 2
+  , isTcClsNameSpace ns
+  , Just n <- sbs_Sum sbs
+  , n >= 2
   = Just (tyConName (sumTyCon n))
   where
     fs  = occNameFS occ
+    ns  = occNameSpace occ
     sbs = fastStringToShortByteString fs   -- O(1) field access
 isSumTyOrigName_maybe _ _ = Nothing
 
+-- Identify original names of unboxed sum data constructors.
+-- "(#_|#)", "(#_||#)", (#|_|#)"
+--
+-- Examples:
+--   1/2) isSumTyOrigName_maybe GHC.Types (mkDataOcc "(#_|#)")  =  Just <wired-in Name for 1st alt of unboxed 2-sums>
+--   1/3) isSumTyOrigName_maybe GHC.Types (mkDataOcc "(#_||#)") =  Just <wired-in Name for 1st alt of unboxed 3-sums>
+--   2/3) isSumTyOrigName_maybe GHC.Types (mkDataOcc "(#|_|#)") =  Just <wired-in Name for 2nd alt of unboxed 3-sums>
+--   ...
+--
+-- Non-examples: Sum<n>#, as this is the name format of unboxed sum /type/ constructors.
 isSumDataOrigName_maybe :: Module -> OccName -> Maybe Name
 isSumDataOrigName_maybe mod occ
   | mod == gHC_TYPES
+  , isDataConNameSpace ns
   , Just (k,n) <- (is_unboxed_sum_data_syntax fs)
   = Just (unboxedSumDataConName k n)
   where fs = occNameFS occ
+        ns = occNameSpace occ
 isSumDataOrigName_maybe _ _ = Nothing
 
 {-


=====================================
compiler/GHC/Types/Name/Cache.hs
=====================================
@@ -73,6 +73,8 @@ or exported. Such names come in two varieties:
     * unboxed sum type syntax `(#|#)`, `(#||#)`, `(#|||#)`, ...
     * unboxed sum data syntax `(#_|#)`, `(#|_#)`, `(#_||#), ...
 
+Concretely, a built-in name is a WiredIn Name that has a BuiltInSyntax flag.
+
 Historically, GHC used to avoid putting any built-in syntax in the OrigNameCache
 to avoid dealing with infinite families of names (tuples and sums). This measure
 has become inadequate with the introduction of NoListTuplePuns (GHC Proposal #475).


=====================================
testsuite/tests/interface-stability/ghc-experimental-exports.stdout-mingw32
=====================================
@@ -4474,6 +4474,7 @@ module GHC.PrimOps where
   type role MVar# nominal representational
   type MVar# :: forall {l :: Levity}. * -> TYPE (BoxedRep l) -> UnliftedType
   data MVar# a b
+  MkSolo# :: forall (k :: RuntimeRep) (a :: TYPE k). a -> (# a #)
   type MultMul :: Multiplicity -> Multiplicity -> Multiplicity
   type family MultMul a b where
     forall (x :: Multiplicity). MultMul One x = x
@@ -7485,9 +7486,9 @@ module Prelude.Experimental where
   data List a = ...
   pattern Solo :: forall a. a -> Solo a
   type Solo :: * -> *
-  data Solo a = ...
+  data Solo a = MkSolo a
   type Solo# :: forall (k :: GHC.Types.RuntimeRep). TYPE k -> TYPE (GHC.Types.TupleRep '[k])
-  data Solo# a = ...
+  data Solo# a = MkSolo# a
   type Sum10# :: forall (k0 :: GHC.Types.RuntimeRep) (k1 :: GHC.Types.RuntimeRep) (k2 :: GHC.Types.RuntimeRep) (k3 :: GHC.Types.RuntimeRep) (k4 :: GHC.Types.RuntimeRep) (k5 :: GHC.Types.RuntimeRep) (k6 :: GHC.Types.RuntimeRep) (k7 :: GHC.Types.RuntimeRep) (k8 :: GHC.Types.RuntimeRep) (k9 :: GHC.Types.RuntimeRep). TYPE k0 -> TYPE k1 -> TYPE k2 -> TYPE k3 -> TYPE k4 -> TYPE k5 -> TYPE k6 -> TYPE k7 -> TYPE k8 -> TYPE k9 -> TYPE (GHC.Types.SumRep [k0, k1, k2, k3, k4, k5, k6, k7, k8, k9])
   data Sum10# a b c d e f g h i j = ...
   type Sum11# :: forall (k0 :: GHC.Types.RuntimeRep) (k1 :: GHC.Types.RuntimeRep) (k2 :: GHC.Types.RuntimeRep) (k3 :: GHC.Types.RuntimeRep) (k4 :: GHC.Types.RuntimeRep) (k5 :: GHC.Types.RuntimeRep) (k6 :: GHC.Types.RuntimeRep) (k7 :: GHC.Types.RuntimeRep) (k8 :: GHC.Types.RuntimeRep) (k9 :: GHC.Types.RuntimeRep) (k10 :: GHC.Types.RuntimeRep). TYPE k0 -> TYPE k1 -> TYPE k2 -> TYPE k3 -> TYPE k4 -> TYPE k5 -> TYPE k6 -> TYPE k7 -> TYPE k8 -> TYPE k9 -> TYPE k10 -> TYPE (GHC.Types.SumRep [k0, k1, k2, k3, k4, k5, k6, k7, k8, k9, k10])



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/3b0854288ed74134f69062a3fb1cbdd97315d98b...b2ece4510741c628ce600c32dec8184dc736d265

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/3b0854288ed74134f69062a3fb1cbdd97315d98b...b2ece4510741c628ce600c32dec8184dc736d265
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20250122/86f0347e/attachment-0001.html>


More information about the ghc-commits mailing list