From 1b34fea4788136b54ec77c6ed9678754d109fc79 Mon Sep 17 00:00:00 2001 From: Dennis Date: Thu, 10 Apr 2025 23:05:48 +0200 Subject: [PATCH] Fix #21179 - Failure to convert const(T) to T after type is used in cast() (#21201) --- compiler/src/dmd/typesem.d | 16 +++++++++++++--- compiler/test/compilable/test21179.d | 11 +++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 compiler/test/compilable/test21179.d diff --git a/compiler/src/dmd/typesem.d b/compiler/src/dmd/typesem.d index 3bc0489bff..d4c7a5865d 100644 --- a/compiler/src/dmd/typesem.d +++ b/compiler/src/dmd/typesem.d @@ -3266,9 +3266,19 @@ Type merge(Type type) case Tsarray: // prevents generating the mangle if the array dim is not yet known - if (!type.isTypeSArray().dim.isIntegerExp()) - return type; - goto default; + if (auto ie = type.isTypeSArray().dim.isIntegerExp()) + { + // After TypeSemantic, the length is always converted to size_t, but the parser + // usually generates regular integer types (e.g. in cast(const ubyte[2])) which + // it may try to merge, which then leads to failing implicit conversions as 2LU != 2 + // according to Expression.equals. Only merge array types with size_t lengths for now. + // https://github.com/dlang/dmd/issues/21179 + if (ie.type != Type.tsize_t) + return type; + + goto default; + } + return type; case Tenum: break; diff --git a/compiler/test/compilable/test21179.d b/compiler/test/compilable/test21179.d new file mode 100644 index 0000000000..78bdffda55 --- /dev/null +++ b/compiler/test/compilable/test21179.d @@ -0,0 +1,11 @@ +// https://github.com/dlang/dmd/issues/21179 + +void bigEndianToNative(ubyte[2] a) {} + +void main() +{ + ubyte[] arr; + const ubyte[2] bytes; + bigEndianToNative(bytes); + auto b = cast(const ubyte[2][]) arr; +}