Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/src/dmd/dcast.d
Original file line number Diff line number Diff line change
Expand Up @@ -1603,14 +1603,19 @@ MATCH implicitConvTo(Type from, Type to)
// Disallow implicit conversion from complex to non-complex
if (from.flags & TFlags.complex && !(tob.flags & TFlags.complex))
return MATCH.nomatch;

// Disallow implicit conversion of real or imaginary to complex
if (from.flags & (TFlags.real_ | TFlags.imaginary) && tob.flags & TFlags.complex)
return MATCH.nomatch;

// Disallow implicit conversion to-from real and imaginary
if ((from.flags & (TFlags.real_ | TFlags.imaginary)) != (tob.flags & (TFlags.real_ | TFlags.imaginary)))
return MATCH.nomatch;
// Allow complex float to complex double promotion
if ((from.flags & TFlags.complex) && (tob.flags & TFlags.complex))
{
if (from.size(Loc.initial) < tob.size(Loc.initial))
return MATCH.convert;
}
}
return MATCH.convert;

Expand Down
25 changes: 25 additions & 0 deletions compiler/test/compilable/importc_complex_promotion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Ensure complex float can be implicitly promoted to complex double
// Fixes issue #22259
#include <complex.h>

void testComplexPromotion()
{
_Complex float yf = 1.0if;
_Complex double x = yf; // promotion: _Complex float -> _Complex double
}

void testComplexPreservation()
{
// These should still work (no change)
_Complex float f1 = 1.0f + 1.0if;
_Complex float f2 = 1.0if + 2.0if;
_Complex double d1 = 1.0 + 2.0i;
_Complex double d2 = 2.0 + 3.0i;
}

int main()
{
testComplexPromotion();
testComplexPreservation();
return 0;
}
Loading