Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make subtracting pointers of different types an error #20577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 2 additions & 4 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -12403,12 +12403,10 @@

if (!p1.equivalent(p2))
{
// Deprecation to remain for at least a year, after which this should be
// changed to an error
// See https://github.com/dlang/dmd/pull/7332
deprecation(exp.loc,
"cannot subtract pointers to different types: `%s` and `%s`.",
error(exp.loc, "cannot subtract pointers to different types: `%s` and `%s`.",

Check warning on line 12407 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L12407

Added line #L12407 was not covered by tests
t1.toChars(), t2.toChars());
return setError();

Check warning on line 12409 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L12409

Added line #L12409 was not covered by tests
}

// Need to divide the result by the stride
Expand Down
16 changes: 10 additions & 6 deletions compiler/test/fail_compilation/test11006.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/* REQUIRED_ARGS: -main -de
* TEST_OUTPUT:
/* TEST_OUTPUT:
---
fail_compilation/test11006.d(10): Deprecation: cannot subtract pointers to different types: `void*` and `int*`.
fail_compilation/test11006.d(10): while evaluating: `static assert(2L == 2L)`
fail_compilation/test11006.d(11): Deprecation: cannot subtract pointers to different types: `int*` and `void*`.
fail_compilation/test11006.d(11): while evaluating: `static assert(8L == 8L)`
fail_compilation/test11006.d(11): Error: cannot subtract pointers to different types: `void*` and `int*`.
fail_compilation/test11006.d(11): while evaluating: `static assert(2L == 2L)`
fail_compilation/test11006.d(12): Error: cannot subtract pointers to different types: `int*` and `void*`.
fail_compilation/test11006.d(12): while evaluating: `static assert(8L == 8L)`
fail_compilation/test11006.d(15): Error: cannot subtract pointers to different types: `ushort*` and `ubyte*`.
---
*/
static assert(cast(void*)8 - cast(int*) 0 == 2L);
static assert(cast(int*) 8 - cast(void*)0 == 8L);
void test()
{
auto foo = (ushort*).init - (ubyte*).init;
}
Loading