Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

release/21.x: [clang] Fix pointer comparisons between pointers to constexpr-unknown (#147663)#148907

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tru merged 1 commit intollvm:release/21.xfromllvmbot:issue147663
Jul 17, 2025

Conversation

llvmbot
Copy link
Member

Backport20c8e3c

Requested by:@efriedma-quic

@llvmbot
Copy link
MemberAuthor

@zygoloid What do you think about merging this PR to the release branch?

@llvmbotllvmbot requested a review fromzygoloidJuly 15, 2025 17:44
@llvmbotllvmbot added clangClang issues not falling into any other category clang:frontendLanguage frontend issues, e.g. anything involving "Sema" labelsJul 15, 2025
@llvmbot
Copy link
MemberAuthor

@llvm/pr-subscribers-clang

Author: None (llvmbot)

Changes

Backport20c8e3c

Requested by: @efriedma-quic


Full diff:https://github.com/llvm/llvm-project/pull/148907.diff

4 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/AST/ExprConstant.cpp (+4-6)
  • (modified) clang/test/SemaCXX/constant-expression-cxx14.cpp (+15)
  • (modified) clang/test/SemaCXX/constant-expression-p2280r4.cpp (+24-8)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rstindex bd7a4b20242fd..2add72a1654b1 100644--- a/clang/docs/ReleaseNotes.rst+++ b/clang/docs/ReleaseNotes.rst@@ -967,6 +967,7 @@ Bug Fixes to C++ Support - Fix a crash with NTTP when instantiating local class. - Fixed a crash involving list-initialization of an empty class with a   non-empty initializer list. (#GH147949)+- Fixed constant evaluation of equality comparisons of constexpr-unknown references. (#GH147663)  Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cppindex 419dd5dbdc695..1b33b6706e204 100644--- a/clang/lib/AST/ExprConstant.cpp+++ b/clang/lib/AST/ExprConstant.cpp@@ -14478,12 +14478,6 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,     if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)       return false;-    // If we have Unknown pointers we should fail if they are not global values.-    if (!(IsGlobalLValue(LHSValue.getLValueBase()) &&-          IsGlobalLValue(RHSValue.getLValueBase())) &&-        (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))-      return false;-     // Reject differing bases from the normal codepath; we special-case     // comparisons to null.     if (!HasSameBase(LHSValue, RHSValue)) {@@ -14545,6 +14539,10 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,           (LHSValue.Base && isZeroSized(RHSValue)))         return DiagComparison(             diag::note_constexpr_pointer_comparison_zero_sized);+      if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)+        return DiagComparison(+            diag::note_constexpr_pointer_comparison_unspecified);+      // FIXME: Verify both variables are live.       return Success(CmpResult::Unequal, E);     }diff --git a/clang/test/SemaCXX/constant-expression-cxx14.cpp b/clang/test/SemaCXX/constant-expression-cxx14.cppindex e16a69df3830d..e93b98c185a82 100644--- a/clang/test/SemaCXX/constant-expression-cxx14.cpp+++ b/clang/test/SemaCXX/constant-expression-cxx14.cpp@@ -1321,3 +1321,18 @@ constexpr bool check = different_in_loop();   // expected-error@-1 {{}} expected-note@-1 {{in call}}  }++namespace comparison_dead_variable {+  constexpr bool f() {+    int *p1 = 0, *p2 = 0;+    {+        int x = 0; p1 = &x;+    }+    {+        int x = 0; p2 = &x;+    }+    return p1 != p2;+  }+  // FIXME: This should fail.+  static_assert(f(),"");+}diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cppindex dffb386f530f4..03fea91169787 100644--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp@@ -319,7 +319,7 @@ namespace casting { }  namespace pointer_comparisons {-  extern int &extern_n; // interpreter-note 2 {{declared here}}+  extern int &extern_n; // interpreter-note 4 {{declared here}}   extern int &extern_n2;   constexpr int f1(bool b, int& n) {     if (b) {@@ -330,14 +330,30 @@ namespace pointer_comparisons {   // FIXME: interpreter incorrectly rejects; both sides are the same constexpr-unknown value.   static_assert(f1(false, extern_n)); // interpreter-error {{static assertion expression is not an integral constant expression}} \                                       // interpreter-note {{initializer of 'extern_n' is unknown}}-  // FIXME: We should diagnose this: we don't know if the references bind-  // to the same object.-  static_assert(&extern_n != &extern_n2); // interpreter-error {{static assertion expression is not an integral constant expression}} \+  static_assert(&extern_n != &extern_n2); // expected-error {{static assertion expression is not an integral constant expression}} \+                                          // nointerpreter-note {{comparison between pointers to unrelated objects '&extern_n' and '&extern_n2' has unspecified value}} \                                           // interpreter-note {{initializer of 'extern_n' is unknown}}   void f2(const int &n) {-    // FIXME: We should not diagnose this: the two objects provably have-    // different addresses because the lifetime of "n" extends across-    // the initialization.-    constexpr int x = &x == &n; // nointerpreter-error {{must be initialized by a constant expression}}+    constexpr int x = &x == &n; // nointerpreter-error {{must be initialized by a constant expression}} \+                                // nointerpreter-note {{comparison between pointers to unrelated objects '&x' and '&n' has unspecified value}}+    // Distinct variables are not equal, even if they're local variables.+    constexpr int y = &x == &y;+    static_assert(!y);   }+  constexpr int f3() {+    int x;+    return &x == &extern_n; // nointerpreter-note {{comparison between pointers to unrelated objects '&x' and '&extern_n' has unspecified value}} \+                            // interpreter-note {{initializer of 'extern_n' is unknown}}+  }+  static_assert(!f3()); // expected-error {{static assertion expression is not an integral constant expression}} \+                        // expected-note {{in call to 'f3()'}}+  constexpr int f4() {+    int *p = new int;+    bool b = p == &extern_n; // nointerpreter-note {{comparison between pointers to unrelated objects '&{*new int#0}' and '&extern_n' has unspecified value}} \+                             // interpreter-note {{initializer of 'extern_n' is unknown}}+    delete p;+    return b;+  }+  static_assert(!f4()); // expected-error {{static assertion expression is not an integral constant expression}} \+                        // expected-note {{in call to 'f4()'}} }

@nikicnikic moved this fromNeeds Triage toNeeds Review inLLVM Release StatusJul 15, 2025
@tru
Copy link
Collaborator

tru commentedJul 17, 2025

@zygoloid how about merging this to the release branch?

Copy link
Collaborator

@zygoloidzygoloid left a comment

Choose a reason for hiding this comment

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

LG for branch.

@github-project-automationgithub-project-automationbot moved this fromNeeds Review toNeeds Merge inLLVM Release StatusJul 17, 2025
…llvm#147663)A constexpr-unknown reference can be equal to an arbitrary value, exceptvalues allocated during constant evaluation. Fix the handling.The standard is unclear exactly which pointer comparisons count as"unknown" in this context; for example, in some cases we could usealignment to prove two constexpr-unknown references are not equal. Idecided to ignore all the cases involving variables not allocated duringconstant evaluation.While looking at this, I also spotted that there might be issues withlifetimes, but I didn't try to address it.(cherry picked from commit20c8e3c)
@trutru merged commit2067574 intollvm:release/21.xJul 17, 2025
@github-project-automationgithub-project-automationbot moved this fromNeeds Merge toDone inLLVM Release StatusJul 17, 2025
@trutru deleted the issue147663 branchJuly 17, 2025 15:56
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@zygoloidzygoloidzygoloid approved these changes

Assignees
No one assigned
Labels
clang:frontendLanguage frontend issues, e.g. anything involving "Sema"clangClang issues not falling into any other category
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants
@llvmbot@tru@zygoloid@efriedma-quic

[8]ページ先頭

©2009-2025 Movatter.jp