[llvm-branch-commits] [clang] release/21.x: [clang] Fix const eval of constexpr-unknown relational comparisons. (#150088) (PR #150981)
Tobias Hieta via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jul 29 00:57:52 PDT 2025
https://github.com/tru updated https://github.com/llvm/llvm-project/pull/150981
>From dc90bf0329259c8a34b5c4c6de53c8e2a7ca055a Mon Sep 17 00:00:00 2001
From: Eli Friedman <efriedma at quicinc.com>
Date: Thu, 24 Jul 2025 13:36:54 -0700
Subject: [PATCH] [clang] Fix const eval of constexpr-unknown relational
comparisons. (#150088)
Like in other places, ignore the reference type of the base. (It might
make sense to refactor this at some point.)
Fixes #150015.
(cherry picked from commit bba846773c7dfce0f95b8846672d8dd5fa8912be)
---
clang/lib/AST/ExprConstant.cpp | 6 ++++--
clang/test/SemaCXX/constant-expression-p2280r4.cpp | 14 ++++++++++++++
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d7b1173283c57..0733f8e8a33b0 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14566,7 +14566,9 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
bool WasArrayIndex;
unsigned Mismatch = FindDesignatorMismatch(
- getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex);
+ LHSValue.Base.isNull() ? QualType()
+ : getType(LHSValue.Base).getNonReferenceType(),
+ LHSDesignator, RHSDesignator, WasArrayIndex);
// At the point where the designators diverge, the comparison has a
// specified value if:
// - we are comparing array indices
@@ -14610,7 +14612,7 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
// compare pointers within the object in question; otherwise, the result
// depends on where the object is located in memory.
if (!LHSValue.Base.isNull() && IsRelational) {
- QualType BaseTy = getType(LHSValue.Base);
+ QualType BaseTy = getType(LHSValue.Base).getNonReferenceType();
if (BaseTy->isIncompleteType())
return Error(E);
CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy);
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index 16f5f823d26c1..312a77830420b 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -383,3 +383,17 @@ namespace enable_if_2 {
}
}
}
+
+namespace GH150015 {
+ extern int (& c)[8]; // interpreter-note {{declared here}}
+ constexpr int x = c <= c+8; // interpreter-error {{constexpr variable 'x' must be initialized by a constant expression}} \
+ // interpreter-note {{initializer of 'c' is unknown}}
+
+ struct X {};
+ struct Y {};
+ struct Z : X, Y {};
+ extern Z &z; // interpreter-note{{declared here}}
+ constexpr int bases = (void*)(X*)&z <= (Y*)&z; // expected-error {{constexpr variable 'bases' must be initialized by a constant expression}} \
+ // nointerpreter-note {{comparison of addresses of subobjects of different base classes has unspecified value}} \
+ // interpreter-note {{initializer of 'z' is unknown}}
+}
More information about the llvm-branch-commits
mailing list