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

Print range computed by expression range analysis in error messages #18615

Open
dlangBugzillaToGithub opened this issue Jun 23, 2013 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-06-23T13:11:24Z

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

Description

A low-priority enhancement request.


For this code:

void main() {
    ubyte x;
    ubyte y = x << 1;
}


The range analysis determines that it's conceivable to the result of that expression to not fit in y, so the D compiler 2.064alpha gives:


temp.d(3): Error: cannot implicitly convert expression (cast(int)x << 1) of type int to ubyte


To help the programmer understand faster the mistake in his/her code when expressions become more complex I think it's also useful to print the range resulting from the analysis, like:


temp.d(3): Error: cannot implicitly convert expression (cast(int)x << 1) in interval [0 ... 510] of type int to ubyte


It uses 3 dots because it's an interval that includes the right end. Otherwise if you print an interval open on the right in a case like this you have to print a ulong.max+1 value:


void main() {
    ulong x;
    int y = x;
}


One downside: those error messages become a little longer (and a bit more noisy).
@dlangBugzillaToGithub
Copy link
Author

bearophile_hugs commented on 2013-06-23T14:08:52Z

> temp.d(3): Error: cannot implicitly convert expression (cast(int)x << 1) in
> interval [0 ... 510] of type int to ubyte

Jonathan M Davis suggests to use a more mathematical syntax to denote an interval closed on the right, like:

temp.d(3): Error: cannot implicitly convert expression (cast(int)x << 1) in interval [0, 510] of type int to ubyte

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