diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -301,6 +301,8 @@ expressions/statements with invalid source locations in non-assert builds. Assert builds may still see assertions triggered from this. (`#62105 `_) +- Fix crash after suggesting typo correction to constexpr if condition. + (`#61885 `_) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -12849,11 +12849,17 @@ friend class Sema; ConditionResult(Sema &S, Decl *ConditionVar, FullExprArg Condition, bool IsConstexpr) - : ConditionVar(ConditionVar), Condition(Condition), Invalid(false), - HasKnownValue(IsConstexpr && Condition.get() && - !Condition.get()->isValueDependent()), - KnownValue(HasKnownValue && - !!Condition.get()->EvaluateKnownConstInt(S.Context)) {} + : ConditionVar(ConditionVar), Condition(Condition), Invalid(false) { + if (IsConstexpr && Condition.get()) { + if (std::optional Val = + Condition.get()->getIntegerConstantExpr(S.Context)) { + KnownValue = !!(*Val); + HasKnownValue = true; + } + } else { + HasKnownValue = false; + } + } explicit ConditionResult(bool Invalid) : ConditionVar(nullptr), Condition(nullptr), Invalid(Invalid), HasKnownValue(false), KnownValue(false) {} diff --git a/clang/test/SemaCXX/invalid-if-constexpr.cpp b/clang/test/SemaCXX/invalid-if-constexpr.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/invalid-if-constexpr.cpp @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 -verify -std=c++20 %s + +namespace GH61885 { +void similar() { // expected-note {{'similar' declared here}} + if constexpr (similer<>) {} // expected-error {{use of undeclared identifier 'similer'; did you mean 'similar'?}} +} +void a() { if constexpr (__adl_swap<>) {}} // expected-error{{use of undeclared identifier '__adl_swap'; did you mean '__sync_swap'?}} \ + // expected-note {{'__sync_swap' declared here}} + +int AA() { return true;} // expected-note {{'AA' declared here}} + +void b() { if constexpr (AAA<>) {}} // expected-error {{use of undeclared identifier 'AAA'; did you mean 'AA'?}} +} +