@@ -167,7 +167,7 @@ $(H3 $(LEGACY_LNAME2 Pointer Conversions, pointer-conversions, Pointer Conversio
167
167
$(H3 $(LEGACY_LNAME2 Implicit Conversions, implicit-conversions, Implicit Conversions))
168
168
169
169
$(P Implicit conversions are used to automatically convert
170
- types as required.
170
+ types as required. The rules for integers are detailed in the next sections.
171
171
)
172
172
173
173
$(P An enum can be implicitly converted to its base
@@ -344,6 +344,34 @@ ulong u4 = long(-1); // ok, -1 can be represented in a ulong
344
344
point types.
345
345
)
346
346
347
+ $(H3 $(LNAME2 vrp, Value Range Propagation))
348
+
349
+ $(P Besides type-based implicit conversions, D allows certain integer
350
+ expressions to implicitly convert to a narrower type after
351
+ integer promotion. This works by analysing the minimum and
352
+ maximum possible range of values for each expression.
353
+ If that range of values would fit inside a narrower type, implicit
354
+ conversion is allowed. If one of the values is known at compile-time,
355
+ that can further reduce the range of values.)
356
+
357
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
358
+ ---
359
+ extern char c;
360
+ short s = c + 100; // promoted to int, but narrowed to `c.min + 100` ... `c.max + 100`
361
+
362
+ extern int i;
363
+ ubyte j = i & 0x3F;
364
+ //ubyte k = i & 0x14A; // error, 0x14A > ubyte.max
365
+ ushort k = i & 0x14A; // OK
366
+
367
+ extern ubyte b;
368
+ //ubyte p = b + b; // error, ubyte.max + ubyte.max > ubyte.max
369
+ short p = b + b; // OK
370
+ ---
371
+ )
372
+ $(P For more information, see $(LINK2 https://digitalmars.com/articles/b62.html, here).)
373
+
374
+
347
375
$(H2 $(LNAME2 bool, $(D bool)))
348
376
349
377
$(P The bool type is a byte-size type that can only hold the value `true` or
0 commit comments