Skip to content

Commit 4fccfa8

Browse files
committed
Fix Issue 23296 - Value Range Propagation not documented
1 parent 62ac7b9 commit 4fccfa8

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

spec/type.dd

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ $(H3 $(LEGACY_LNAME2 Pointer Conversions, pointer-conversions, Pointer Conversio
167167
$(H3 $(LEGACY_LNAME2 Implicit Conversions, implicit-conversions, Implicit Conversions))
168168

169169
$(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.
171171
)
172172

173173
$(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
344344
point types.
345345
)
346346

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+
347375
$(H2 $(LNAME2 bool, $(D bool)))
348376

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

0 commit comments

Comments
 (0)