[Git][ghc/ghc][wip/better-error-for-chr] base: improve error message for Data.Char.chr
Bodigrim pushed to branch wip/better-error-for-chr at Glasgow Haskell Compiler / GHC Commits: 87db1207 by Andrew Lelechenko at 2026-03-28T11:14:15+00:00 base: improve error message for Data.Char.chr As per https://github.com/haskell/core-libraries-committee/issues/384 - - - - - 5 changed files: - libraries/base/changelog.md - libraries/base/tests/enum01.stdout - libraries/base/tests/enum01.stdout-alpha-dec-osf3 - libraries/base/tests/enum01.stdout-ws-64 - libraries/ghc-internal/src/GHC/Internal/Char.hs Changes: ===================================== libraries/base/changelog.md ===================================== @@ -28,6 +28,7 @@ * Hide implementation details when throwing exceptions in throw and throwSTM. ([CLC proposal #387](https://github.com/haskell/core-libraries-committee/issues/387)) * Change `hIsReadable` and `hIsWritable` such that they always throw a respective exception when encountering a closed or semi-closed handle, not just in the case of a file handle. ([CLC proposal #371](github.com/haskell/core-libraries-committee/issues/371)) * Annotate `onException` continuation with `WhileHandling`. ([CLC Proposal #397](https://github.com/haskell/core-libraries-committee/issues/397)) + * Improve error message for `Data.Char.chr`. ([CLC Proposal #384](https://github.com/haskell/core-libraries-committee/issues/384)) ## 4.22.0.0 *TBA* * Shipped with GHC 9.14.1 ===================================== libraries/base/tests/enum01.stdout ===================================== @@ -81,7 +81,7 @@ Testing Enum Char: pred (maxBound::Char) = '\1114110' pred (minBound::Char) = error "Prelude.Enum.Char.pred: bad argument" (map (toEnum::Int->Char) [123,ord (minBound::Char), ord(maxBound::Char)]) = "{\NUL\1114111" - (toEnum::Int->Char) (minBound::Int) = error "Prelude.chr: bad argument: (-2147483648)" + (toEnum::Int->Char) (minBound::Int) = error "Data.Char.chr: argument outside Unicode range: 0..1114111: (-2147483648)" (map fromEnum ['X',minBound,maxBound]) = [88,0,1114111] (take 7 ['\NUL' .. ]) = "\NUL\SOH\STX\ETX\EOT\ENQ\ACK" (take 7 ['\250' .. ]) = "\250\251\252\253\254\255\256" ===================================== libraries/base/tests/enum01.stdout-alpha-dec-osf3 ===================================== @@ -65,7 +65,7 @@ Testing Enum Char: pred (maxBound::Char) = '\1114110' pred (minBound::Char) = error "Prelude.Enum.Char.pred: bad argument" (map (toEnum::Int->Char) [123,ord (minBound::Char), ord(maxBound::Char)]) = "{\NUL\1114111" - (toEnum::Int->Char) (minBound::Int) = error "Prelude.chr: bad argument" + (toEnum::Int->Char) (minBound::Int) = error "Data.Char.chr: argument outside Unicode range: 0..1114111:" (map fromEnum ['X',minBound,maxBound]) = [88,0,1114111] (take 7 ['\NUL' .. ]) = "\NUL\SOH\STX\ETX\EOT\ENQ\ACK" (take 7 ['\250' .. ]) = "\250\251\252\253\254\255\256" ===================================== libraries/base/tests/enum01.stdout-ws-64 ===================================== @@ -81,7 +81,7 @@ Testing Enum Char: pred (maxBound::Char) = '\1114110' pred (minBound::Char) = error "Prelude.Enum.Char.pred: bad argument" (map (toEnum::Int->Char) [123,ord (minBound::Char), ord(maxBound::Char)]) = "{\NUL\1114111" - (toEnum::Int->Char) (minBound::Int) = error "Prelude.chr: bad argument: (-9223372036854775808)" + (toEnum::Int->Char) (minBound::Int) = error "Data.Char.chr: argument outside Unicode range: 0..1114111: (-9223372036854775808)" (map fromEnum ['X',minBound,maxBound]) = [88,0,1114111] (take 7 ['\NUL' .. ]) = "\NUL\SOH\STX\ETX\EOT\ENQ\ACK" (take 7 ['\250' .. ]) = "\250\251\252\253\254\255\256" ===================================== libraries/ghc-internal/src/GHC/Internal/Char.hs ===================================== @@ -12,7 +12,7 @@ module GHC.Internal.Char import GHC.Internal.Classes (eqChar, neChar) import GHC.Internal.Base (otherwise, (++)) -import GHC.Internal.Err (errorWithoutStackTrace) +import GHC.Internal.Err (error) import GHC.Internal.Show import GHC.Internal.Prim (chr#, int2Word#, leWord#, Int#, Char#) import GHC.Internal.Types (Char(..), Int(..), isTrue#) @@ -29,4 +29,7 @@ safe_chr# i# {-# NOINLINE chr_error #-} chr_error :: Int# -> Char# -chr_error i# = errorWithoutStackTrace ("Prelude.chr: bad argument: " ++ showSignedInt (I# 9#) (I# i#) "") +chr_error i# = error ("Data.Char.chr: argument outside Unicode range: 0..1114111: " ++ showSignedInt (I# 9#) (I# i#) "") +-- It's not really "Data.Char", but we assume that +-- the majority of users will import it from "base:Data.Char" +-- and not from "ghc-internal:GHC.Internal.Char". View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/87db120721d429258a598430ec23a029... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/87db120721d429258a598430ec23a029... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Bodigrim (@Bodigrim)