Skip to content

Commit ca56626

Browse files
committed
[spec/type] Document integer implicit conversions
1 parent c022f8d commit ca56626

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

spec/type.dd

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,21 +460,47 @@ void test()
460460

461461
$(H3 $(LNAME2 disallowed-conversions, Preserving Bit Patterns))
462462

463+
$(P An integer of type `I` implicitly converts to another integer type `J` when
464+
`J.sizeof >= I.sizeof`.)
465+
466+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
467+
---
468+
void f(byte b, ubyte ub, short s)
469+
{
470+
b = ub; // OK, bit pattern same
471+
ub = b; // OK, bit pattern same
472+
s = b; // OK, widening conversion
473+
b = s; // error, implicit narrowing
474+
}
475+
---
476+
)
477+
463478
$(P Integer values cannot be implicitly converted to another
464-
type that cannot represent the integer bit pattern after integral
465-
promotion. For example:)
479+
type that cannot represent the integer bit pattern after
480+
$(RELATIVE_LINK2 integer-promotions, integral promotion). For example:)
466481

467482
$(SPEC_RUNNABLE_EXAMPLE_FAIL
468483
---
469-
ubyte u1 = -1; // error, -1 cannot be represented in a ubyte
470-
ushort u2 = -1; // error, -1 cannot be represented in a ushort
471-
uint u3 = int(-1); // ok, -1 can be represented in an int, which can be converted to a uint
472-
ulong u4 = long(-1); // ok, -1 can be represented in a long, which can be converted to a ulong
484+
ubyte u1 = -1; // error, -1 cannot be represented in a ubyte
485+
ushort u2 = -1; // error, -1 cannot be represented in a ushort
486+
uint u3 = -1; // ok, -1 can be represented in an int, which can be converted to a uint
487+
ulong u4 = -1; // ok, -1 can be represented in a long, which can be converted to a ulong
473488
---
474489
)
475490

476-
* Floating point types cannot be implicitly converted to
477-
integral types.
491+
* Integral types implicitly convert to floating point types.
492+
* Floating point types cannot be implicitly converted to integral types.
493+
494+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
495+
---
496+
void f(int i, float f)
497+
{
498+
f = i; // OK
499+
i = f; // error
500+
}
501+
---
502+
)
503+
478504
* Complex or imaginary floating point types cannot be implicitly converted
479505
to non-complex floating point types.
480506
* Non-complex floating point types cannot be implicitly converted to imaginary floating

0 commit comments

Comments
 (0)