[Git][ghc/ghc][master] Consistent pretty-printing of HsString, HsIsString, HsStrTy
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: faf14e0c by Vladislav Zavialov at 2026-02-27T05:48:45-05:00 Consistent pretty-printing of HsString, HsIsString, HsStrTy Factor out a helper to pretty-print string literals, thus fixing newline handling for overloaded string literals and type literals. Test cases: T26860ppr T26860ppr_overloaded T26860ppr_tylit Follow up to ddf1434ff9bb08cfef3c93f23de6b83ec698aa27 - - - - - 7 changed files: - compiler/GHC/Hs/Lit.hs - compiler/GHC/Hs/Type.hs - + testsuite/tests/parser/should_fail/T26860ppr_overloaded.hs - + testsuite/tests/parser/should_fail/T26860ppr_overloaded.stderr - + testsuite/tests/parser/should_fail/T26860ppr_tylit.hs - + testsuite/tests/parser/should_fail/T26860ppr_tylit.stderr - testsuite/tests/parser/should_fail/all.T Changes: ===================================== compiler/GHC/Hs/Lit.hs ===================================== @@ -21,7 +21,7 @@ import GHC.Prelude import {-# SOURCE #-} GHC.Hs.Expr( pprExpr ) -import GHC.Data.FastString (unpackFS) +import GHC.Data.FastString (FastString, unpackFS) import GHC.Types.Basic (PprPrec(..), topPrec ) import GHC.Core.Ppr ( {- instance OutputableBndr TyVar -} ) import GHC.Types.SourceText @@ -209,10 +209,7 @@ Equivalently it's True if instance IsPass p => Outputable (HsLit (GhcPass p)) where ppr (HsChar st c) = pprWithSourceText st (pprHsChar c) ppr (HsCharPrim st c) = pprWithSourceText st (pprPrimChar c) - ppr (HsString st s) = - case st of - NoSourceText -> pprHsString s - SourceText src -> vcat $ map text $ split '\n' (unpackFS src) + ppr (HsString st s) = pprHsStringLit st s ppr (HsStringPrim st s) = pprWithSourceText st (pprHsBytes s) ppr (HsInt _ i) = pprWithSourceText (il_text i) (integer (il_value i)) @@ -233,6 +230,10 @@ instance IsPass p => Outputable (HsLit (GhcPass p)) where (HsInteger st i _) -> pprWithSourceText st (integer i) (HsRat f _) -> ppr f +pprHsStringLit :: SourceText -> FastString -> SDoc +pprHsStringLit NoSourceText s = pprHsString s +pprHsStringLit (SourceText src) _ = vcat $ map text $ split '\n' (unpackFS src) + -- in debug mode, print the expression that it's resolved to, too instance OutputableBndrId p => Outputable (HsOverLit (GhcPass p)) where @@ -242,7 +243,7 @@ instance OutputableBndrId p instance Outputable OverLitVal where ppr (HsIntegral i) = pprWithSourceText (il_text i) (integer (il_value i)) ppr (HsFractional f) = ppr f - ppr (HsIsString st s) = pprWithSourceText st (pprHsString s) + ppr (HsIsString st s) = pprHsStringLit st s negateOverLitVal :: OverLitVal -> OverLitVal negateOverLitVal (HsIntegral i) = HsIntegral (negateIntegralLit i) ===================================== compiler/GHC/Hs/Type.hs ===================================== @@ -116,6 +116,7 @@ import GHC.Core.Ppr ( pprOccWithTick) import GHC.Core.Type import GHC.Core.Multiplicity( pprArrowWithMultiplicity ) import GHC.Hs.Doc +import GHC.Hs.Lit (pprHsStringLit) import GHC.Generics (Generic, Generically(..)) import GHC.Types.Basic import GHC.Types.SrcLoc @@ -1346,7 +1347,7 @@ instance (OutputableBndrId pass) => OutputableBndr (GenLocated SrcSpan (FieldOcc ppr_tylit :: (HsTyLit (GhcPass p)) -> SDoc ppr_tylit (HsNumTy source i) = pprWithSourceText source (integer i) -ppr_tylit (HsStrTy source s) = pprWithSourceText source (text (show s)) +ppr_tylit (HsStrTy source s) = pprHsStringLit source s ppr_tylit (HsCharTy source c) = pprWithSourceText source (text (show c)) pprAnonWildCard :: SDoc ===================================== testsuite/tests/parser/should_fail/T26860ppr_overloaded.hs ===================================== @@ -0,0 +1,11 @@ +{-# LANGUAGE OverloadedStrings #-} + +module T26860ppr_overloaded where + +-- Test that the error message containing the string literal is well-formatted. +-- See also: parser/should_fail/MultilineStringsError +x :: Int +x = "first line \ + \asdf\n\ + \second line" + ===================================== testsuite/tests/parser/should_fail/T26860ppr_overloaded.stderr ===================================== @@ -0,0 +1,14 @@ +T26860ppr_overloaded.hs:8:5: error: [GHC-39999] + • No instance for ‘GHC.Internal.Data.String.IsString Int’ + arising from the literal ‘"first line \ + \asdf\n\ + \second line"’ + • In the expression: + "first line \ + \asdf\n\ + \second line" + In an equation for ‘x’: + x = "first line \ + \asdf\n\ + \second line" + ===================================== testsuite/tests/parser/should_fail/T26860ppr_tylit.hs ===================================== @@ -0,0 +1,13 @@ +{-# LANGUAGE DataKinds #-} + +module T26860ppr_tylit where + +import Data.Kind (Type) + +-- Test that the error message containing the string literal is well-formatted. +-- See also: parser/should_fail/MultilineStringsError +type X :: Type +type X = "first line \ + \asdf\n\ + \second line" + ===================================== testsuite/tests/parser/should_fail/T26860ppr_tylit.stderr ===================================== @@ -0,0 +1,11 @@ +T26860ppr_tylit.hs:10:10: error: [GHC-83865] + • Expected a type, + but ‘"first line \ + \asdf\n\ + \second line"’ has kind + ‘GHC.Internal.Types.Symbol’ + • In the type ‘"first line \ + \asdf\n\ + \second line"’ + In the type synonym declaration for ‘X’ + ===================================== testsuite/tests/parser/should_fail/all.T ===================================== @@ -245,3 +245,5 @@ test('T26418', normal, compile_fail, ['']) test('T12488c', normal, compile_fail, ['']) test('T12488d', normal, compile_fail, ['']) test('T26860ppr', normal, compile_fail, ['']) +test('T26860ppr_overloaded', normal, compile_fail, ['']) +test('T26860ppr_tylit', normal, compile_fail, ['']) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/faf14e0c020e9fb66207026110cce2fd... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/faf14e0c020e9fb66207026110cce2fd... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Marge Bot (@marge-bot)