Skip to content

Commit 4b7e861

Browse files
authored
[clang]use correct this scope to evaluate noexcept expr (#77416)
Fixes: #77411 When substituting deduced type, noexcept expr in method should be instantiated and evaluated. ThisScrope should be switched to method context instead of origin sema context
1 parent 2aec708 commit 4b7e861

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ Bug Fixes in This Version
610610
of template classes. Fixes
611611
(`#68543 <https://github.com/llvm/llvm-project/issues/68543>`_,
612612
`#42496 <https://github.com/llvm/llvm-project/issues/42496>`_,
613-
`#77071 <https://github.com/llvm/llvm-project/issues/77071>`_)
613+
`#77071 <https://github.com/llvm/llvm-project/issues/77071>`_,
614+
`#77411 <https://github.com/llvm/llvm-project/issues/77411>`_)
614615
- Fixed an issue when a shift count larger than ``__INT64_MAX__``, in a right
615616
shift operation, could result in missing warnings about
616617
``shift count >= width of type`` or internal compiler error.

clang/lib/Sema/TreeTransform.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6192,6 +6192,13 @@ bool TreeTransform<Derived>::TransformExceptionSpec(
61926192

61936193
// Instantiate a dynamic noexcept expression, if any.
61946194
if (isComputedNoexcept(ESI.Type)) {
6195+
// Update this scrope because ContextDecl in Sema will be used in
6196+
// TransformExpr.
6197+
auto *Method = dyn_cast_if_present<CXXMethodDecl>(ESI.SourceTemplate);
6198+
Sema::CXXThisScopeRAII ThisScope(
6199+
SemaRef, Method ? Method->getParent() : nullptr,
6200+
Method ? Method->getMethodQualifiers() : Qualifiers{},
6201+
Method != nullptr);
61956202
EnterExpressionEvaluationContext Unevaluated(
61966203
getSema(), Sema::ExpressionEvaluationContext::ConstantEvaluated);
61976204
ExprResult NoexceptExpr = getDerived().TransformExpr(ESI.NoexceptExpr);

clang/test/SemaCXX/cxx1z-noexcept-function-type.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ namespace DependentDefaultCtorExceptionSpec {
6464
struct A { multimap<int> Map; } a;
6565

6666
static_assert(noexcept(A()));
67+
68+
template <class> struct NoexceptWithThis {
69+
int ca;
70+
template <class T> auto foo(T) noexcept(ca) { return true; }
71+
// expected-error@-1 {{noexcept specifier argument is not a constant expression}}
72+
// expected-note@-2 {{in instantiation of exception specification}}
73+
// expected-note@-3 {{implicit use of 'this' pointer is only allowed within the evaluation of a call to a 'constexpr' member function}}
74+
};
75+
struct InstantiateFromAnotherClass {
76+
template <class B, class T = decltype(static_cast<bool (B::*)(int)>(&B::foo))> // expected-note {{in instantiation of function template specialization}}
77+
InstantiateFromAnotherClass(B *) {} // expected-note {{in instantiation of default argument}}
78+
};
79+
NoexceptWithThis<int> f{};
80+
// Don't crash here.
81+
InstantiateFromAnotherClass b{&f}; // expected-note {{while substituting deduced template arguments into function template}}
6782
}
6883

6984
#endif

0 commit comments

Comments
 (0)