Skip to content

Char.fromCode throws a RangeError out of range, but its documentation says it returns U+FFFD #149

Description

@gilramir

Found against: gren 0.6.6, gren-lang/core 7.4.2, node 2

Summary

Char.fromCode : Int -> Char's doc comment says what a number outside Unicode's
range of codepoints returns:

The full range of unicode is from 0 to 0x10FFFF. With numbers outside that
range, you get [the replacement character][fffd].

with fromCode -1 == '�' as its own example. It throws instead:
_Char_fromCode is __Utils_chr(String.fromCodePoint(code)), and
String.fromCodePoint raises a RangeError below 0 and above 0x10FFFF.

The documentation is accurate about Elm, which the module was derived from —
elm/core's kernel guards the range and returns U+FFFD — and Gren's rewrite to
String.fromCodePoint dropped the guard along with the surrogate arithmetic it
replaced. Nothing in the doc comment was changed, so the two have disagreed
since.

Reproduction

> Char.fromCode 65
'A' : Char
> Char.toCode (Char.fromCode 0xD800)
55296 : Int
> Char.fromCode -1
RangeError: Invalid code point -1
> Char.fromCode 0x110000
RangeError: Invalid code point 1114112
expression documented actual
Char.fromCode 65 'A' 'A'
Char.fromCode 0xD800 not mentioned a Char holding a lone surrogate
Char.fromCode -1 '\u{FFFD}' RangeError: Invalid code point -1
Char.fromCode 0x110000 '\u{FFFD}' RangeError: Invalid code point 1114112

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions