-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 15702: prohibit implicit conversion to void[] in @safe code if …
…array has indirections.
- Loading branch information
H. S. Teoh
committed
Feb 21, 2016
1 parent
eb8c2c7
commit ced053e
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
TEST_OUTPUT: | ||
--- | ||
fail_compilation/fail15702.d(39): Error: cast from int*[] to void[] not allowed in safe code | ||
fail_compilation/fail15702.d(40): Error: cast from Object[] to void[] not allowed in safe code | ||
fail_compilation/fail15702.d(41): Error: cannot implicitly convert expression (ptrs) of type int*[] with indirections to void[] in @safe code | ||
fail_compilation/fail15702.d(42): Error: cannot implicitly convert expression (objs) of type Object[] with indirections to void[] in @safe code | ||
fail_compilation/fail15702.d(43): Error: cannot implicitly convert expression ([new Object]) of type Object[] with indirections to void[] in @safe code | ||
fail_compilation/fail15702.d(44): Error: cast from Object[] to int[] not allowed in safe code | ||
--- | ||
*/ | ||
void notTrustworthy(void[] buf) @trusted | ||
{ | ||
auto bytes = cast(ubyte[]) buf; | ||
bytes[0] = 123; | ||
} | ||
void trustworthy(const(void)[] buf) @trusted { } | ||
|
||
int[] ints; | ||
int*[] ptrs; | ||
Object[] objs; | ||
|
||
void safeFun() @safe | ||
{ | ||
// should be allowed: | ||
notTrustworthy(ints); | ||
trustworthy(ints); | ||
trustworthy(ptrs); | ||
trustworthy(objs); | ||
const(void)[] arrconst = ptrs; | ||
const(void[]) constarr = ptrs; | ||
auto a1 = cast(const(int)[]) objs; | ||
auto a2 = cast(int*[]) ptrs; | ||
|
||
// typeof should not emit errors | ||
alias X = typeof(notTrustworthy(ptrs)); | ||
|
||
// should be prohibited: | ||
notTrustworthy(cast(void[]) ptrs); | ||
notTrustworthy(cast(void[]) objs); | ||
notTrustworthy(ptrs); | ||
notTrustworthy(objs); | ||
void[] a3 = [ new Object() ]; | ||
auto a4 = cast(int[]) objs; | ||
} | ||
|
||
void unsafeFun() @system | ||
{ | ||
// should be allowed: | ||
notTrustworthy(ints); | ||
trustworthy(ints); | ||
trustworthy(ptrs); | ||
trustworthy(objs); | ||
const(void)[] arrconst = ptrs; | ||
const(void[]) constarr = ptrs; | ||
|
||
// should also be allowed: | ||
notTrustworthy(cast(void[]) ptrs); | ||
notTrustworthy(cast(void[]) objs); | ||
notTrustworthy(ptrs); | ||
notTrustworthy(objs); | ||
void[] arr = [ new Object() ]; | ||
} |