Skip to content

don't use memcmp() for struct equality, do memberwise comparisons per the spec#9331

Merged
WalterBright merged 1 commit intodlang:masterfrom
WalterBright:no-cmpsb
Feb 9, 2019
Merged

don't use memcmp() for struct equality, do memberwise comparisons per the spec#9331
WalterBright merged 1 commit intodlang:masterfrom
WalterBright:no-cmpsb

Conversation

@WalterBright
Copy link
Member

I.e. if the structs fit in 2 or fewer registers use a member-wise compare.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @WalterBright!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub fetch digger
dub run digger -- build "master + dmd#9331"

@WalterBright
Copy link
Member Author

Hmm. Needs more work.

@WalterBright WalterBright added the Review:WIP Work In Progress - not ready for review or pulling label Feb 7, 2019
@WalterBright WalterBright force-pushed the no-cmpsb branch 2 times, most recently from 6d73c26 to 17dd56f Compare February 7, 2019 22:53
@WalterBright
Copy link
Member Author

Who knows why these tests are failing. Can't tell anything from the logs. For one thing, the logs should STOP when there's a failure, not continue going on for thousands of lines. The thing that failed should be the last line. The tests in test/runnable are all simple to diagnose with simple logs.

@Geod24
Copy link
Member

Geod24 commented Feb 8, 2019

the logs should STOP when there's a failure, not continue going on for thousands of lines

True, but not trivial to do, especially if we want to run tests concurrently.

Who knows why these tests are failing.

There seems to be multiple reason. One I was able to dig out pretty easily on Windows is:

std\variant.d(270): Warning: statement is not reachable

Handy link to L270

@WalterBright
Copy link
Member Author

Can't repro the variant.d issue, either on Linux or Windows. Sigh. Looking at the code in variant.d, I don't see any reason for the warning, either. Can anyone else repro a problem? It's only a one line change to the compiler.

@WalterBright
Copy link
Member Author

@schveiguy @wilzbach as recent committers to std.variant, can you perhaps help out with what is going on here?

The one line change to the compiler replaces a bit compare of struct equality with a member-wise compare for some structs.

@WalterBright
Copy link
Member Author

Spec reference: dlang/dlang.org#2572

@WalterBright
Copy link
Member Author

Revised the PR so it is in conformance with the spec.

@WalterBright WalterBright changed the title don't use memcmp() for small struct equality don't use memcmp() for struct equality, do memberwise comparisons per the spec Feb 8, 2019
@WalterBright
Copy link
Member Author

I think this will need a -preview=fieldcmp switch, because it is apparently breaking existing code. But that is blocked by #9206

@WalterBright
Copy link
Member Author

Rather than wait for #9206, I just went ahead and added a -preview=fieldwise switch directly and unblocked this.

@aG0aep6G
Copy link
Contributor

aG0aep6G commented Feb 8, 2019

Related issue: 16216.

@WalterBright
Copy link
Member Author

@aG0aep6G Thank you, I wasn't aware of that bug report. This should fix it (but not for is comparisons).

@WalterBright
Copy link
Member Author

@wilzbach @thewilsonator ready to pull now.

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Though I want to point out that we shouldn't abuse the new -preview too much as we still aren't planning to do a D3 (or D 2020 (?)) in the foreseeable future (?) and thus breaking changes must still go through a deprecation phase in which the users can still compile their code, but are warned about the change.
Just by adding a -preview switch a user won't be warned that this code is affected by this breaking change.

The spec requires that the comparison be done in a memberwise manner.
This brings the implementation in line with this.

$(LINK2 https://dlang.org/spec/expression.html#equality_expressions, Equality Expressions)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This links stands a bit out in the flow of the reader:

image

Maybe directly link it as e,g, "Equality Expressions can break ..."?

@wilzbach
Copy link
Contributor

wilzbach commented Feb 9, 2019

True, but not trivial to do, especially if we want to run tests concurrently.

Isn't this more a "feature" that someone actively worked into the testsuite?
I think the idea was that it takes quite a long time to get feedback from all machines, so you at least want to get a full list of all failed tests.
Though I absolutely agree that all failed tests should be printed as a summary at the end.
If we agree to switch over to test/run.d on the auto-tester this could be added easily...

@schveiguy @wilzbach as recent committers to std.variant, can you perhaps help out with what is going on here?
The one line change to the compiler replaces a bit compare of struct equality with a member-wise compare for some structs.

Git blame points to five years ago:
https://github.com/dlang/phobos/blame/e10d6a7e973ed85ccceee21088fa716433429583/std/variant.d#L270

@WalterBright WalterBright merged commit 3fa1978 into dlang:master Feb 9, 2019
@WalterBright WalterBright deleted the no-cmpsb branch February 9, 2019 02:06
@WalterBright
Copy link
Member Author

The problem is I don't know how to detect that the user code would break with this. The user code that relies on the old behavior is wrong in any case. Also, the -revert switch will save those who suspect the new behavior is causing them problems.

@jacob-carlborg
Copy link
Contributor

Just by adding a -preview switch a user won't be warned that this code is affected by this breaking change.

Before this will be turned on by default it needs to go through a deprecation period.

@jacob-carlborg
Copy link
Contributor

jacob-carlborg commented Feb 9, 2019

If we agree to switch over to test/run.d on the auto-tester this could be added easily...

My unit test runner already does this 😃: #9333.

pbackus added a commit to pbackus/dmd that referenced this pull request Dec 8, 2024
Introduced in PR dlang#9331, this was changed from a normal bug fix to a
-preview switch because enabling it triggered a suprious "statement is
not reachable" warning in Phobos. That warning has since been removed
(in PR dlang#15568).
pbackus added a commit to pbackus/dmd that referenced this pull request Dec 8, 2024
Introduced in PR dlang#9331, this was changed from a normal bug fix to a
-preview switch because enabling it triggered a suprious "statement is
not reachable" warning in Phobos. That warning has since been removed
(in PR dlang#15568).
pbackus added a commit to pbackus/dmd that referenced this pull request Dec 8, 2024
Introduced in PR dlang#9331, this was changed from a normal bug fix to a
-preview switch because enabling it triggered a suprious "statement is
not reachable" warning in Phobos. That warning has since been removed
(in PR dlang#15568).
pbackus added a commit to pbackus/dmd that referenced this pull request Dec 8, 2024
Introduced in PR dlang#9331, this was changed from a normal bug fix to a
-preview switch because enabling it triggered a suprious "statement is
not reachable" warning in Phobos. That warning has since been removed
(in PR dlang#15568).
dlang-bot pushed a commit that referenced this pull request Dec 8, 2024
Introduced in PR #9331, this was changed from a normal bug fix to a
-preview switch because enabling it triggered a suprious "statement is
not reachable" warning in Phobos. That warning has since been removed
(in PR #15568).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants