Skip to content

Commit

Permalink
Fix Bugzilla 24603 - Can copy from non-void array into void[] in safe…
Browse files Browse the repository at this point in the history
… code

Deprecate in safe code.
  • Loading branch information
ntrel authored and dlang-bot committed Jun 16, 2024
1 parent cba86d3 commit 8d7839a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -11429,12 +11429,19 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
}
if (t1n.toBasetype.ty == Tvoid && t2n.toBasetype.ty == Tvoid)
{
if (sc.setUnsafe(false, exp.loc, "cannot copy `void[]` to `void[]` in `@safe` code"))
if (sc.setUnsafe(false, exp.loc, "cannot copy `%s` to `%s` in `@safe` code", t2, t1))
return setError();
}
}
else
{
if (exp.e1.op == EXP.slice &&
(t1.ty == Tarray || t1.ty == Tsarray) &&
t1.nextOf().toBasetype().ty == Tvoid &&
sc.setUnsafePreview(FeatureState.default_, false, exp.loc,
"cannot copy `%s` to `%s` in `@safe` code", t2, t1))
return setError();

if (exp.op == EXP.blit)
e2x = e2x.castTo(sc, exp.e1.type);
else
Expand Down
6 changes: 5 additions & 1 deletion compiler/test/fail_compilation/test15704.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
* TEST_OUTPUT:
---
fail_compilation/test15704.d(15): Error: cannot copy `void[]` to `void[]` in `@safe` code
fail_compilation/test15704.d(17): Error: cannot copy `void[]` to `void[]` in `@safe` code
fail_compilation/test15704.d(18): Error: cannot copy `const(void)[]` to `void[]` in `@safe` code
fail_compilation/test15704.d(19): Deprecation: cannot copy `int[]` to `void[]` in `@safe` code
---
*/

Expand All @@ -13,4 +15,6 @@ void main() @safe {
void[] arr2 = [ 123, 345, 567 ];

arr1[] = arr2[]; // overwrites pointers with arbitrary ints
arr1[] = new const(void)[3];
arr1[] = [5];
}

0 comments on commit 8d7839a

Please sign in to comment.