Skip to content

[Clang] Evaluate dependent indexes of pack indexing in a constant context #106054

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

Merged
merged 3 commits into from
Aug 26, 2024

Conversation

cor3ntin
Copy link
Contributor

Fixes #105900

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

Fixes #105900


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/TreeTransform.h (+17-6)
  • (modified) clang/test/SemaCXX/cxx2c-pack-indexing.cpp (+27)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8f98167dff31ef..384471bf860c5b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -292,6 +292,7 @@ Bug Fixes to C++ Support
 - Correctly check constraints of explicit instantiations of member functions. (#GH46029)
 - Fixed an assertion failure about a constraint of a friend function template references to a value with greater
   template depth than the friend function template. (#GH98258)
+- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 62287c2d26375c..b3854cd8f82220 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6669,9 +6669,15 @@ QualType
 TreeTransform<Derived>::TransformPackIndexingType(TypeLocBuilder &TLB,
                                                   PackIndexingTypeLoc TL) {
   // Transform the index
-  ExprResult IndexExpr = getDerived().TransformExpr(TL.getIndexExpr());
-  if (IndexExpr.isInvalid())
-    return QualType();
+  ExprResult IndexExpr;
+  {
+    EnterExpressionEvaluationContext ConstantContext(
+        SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
+
+    IndexExpr = getDerived().TransformExpr(TL.getIndexExpr());
+    if (IndexExpr.isInvalid())
+      return QualType();
+  }
   QualType Pattern = TL.getPattern();
 
   const PackIndexingType *PIT = TL.getTypePtr();
@@ -15299,9 +15305,14 @@ TreeTransform<Derived>::TransformPackIndexingExpr(PackIndexingExpr *E) {
     return E;
 
   // Transform the index
-  ExprResult IndexExpr = getDerived().TransformExpr(E->getIndexExpr());
-  if (IndexExpr.isInvalid())
-    return ExprError();
+  ExprResult IndexExpr;
+  {
+    EnterExpressionEvaluationContext ConstantContext(
+        SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
+    IndexExpr = getDerived().TransformExpr(E->getIndexExpr());
+    if (IndexExpr.isInvalid())
+      return ExprError();
+  }
 
   SmallVector<Expr *, 5> ExpandedExprs;
   if (!E->expandsToEmptyPack() && E->getExpressions().empty()) {
diff --git a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
index 9ea90a4c3e30fd..37dceaee64fac2 100644
--- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
+++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
@@ -231,3 +231,30 @@ struct type_info {
 namespace GH93650 {
 auto func(auto... inputArgs) { return typeid(inputArgs...[0]); }
 } // namespace GH93650
+
+
+namespace GH105900 {
+
+template <typename... opts>
+struct types  {
+    template <unsigned idx>
+    static constexpr __SIZE_TYPE__ get_index() { return idx; }
+
+    template <unsigned s>
+    static auto x() -> opts...[get_index<s>()] {}
+};
+
+template <auto... opts>
+struct vars  {
+    template <unsigned idx>
+    static constexpr __SIZE_TYPE__ get_index() { return idx; }
+
+    template <unsigned s>
+    static auto x() -> decltype(opts...[get_index<s>()]) {return 0;}
+};
+
+void f() {
+    types<void>::x<0>(); // Error
+    vars<0>::x<0>(); // Error
+}
+}

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment on lines 257 to 258
types<void>::x<0>(); // Error
vars<0>::x<0>(); // Error
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
types<void>::x<0>(); // Error
vars<0>::x<0>(); // Error
types<void>::x<0>();
vars<0>::x<0>();

@cor3ntin cor3ntin merged commit 5a288b9 into llvm:main Aug 26, 2024
9 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Aug 26, 2024

LLVM Buildbot has detected a new failure on builder libc-x86_64-debian-fullbuild-dbg running on libc-x86_64-debian-fullbuild while building clang at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/179/builds/4976

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/libc-linux.py ...' (failure)
...
[       OK ] LlvmLibcFILETest.FFlush (82 us)
[ RUN      ] LlvmLibcFILETest.FOpenFWriteSizeGreaterThanOne
[       OK ] LlvmLibcFILETest.FOpenFWriteSizeGreaterThanOne (90 us)
Ran 3 tests.  PASS: 3  FAIL: 0
[827/1074] Running unit test libc.test.src.stdio.vfprintf_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcVFPrintfTest.WriteToFile
[       OK ] LlvmLibcVFPrintfTest.WriteToFile (192 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[828/1074] Running unit test libc.test.src.stdio.fscanf_test.__unit__
FAILED: projects/libc/test/src/stdio/CMakeFiles/libc.test.src.stdio.fscanf_test.__unit__ /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/build/projects/libc/test/src/stdio/CMakeFiles/libc.test.src.stdio.fscanf_test.__unit__ 
cd /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/build/projects/libc/test/src/stdio && /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/build/projects/libc/test/src/stdio/libc.test.src.stdio.fscanf_test.__unit__.__build__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcFScanfTest.WriteToFile
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/llvm-project/libc/test/src/stdio/fscanf_test.cpp:73: FAILURE
      Expected: read
      Which is: 0
To be equal to: 1
      Which is: 1
[  FAILED  ] LlvmLibcFScanfTest.WriteToFile
Ran 1 tests.  PASS: 0  FAIL: 1
[829/1074] Running unit test libc.test.src.stdio.vfscanf_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcFScanfTest.WriteToFile
[       OK ] LlvmLibcFScanfTest.WriteToFile (203 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[830/1074] Running unit test libc.test.src.stdio.fopen_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcFOpenTest.PrintToFile
[       OK ] LlvmLibcFOpenTest.PrintToFile (158 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[831/1074] Running unit test libc.test.src.stdio.putc_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcPutcTest.WriteToFile
[       OK ] LlvmLibcPutcTest.WriteToFile (182 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[832/1074] Running unit test libc.test.src.stdio.fdopen_test.__unit__
[==========] Running 3 tests from 1 test suite.
[ RUN      ] LlvmLibcStdioFdopenTest.WriteAppendRead
[       OK ] LlvmLibcStdioFdopenTest.WriteAppendRead (174 us)
[ RUN      ] LlvmLibcStdioFdopenTest.InvalidFd
[       OK ] LlvmLibcStdioFdopenTest.InvalidFd (12 us)
[ RUN      ] LlvmLibcStdioFdopenTest.InvalidMode
[       OK ] LlvmLibcStdioFdopenTest.InvalidMode (11 us)
Ran 3 tests.  PASS: 3  FAIL: 0
[833/1074] Running unit test libc.test.src.stdio.fgetc_test.__unit__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcGetcTest.WriteAndReadCharactersWithFgetc
[       OK ] LlvmLibcGetcTest.WriteAndReadCharactersWithFgetc (150 us)
Step 8 (libc-unit-tests) failure: libc-unit-tests (failure)
...
[       OK ] LlvmLibcFILETest.FFlush (82 us)
[ RUN      ] LlvmLibcFILETest.FOpenFWriteSizeGreaterThanOne
[       OK ] LlvmLibcFILETest.FOpenFWriteSizeGreaterThanOne (90 us)
Ran 3 tests.  PASS: 3  FAIL: 0
[827/1074] Running unit test libc.test.src.stdio.vfprintf_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcVFPrintfTest.WriteToFile
[       OK ] LlvmLibcVFPrintfTest.WriteToFile (192 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[828/1074] Running unit test libc.test.src.stdio.fscanf_test.__unit__
FAILED: projects/libc/test/src/stdio/CMakeFiles/libc.test.src.stdio.fscanf_test.__unit__ /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/build/projects/libc/test/src/stdio/CMakeFiles/libc.test.src.stdio.fscanf_test.__unit__ 
cd /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/build/projects/libc/test/src/stdio && /home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/build/projects/libc/test/src/stdio/libc.test.src.stdio.fscanf_test.__unit__.__build__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcFScanfTest.WriteToFile
/home/llvm-libc-buildbot/buildbot-worker/libc-x86_64-debian-fullbuild/libc-x86_64-debian-fullbuild-dbg/llvm-project/libc/test/src/stdio/fscanf_test.cpp:73: FAILURE
      Expected: read
      Which is: 0
To be equal to: 1
      Which is: 1
[  FAILED  ] LlvmLibcFScanfTest.WriteToFile
Ran 1 tests.  PASS: 0  FAIL: 1
[829/1074] Running unit test libc.test.src.stdio.vfscanf_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcFScanfTest.WriteToFile
[       OK ] LlvmLibcFScanfTest.WriteToFile (203 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[830/1074] Running unit test libc.test.src.stdio.fopen_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcFOpenTest.PrintToFile
[       OK ] LlvmLibcFOpenTest.PrintToFile (158 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[831/1074] Running unit test libc.test.src.stdio.putc_test.__unit__
[==========] Running 1 test from 1 test suite.
[ RUN      ] LlvmLibcPutcTest.WriteToFile
[       OK ] LlvmLibcPutcTest.WriteToFile (182 us)
Ran 1 tests.  PASS: 1  FAIL: 0
[832/1074] Running unit test libc.test.src.stdio.fdopen_test.__unit__
[==========] Running 3 tests from 1 test suite.
[ RUN      ] LlvmLibcStdioFdopenTest.WriteAppendRead
[       OK ] LlvmLibcStdioFdopenTest.WriteAppendRead (174 us)
[ RUN      ] LlvmLibcStdioFdopenTest.InvalidFd
[       OK ] LlvmLibcStdioFdopenTest.InvalidFd (12 us)
[ RUN      ] LlvmLibcStdioFdopenTest.InvalidMode
[       OK ] LlvmLibcStdioFdopenTest.InvalidMode (11 us)
Ran 3 tests.  PASS: 3  FAIL: 0
[833/1074] Running unit test libc.test.src.stdio.fgetc_test.__unit__
[==========] Running 2 tests from 1 test suite.
[ RUN      ] LlvmLibcGetcTest.WriteAndReadCharactersWithFgetc
[       OK ] LlvmLibcGetcTest.WriteAndReadCharactersWithFgetc (150 us)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Clang] Error on pack indexing in return type
4 participants