Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit conversion from uint to dchar allows dchar > dchar.max #17709

Open
dlangBugzillaToGithub opened this issue May 28, 2015 · 2 comments
Open

Comments

@dlangBugzillaToGithub
Copy link

Lionello Lunesu (@lionello) reported this on 2015-05-28T16:58:03Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=14627

CC List

Description

This code compiles & runs:

void main()
{
  uint u = ~0;
  dchar d = u;//implicit conversion
  assert(d > d.max);
}

It should use VRP to determine whether the range fits dchar range.
~
~
~
~
~
@dlangBugzillaToGithub
Copy link
Author

lio+bugzilla (@lionello) commented on 2015-05-28T20:06:59Z

There are two solutions to this:

1. Make dchar.max == uint.max

2. Make dchar = uint use range checks to determine whether implicit cast is safe


ad 1. This changes dchar.max, but there doesn't appear to be much code depending on it. In fact, Phobos has a couple `if (d > 0x10FFFF)` which don't currently make sense.

ad 2. Generate warning/deprecation when the range falls outside 0..0x10FFFF. breaks some code that assembles a dchar from wchar/char, and fixed by adding casts.

@dlangBugzillaToGithub
Copy link
Author

thomas.bockman commented on 2015-10-24T16:13:54Z

The issue of dchar.max versus uint.max has been discussed on the forums:
    http://forum.dlang.org/thread/qjmikijfluaniwnxhigp@forum.dlang.org

It was decided that D compilers must support dchar values which are outside the range of valid Unicode code points; otherwise handling encoding errors and the like gets very awkward. Besides, actually enforcing a range less than that which is actually representable by the binary format of dchar would be very difficult to do in a performant way.

In light of this, I think #1 (Make dchar.max == uint.max) is the correct solution. Allowing dchar.max to be less than its true maximum has probably caused subtle bugs in some generic code.

The value of the maximum code point (0x10FFFF) should be moved to a separate constant.

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

No branches or pull requests

1 participant