Skip to content

Commit a89f095

Browse files
kubo39thewilsonator
authored andcommitted
Fix #4993: allow to cast between for fat values
1 parent a1363ff commit a89f095

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

gen/llvmhelpers.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,15 @@ DValue *DtoCastStruct(Loc loc, DValue *val, Type *to) {
673673
return new DLValue(to, lval);
674674
}
675675

676+
// A cast between fat values is possible only when the sizes match.
677+
// https://github.com/ldc-developers/ldc/issues/4993
678+
if (totype->ty == TY::Tsarray || totype->ty == TY::Tvector) {
679+
if (size(totype) == size(val->type->toBasetype())) {
680+
llvm::Value *lval = DtoLVal(val);
681+
return new DLValue(to, lval);
682+
}
683+
}
684+
676685
error(loc, "Internal Compiler Error: Invalid struct cast from `%s` to `%s`",
677686
val->type->toChars(), to->toChars());
678687
fatal();

tests/compilable/gh4993.d

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %ldc -c %s
2+
3+
struct Foo {}
4+
5+
struct Bar
6+
{
7+
int[4] v;
8+
}
9+
10+
void main() {
11+
Foo f;
12+
auto x = cast(ubyte[Foo.sizeof])f;
13+
14+
Bar b;
15+
auto y = cast(__vector(int[4]))b;
16+
}

0 commit comments

Comments
 (0)