Bug 8216 – CTFE should allow 'pointer is inside range' comparisons
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-09T23:13:00Z
Last change time
2015-06-09T05:12:00Z
Keywords
CTFE, pull
Assigned to
nobody
Creator
clugdbug
Comments
Comment #0 by clugdbug — 2012-06-09T23:13:06Z
Currently all < and > comparisons of pointers do not compile in CTFE if the pointers are non-null and point to unrelated memory blocks, because the relative ordering of the memory blocks is undefined.
But this gives not way to check if a pointer lies within a range, even though that operation doesn't actually depend on the ordering of the memory blocks.
My patch implements *all* cases where the memory block ordering is checked in *both* directions within a single && or || expression.
That is, expressions of the form:
( p1 > q1 && p2 <= q2 )
( p1 < q1 || p2 > q2 )
where p1, p2, q1, q2 are arbitrary expressions of type pointer.
p1 and p2 must point to memory block P, q1 and q2 must point to memory block Q.
Each operators can be >, <, >=, or <=, and ! can be added anywhere, provided that eventual effect of the comparison is a check that P is inside Q, or P is outside Q.
I believe this satisfies the primary requests made on the newsgroup:
* It provides a way to check if a pointer lies inside a particular memory block;
* It is @safe and pure - it never involves undefined or compiler-specific behaviour
* The situations where it is valid are broad and yet clearly defined -- _anything_ within a && or || will work.
* Most code which wasn't written with CTFE in mind, will work in CTFE without modification.