Skip to content

[Clang][Sema] Fix crash when diagnosing near-match for 'constexpr' redeclaration in C++11 #92452

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

Merged
merged 3 commits into from
May 20, 2024
Merged
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,8 @@ Bug Fixes to C++ Support
- Fix a bug with checking constrained non-type template parameters for equivalence. Fixes (#GH77377).
- Fix a bug where the last argument was not considered when considering the most viable function for
explicit object argument member functions. Fixes (#GH92188).
- Fix a C++11 crash when a non-const non-static member function is defined out-of-line with
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a bug # for this one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't know, I just happened upon it when working on #92449... I'll check

Copy link
Member Author

Choose a reason for hiding this comment

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

I looked, but couldn't find any

Copy link
Member Author

Choose a reason for hiding this comment

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

Nevermind, I did :) #61004

the ``constexpr`` specifier. Fixes (#GH61004).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
21 changes: 11 additions & 10 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9217,19 +9217,20 @@ static NamedDecl *DiagnoseInvalidRedeclaration(
<< Idx << FDParam->getType()
<< NewFD->getParamDecl(Idx - 1)->getType();
} else if (FDisConst != NewFDisConst) {
SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match)
<< NewFDisConst << FD->getSourceRange().getEnd()
<< (NewFDisConst
? FixItHint::CreateRemoval(ExtraArgs.D.getFunctionTypeInfo()
.getConstQualifierLoc())
: FixItHint::CreateInsertion(ExtraArgs.D.getFunctionTypeInfo()
.getRParenLoc()
.getLocWithOffset(1),
" const"));
} else
auto DB = SemaRef.Diag(FD->getLocation(),
diag::note_member_def_close_const_match)
<< NewFDisConst << FD->getSourceRange().getEnd();
if (const auto &FTI = ExtraArgs.D.getFunctionTypeInfo(); !NewFDisConst)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (const auto &FTI = ExtraArgs.D.getFunctionTypeInfo(); !NewFDisConst)
if (const auto &FTI = ExtraArgs.D.getFunctionTypeInfo(); NewFDisConst)

This is closer to what we mean, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Er, no. We want to suggest removing const when NewFDisConst is true, or suggest inserting it if false.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Woops, I forgot my other change :D I meant FDisConst since i saw the diagnostic was in terms of the 'old' value? But I could have been mistaken.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would work either way... since this is guarded by if (FDisConst != NewFDisConst), !NewFDisConst implies FDisConst.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I agree it is not really a meaningful change, besides being a bit of a 'say what you mean'. That is, use the variable that is more closely related to what we MEAN. I haven't reviewed this in long enough to know if I was correct here, so if you're sure that the variable you're using is the one that properly states the intent, feel free to ignore hte comment.

DB << FixItHint::CreateInsertion(FTI.getRParenLoc().getLocWithOffset(1),
" const");
else if (FTI.hasMethodTypeQualifiers() &&
FTI.getConstQualifierLoc().isValid())
DB << FixItHint::CreateRemoval(FTI.getConstQualifierLoc());
} else {
SemaRef.Diag(FD->getLocation(),
IsMember ? diag::note_member_def_close_match
: diag::note_local_decl_close_match);
}
}
return nullptr;
}
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,14 @@ namespace {
// FIXME: We should diagnose this prior to C++17.
const int &r = A::n;
}

#if __cplusplus < 201402L
namespace ImplicitConstexprDef {
struct A {
void f(); // expected-note {{member declaration does not match because it is not const qualified}}
};

constexpr void A::f() { } // expected-warning {{'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior}}
// expected-error@-1 {{out-of-line definition of 'f' does not match any declaration in 'ImplicitConstexprDef::A'}}
}
#endif
Loading