Skip to content

Commit 7c862ef

Browse files
authored
Merge pull request #3379 from ntrel/vrp
[spec/type] Add Value Range Propagation section Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 05b1288 + 33fd363 commit 7c862ef

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

spec/type.dd

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ $(H3 $(LEGACY_LNAME2 Pointer Conversions, pointer-conversions, Pointer Conversio
223223
$(H3 $(LEGACY_LNAME2 Implicit Conversions, implicit-conversions, Implicit Conversions))
224224

225225
$(P Implicit conversions are used to automatically convert
226-
types as required.
226+
types as required. The rules for integers are detailed in the next sections.
227227
)
228228

229229
$(P An enum can be implicitly converted to its base
@@ -463,6 +463,48 @@ ulong u4 = long(-1); // ok, -1 can be represented in a ulong
463463
point types.
464464
)
465465

466+
$(H3 $(LNAME2 vrp, Value Range Propagation))
467+
468+
$(P Besides type-based implicit conversions, D allows certain integer
469+
expressions to implicitly convert to a narrower type after
470+
integer promotion. This works by analysing the minimum and
471+
maximum possible range of values for each expression.
472+
If that range of values matches or is a subset of a narrower
473+
target type's value range, implicit
474+
conversion is allowed. If a subexpression is known at compile-time,
475+
that can further narrow the range of values.)
476+
477+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
478+
---
479+
extern char c;
480+
// min is c.min + 100 > short.min
481+
// max is c.max + 100 < short.max
482+
short s = c + 100; // OK
483+
484+
extern int i;
485+
ubyte j = i & 0x3F; // OK, 0 ... 0x3F
486+
//ubyte k = i & 0x14A; // error, 0x14A > ubyte.max
487+
ushort k = i & 0x14A; // OK
488+
489+
extern ubyte b;
490+
k = i & b; // OK, 0 ... b.max
491+
//b = b + b; // error, b.max + b.max > b.max
492+
s = b + b; // OK, 0 ... b.max + b.max
493+
---
494+
)
495+
$(P Note the implementation does not track the range of possible values for variables,
496+
only expressions:)
497+
---
498+
extern int i;
499+
short s = i & 0xff; // OK
500+
// s is now assumed to be s.min ... s.max, not 0 ... 0xff
501+
//byte b = s; // error
502+
byte b = s & 0xff; // OK
503+
---
504+
* For more information, see $(LINK2 https://digitalmars.com/articles/b62.html, the dmc article).
505+
* See also: $(LINK https://en.wikipedia.org/wiki/Value_range_analysis).
506+
507+
466508
$(H2 $(LNAME2 bool, $(D bool)))
467509

468510
$(P The bool type is a byte-size type that can only hold the value `true` or

0 commit comments

Comments
 (0)