Skip to content

Commit 47ecf15

Browse files
authored
Merge pull request #3834 from ntrel/implicit-conv
[spec/type] Document integer implicit conversions Signed-off-by: Nicholas Wilson <thewilsonator@users.noreply.github.com> Merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 9e5bca2 + 127fc7d commit 47ecf15

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

spec/type.dd

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,23 +458,51 @@ void test()
458458
$(DDSUBLINK spec/expression, assignment_operator_expressions, operator assignment)
459459
is equivalent to `e = cast(E)(e + 4)`.)
460460

461-
$(H3 $(LNAME2 disallowed-conversions, Preserving Bit Patterns))
461+
$(H3 $(LEGACY_LNAME2 disallowed-conversions, integer-conversions, Integer Type Conversions))
462+
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+
)
462477

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+
$(H3 $(LNAME2 floating-point-conversions, Floating Point Type Conversions))
492+
493+
* Integral types implicitly convert to floating point types.
494+
* Floating point types cannot be implicitly converted to integral types.
495+
496+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
497+
---
498+
void f(int i, float f)
499+
{
500+
f = i; // OK
501+
i = f; // error
502+
}
503+
---
504+
)
505+
478506
* Complex or imaginary floating point types cannot be implicitly converted
479507
to non-complex floating point types.
480508
* Non-complex floating point types cannot be implicitly converted to imaginary floating

0 commit comments

Comments
 (0)