This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Improve the error diagnostic for dot destructor calls on pointer objects
ClosedPublic

Authored by arphaman on Oct 20 2016, 3:12 AM.

Details

Summary

This patch improves the mismatched destructor type error by detecting when the destructor call has used a '.' instead of a '->' on a pointer to the destructed type.

For example, when given the code sample below:

struct Bar {
  ~Bar();
};
void bar(Bar *object) {
  object.~Bar();
}

Clang will now produce the following diagnostic: error: member reference type 'Bar *' is a pointer; did you mean to use '->'?.

Diff Detail

Repository
rL LLVM

Event Timeline

arphaman updated this revision to Diff 75275.Oct 20 2016, 3:12 AM
arphaman retitled this revision from to [Sema] Improve the error diagnostic for dot destructor calls on pointer objects.
arphaman updated this object.
arphaman added reviewers: dblaikie, majnemer.
arphaman set the repository for this revision to rL LLVM.
arphaman added a subscriber: cfe-commits.
aaron.ballman accepted this revision.Oct 20 2016, 6:47 AM
aaron.ballman added a reviewer: aaron.ballman.
aaron.ballman added a subscriber: aaron.ballman.

LGTM

lib/Sema/SemaExprCXX.cpp
6287 ↗(On Diff #75275)

You can elide the curly braces.

This revision is now accepted and ready to land.Oct 20 2016, 6:47 AM
arphaman updated this revision to Diff 75403.Oct 21 2016, 3:16 AM
arphaman edited edge metadata.

The updated patch improves error handling and adds a test for the fixit.

If we issue a fixit we should recover as-if the code was written with the fixit in. Does this code do that? (can we test it? I know we test some fixits - not sure it's necessary/worthwhile to test them all, but maybe we have a good idiom for testing that the recovery is correct)

This code does perform recovery, but the constructed AST for the destructor calls is different from the AST that would have been constructed if the code was correct: we still end up building the pseudo destructor expression. I'm not sure how important is that though, so please let me know if I should try and make the ASTs the same.

arphaman updated this revision to Diff 75405.Oct 21 2016, 3:35 AM

Fix a typo in the fixit test.

rsmith added a subscriber: rsmith.Oct 24 2016, 4:35 PM

This code does perform recovery, but the constructed AST for the destructor calls is different from the AST that would have been constructed if the code was correct: we still end up building the pseudo destructor expression. I'm not sure how important is that though, so please let me know if I should try and make the ASTs the same.

The intent is that the following diagnostics produced by the compile-with-fixit should be the same as the diagnostics that would be produced by the fixed code (specifically: we should not issue any "follow-on" diagnostics if the fix was correct, and if all errors have fixits then the code with fixits applied should compile cleanly). On that basis, it seems like it should be OK to produce a pseudo-destructor expression here, but you should check that the destructor of the base type is callable if it's a class type, and that it's a type for which it's valid to use a pseudo-dtor otherwise (see line 6258).

arphaman updated this revision to Diff 75712.Oct 25 2016, 8:43 AM

The updated patch addresses Richard's comment by making sure the fixit isn't emitted when the destructor call is invalid.

Are there any other comments for this patch? I would like to commit it in the next couple of days, as it was accepted and I believe I addressed Richard's concerns.

Thanks

This revision was automatically updated to reflect the committed changes.