This is an archive of the discontinued LLVM Phabricator instance.

[libcxx][test] Silence -Wself-assign diagnostics
ClosedPublic

Authored by lebedev.ri on Mar 31 2018, 12:09 PM.

Details

Summary

D44883 extends -Wself-assign to also work on C++ classes.
These new warnings pop up in the test suite, so they have to be silenced.

Please refer to the D45082 for disscussion on whether this is the right way to solve this.

Testing: ninja check-libcxx check-libcxxabi in stage-2 build.

Diff Detail

Repository
rCXX libc++

Event Timeline

EricWF added inline comments.Mar 31 2018, 2:09 PM
test/std/language.support/support.types/byteops/and.assign.pass.cpp
30

Should Clang really be warning when the expression is in an unevaluated context?

lebedev.ri added inline comments.Mar 31 2018, 2:15 PM
test/std/language.support/support.types/byteops/and.assign.pass.cpp
30

At least that is what it currently already does:
https://godbolt.org/g/RpcgBi

Quuxplusone added inline comments.
test/std/language.support/support.types/byteops/and.assign.pass.cpp
30

Clang already warns on noexcept(b = b) that "expression with side effects has no effect in an unevaluated context [-Wunevaluated-expression]", so adding one more warning should be acceptable, I think.
Both warnings are (IMO correctly) disabled if the expression b = b is dependently typed, e.g. in a template, where expressions like noexcept(b = b) are practically unavoidable.

Personally I think the warnings are acceptable, but these proposed fixes (inserting C-style casts to T&) are not good. The cast hides the intent of the code (especially in the self-assignment tests for function and any). We shouldn't be encouraging that particular workaround in user code. And besides, a good static analyzer (or even Clang itself) should be able to see that "casting a to the type of a" doesn't change the object referred to, and so the self-assignment is still happening, regardless.

How do the std::byte tests avoid the existing warning for -Wunevaluated-expression? Could we use the same mechanism to avoid the new warning for -Wself-assign?

Could we reuse that mechanism in the tests for byte's -= and /= and %= and ^= operators, which may one day start complaining as loudly as &= and |= when you have the same thing on the left and right hand sides?

Could we reuse that mechanism in the tests for function and any?

lebedev.ri added inline comments.Mar 31 2018, 3:03 PM
test/std/language.support/support.types/byteops/and.assign.pass.cpp
30

but these proposed fixes (inserting C-style casts to T&) are not good. The cast hides the intent of the code (especially in the self-assignment tests for function and any). We shouldn't be encouraging that particular workaround in user code.

Please refer to the D45082 for disscussion on that, and on whether or not to add a more fine-grained sub-flag to -Wself-assign.

And besides, a good static analyzer (or even Clang itself) should be able to see that "casting a to the type of a" doesn't change the object referred to, and so the self-assignment is still happening, regardless.

Absolutely, but static analyzer, not compiler diagnostic.

Could we use the same mechanism to avoid the new warning for -Wself-assign?

Not unless you want to completely disable -Wself-assign (as in, not just the new part from D44883) for tests, no.

lebedev.ri updated this revision to Diff 140788.Apr 3 2018, 7:49 AM

The diagnostic was adjusted not to warn in an unevaluated context.
The remaining diff will have to remain, unless you want to disable -Wself-assign altogether for tests...

If there are remaining problems with this diff, please let me know; otherwise it would be really great to get this accepted!

EricWF accepted this revision.Apr 3 2018, 3:52 PM

LGTM. Perhaps we could add a comment for the first such occurrence of the cast in each file explaining it.

This revision is now accepted and ready to land.Apr 3 2018, 3:52 PM
This revision was automatically updated to reflect the committed changes.