@@ -350,23 +350,27 @@ $(H3 $(LNAME2 vrp, Value Range Propagation))
350
350
expressions to implicitly convert to a narrower type after
351
351
integer promotion. This works by analysing the minimum and
352
352
maximum possible range of values for each expression.
353
- If that range of values matches or is a subset of a narrower type's value range, implicit
353
+ If that range of values matches or is a subset of a narrower
354
+ target type's value range, implicit
354
355
conversion is allowed. If one of the values is known at compile-time,
355
356
that can further reduce the range of values.)
356
357
357
358
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
358
359
---
359
360
extern char c;
360
- short s = c + 100; // promoted to int, but narrowed to `c.min + 100` ... `c.max + 100`
361
+ // min is c.min + 100 > short.min
362
+ // max is c.max + 100 < short.max
363
+ short s = c + 100; // OK
361
364
362
365
extern int i;
363
- ubyte j = i & 0x3F;
366
+ ubyte j = i & 0x3F; // OK, 0 ... 0x3F
364
367
//ubyte k = i & 0x14A; // error, 0x14A > ubyte.max
365
368
ushort k = i & 0x14A; // OK
366
369
367
370
extern ubyte b;
368
- //ubyte p = b + b; // error, ubyte.max + ubyte.max > ubyte.max
369
- short p = b + b; // OK
371
+ k = i & b; // OK, 0 ... b.max
372
+ //b = b + b; // error, b.max + b.max > b.max
373
+ s = b + b; // OK, 0 ... b.max + b.max
370
374
---
371
375
)
372
376
$(P For more information, see $(LINK2 https://digitalmars.com/articles/b62.html, here).)
0 commit comments