Bug 10018 – Value range propagation for immutable variables

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-02T11:39:00Z
Last change time
2014-06-28T12:30:01Z
Keywords
pull
Assigned to
lio+bugzilla
Creator
lio+bugzilla

Comments

Comment #0 by lio+bugzilla — 2013-05-02T11:39:08Z
immutable int i = 1; //range from initializer does not propagate
Comment #1 by lio+bugzilla — 2013-05-02T12:32:38Z
https://github.com/D-Programming-Language/dmd/pull/1955 Not sure how to test this. There's no [portable] way of testing whether a range is being propagated, is there?
Comment #2 by andrej.mitrovich — 2013-05-02T12:33:18Z
I don't understand the bug, what's the issue here?
Comment #3 by lio+bugzilla — 2013-05-02T17:35:02Z
(In reply to comment #2) > I don't understand the bug, what's the issue here? Any calculation with 'i' that uses VRP get the full int range, not the constant 1. The same holds for 'const int'.
Comment #4 by lio+bugzilla — 2013-05-02T18:16:16Z
Degrading to P4, since in many cases, calling optimize() on the Expression before doing getIntRange() will in fact return the constant range. Not closing until I've identified that indeed implementing VarExp::getIntRange() would NOT have any effect.
Comment #5 by bearophile_hugs — 2013-07-10T04:43:10Z
(In reply to comment #1) > https://github.com/D-Programming-Language/dmd/pull/1955 This is a good improvement for D. I hope you will fix and reopen your patch. But I think this issue should be tagged as "enhancement". See also Issue 10594 for an "improvement" of this idea.
Comment #6 by bearophile_hugs — 2013-07-20T13:58:03Z
Se also Issue 10685 , Issue 10615 , Issue 10594
Comment #7 by yebblies — 2013-11-21T08:11:57Z
This seems to work just fine: void main() { immutable int i = 1; byte x = i; }
Comment #8 by bearophile_hugs — 2014-03-16T08:49:12Z
The value range of immutable variables is not yet propagated (see Issue 10594 ): void main(in string[] args) { immutable ushort x = args.length % 5; immutable ubyte y = x; } DMD 2.066alpha gives: temp.d(3,25): Error: cannot implicitly convert expression (x) of type immutable(ushort) to immutable(ubyte)
Comment #9 by lio+bugzilla — 2014-06-28T04:40:20Z
(In reply to yebblies from comment #7) > This seems to work just fine: > > void main() > { > immutable int i = 1; > byte x = i; > } This works now because a call to optimize() will remove the reference to 'i' and replace it with the constant expression. If it's not a constant expression (for example, ?: not known at compile time) then it won't propagate the range.
Comment #10 by lio+bugzilla — 2014-06-28T04:57:05Z
Comment #11 by github-bugzilla — 2014-06-28T12:29:28Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/45a26d5d89c22b74bf4cc9f37eaad8c21c53ea80 Issue 10018 - Add VRP support for const/immutable variables https://github.com/D-Programming-Language/dmd/commit/2ffcbeb68e06b31d6be8553acf3462a2d1926b12 Merge pull request #3699 from lionello/bug10018 Issue 10018 - Add VRP support for const/immutable variables