-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[clang] Fix const eval of constexpr-unknown relational comparisons. #150088
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
[clang] Fix const eval of constexpr-unknown relational comparisons. #150088
Conversation
Like in other places, ignore the reference type of the base. (It might make sense to refactor this at some point.) Fixes llvm#150015.
@llvm/pr-subscribers-clang Author: Eli Friedman (efriedma-quic) ChangesLike in other places, ignore the reference type of the base. (It might make sense to refactor this at some point.) Fixes #150015. Full diff: https://github.com/llvm/llvm-project/pull/150088.diff 2 Files Affected:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 8797eaddd0e18..106ad30adf1e5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14631,8 +14631,9 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
// - Otherwise pointer comparisons are unspecified.
if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
bool WasArrayIndex;
- unsigned Mismatch = FindDesignatorMismatch(
- getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex);
+ unsigned Mismatch =
+ FindDesignatorMismatch(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
@@ -14676,7 +14677,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..3fb7bf28b1e8b 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -383,3 +383,16 @@ 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;
+ constexpr int bases = (void*)(X*)&z <= (Y*)&z; // nointerpreter-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}}
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
clang/lib/AST/ExprConstant.cpp
Outdated
unsigned Mismatch = FindDesignatorMismatch( | ||
getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); | ||
unsigned Mismatch = | ||
FindDesignatorMismatch(getType(LHSValue.Base).getNonReferenceType(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to create a local and set it to the result of getType(LHSValue.Base).getNonReferenceType()
, comment it and then use that in the two locations you just modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with that is that it presumes LHSValue.Base is non-null. Which I don't think we actually guarantee at the point we'd want to declare the variable, so the the best I can do is something like auto GetBaseType = [&]{return getType(LHSValue.Base).getNonReferenceType(); };
.
…lvm#150088) Like in other places, ignore the reference type of the base. (It might make sense to refactor this at some point.) Fixes llvm#150015.
…lvm#150088) Like in other places, ignore the reference type of the base. (It might make sense to refactor this at some point.) Fixes llvm#150015. (cherry picked from commit bba8467)
Like in other places, ignore the reference type of the base. (It might make sense to refactor this at some point.)
Fixes #150015.