@@ -223,7 +223,7 @@ $(H3 $(LEGACY_LNAME2 Pointer Conversions, pointer-conversions, Pointer Conversio
223
223
$(H3 $(LEGACY_LNAME2 Implicit Conversions, implicit-conversions, Implicit Conversions))
224
224
225
225
$(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.
227
227
)
228
228
229
229
$(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
463
463
point types.
464
464
)
465
465
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
+
466
508
$(H2 $(LNAME2 bool, $(D bool)))
467
509
468
510
$(P The bool type is a byte-size type that can only hold the value `true` or
0 commit comments