Skip to content

[Clang] Fix the assertion condition after b8d1f3d6 #132669

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 2 commits into from
Mar 24, 2025

Conversation

zyn0217
Copy link
Contributor

@zyn0217 zyn0217 commented Mar 24, 2025

Thanks to the example provided by @MagentaTreehouse, I realized the assertion I added in b8d1f3d didn't cover all valid cases like, when inheriting from a class template specialization, the source of a synthesized template parameter might be an implicit specialization, whose inner function template is thus living at depth 0, for which we don’t want it to overflow too.

I've decided to remove that assertion because I don't think it's particularly useful: we're checking whether Depth = 0 parameters come from function templates whose parents contribute no template parameters to the depth, which is redundant given what the template depth already means.

This also incorporates a drive-by fix for #132061 (comment), which I somehow missed.

@zyn0217 zyn0217 requested review from mizvekov, cor3ntin and hokein March 24, 2025 04:53
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Mar 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2025

@llvm/pr-subscribers-clang

Author: Younan Zhang (zyn0217)

Changes

Thanks to the example provided by @MagentaTreehouse, I realized the assertion I added didn't cover all valid cases like, when inheriting from a class template specialization, the source of a synthesized template parameter might be an implicit specialization, whose inner function template is thus living at depth 0, for which we don’t want it to overflow too.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateDeductionGuide.cpp (+7-5)
  • (modified) clang/test/SemaTemplate/deduction-guide.cpp (+26)
diff --git a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
index 9cfdb7596b660..e20130a33f368 100644
--- a/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
+++ b/clang/lib/Sema/SemaTemplateDeductionGuide.cpp
@@ -376,12 +376,14 @@ struct ConvertConstructorToDeductionGuideTransform {
         if (NestedPattern)
           Args.addOuterRetainedLevels(NestedPattern->getTemplateDepth());
         auto [Depth, Index] = getDepthAndIndex(Param);
-        // Depth can still be 0 if FTD belongs to an explicit class template
-        // specialization with an empty template parameter list. In that case,
-        // we don't want the NewDepth to overflow, and it should remain 0.
+        // Depth can be 0 if FTD belongs to a class template specialization with
+        // an empty template parameter list (i.e. either an explicit full
+        // specialization or an implicit specialization) In that case, we don't
+        // want the NewDepth to overflow, and it should remain 0.
         assert(Depth ||
-               cast<ClassTemplateSpecializationDecl>(FTD->getDeclContext())
-                   ->isExplicitSpecialization());
+               isa<ClassTemplateDecl *>(
+                   cast<ClassTemplateSpecializationDecl>(FTD->getDeclContext())
+                       ->getSpecializedTemplateOrPartial()));
         NamedDecl *NewParam = transformTemplateParameter(
             SemaRef, DC, Param, Args, Index + Depth1IndexAdjustment,
             Depth ? Depth - 1 : 0);
diff --git a/clang/test/SemaTemplate/deduction-guide.cpp b/clang/test/SemaTemplate/deduction-guide.cpp
index ecd152abebd74..95e740a026550 100644
--- a/clang/test/SemaTemplate/deduction-guide.cpp
+++ b/clang/test/SemaTemplate/deduction-guide.cpp
@@ -723,3 +723,29 @@ void test() { NewDeleteAllocator abc(42); } // expected-error {{no viable constr
 // CHECK-NEXT:  `-ParmVarDecl {{.+}} 'T'
 
 } // namespace GH128691
+
+namespace GH132616_DeductionGuide {
+
+template <class T> struct A {
+  template <class U>
+  A(U);
+};
+
+template <typename>
+struct B: A<int> {
+  using A::A;
+};
+
+template <class T>
+B(T) -> B<T>;
+
+B b(24);
+
+// CHECK-LABEL: Dumping GH132616_DeductionGuide::<deduction guide for B>:
+// CHECK-NEXT: FunctionTemplateDecl {{.+}} implicit <deduction guide for B>
+// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} typename depth 0 index 0
+// CHECK-NEXT: |-TemplateTypeParmDecl {{.+}} class depth 0 index 1 U
+// CHECK-NEXT: `-CXXDeductionGuideDecl {{.+}} implicit <deduction guide for B> 'auto (U) -> B<type-parameter-0-0>'
+// CHECK-NEXT:  `-ParmVarDecl {{.+}} 'U'
+
+} // namespace GH132616_DeductionGuide

@zyn0217 zyn0217 force-pushed the GH132616-deduction-guide-depth branch from 8562b2b to 49f2a7e Compare March 24, 2025 05:19
@zyn0217 zyn0217 marked this pull request as draft March 24, 2025 05:33
zyn0217 added 2 commits March 24, 2025 13:59
Thanks to the example provided by @MagentaTreehouse, I realized the
assertion I added didn't cover all valid cases like, when inheriting
from a class template specialization, the source of a synthesized
template parameter might be an implicit specialization, whose inner
function template is thus living at depth 0, for which we don’t want
it to overflow too.
@zyn0217 zyn0217 force-pushed the GH132616-deduction-guide-depth branch from 49f2a7e to b7bef60 Compare March 24, 2025 05:59
@zyn0217 zyn0217 marked this pull request as ready for review March 24, 2025 06:07
@zyn0217 zyn0217 merged commit 38d71c9 into llvm:main Mar 24, 2025
13 checks passed
@zyn0217 zyn0217 deleted the GH132616-deduction-guide-depth branch March 24, 2025 08:46
@llvm-ci
Copy link
Collaborator

llvm-ci commented Mar 24, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building clang at step 6 "test".

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

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: commands/expression/calculator_mode/TestCalculatorMode.py (31 of 2109)
PASS: lldb-api :: commands/expression/anonymous-struct/TestCallUserAnonTypedef.py (32 of 2109)
PASS: lldb-api :: commands/expression/argument_passing_restrictions/TestArgumentPassingRestrictions.py (33 of 2109)
PASS: lldb-api :: commands/expression/bitfield_enums/TestBitfieldEnums.py (34 of 2109)
PASS: lldb-api :: api/listeners/TestListener.py (35 of 2109)
PASS: lldb-api :: commands/expression/call-function/TestCallBuiltinFunction.py (36 of 2109)
UNSUPPORTED: lldb-api :: commands/expression/call-throws/TestCallThatThrows.py (37 of 2109)
PASS: lldb-api :: commands/command/script/TestCommandScript.py (38 of 2109)
PASS: lldb-api :: commands/expression/call-restarts/TestCallThatRestarts.py (39 of 2109)
PASS: lldb-api :: commands/expression/call-function/TestCallStdStringFunction.py (40 of 2109)
FAIL: lldb-api :: commands/expression/call-function/TestCallStopAndContinue.py (41 of 2109)
******************** TEST 'lldb-api :: commands/expression/call-function/TestCallStopAndContinue.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/commands/expression/call-function -p TestCallStopAndContinue.py
--
Exit Code: -11

Command Output (stdout):
--
lldb version 21.0.0git (https://github.com/llvm/llvm-project.git revision 38d71c9bdcf6b10c6fe02d5bd74fc8e6efb50a4d)
  clang revision 38d71c9bdcf6b10c6fe02d5bd74fc8e6efb50a4d
  llvm revision 38d71c9bdcf6b10c6fe02d5bd74fc8e6efb50a4d
Skipping the following test categories: ['libc++', 'dsym', 'gmodules', 'debugserver', 'objc']

--
Command Output (stderr):
--
UNSUPPORTED: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_dsym (TestCallStopAndContinue.ExprCommandCallStopContinueTestCase) (test case does not fall in any category of interest for this run) 
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_dwarf (TestCallStopAndContinue.ExprCommandCallStopContinueTestCase)
PASS: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_dwo (TestCallStopAndContinue.ExprCommandCallStopContinueTestCase)
----------------------------------------------------------------------
Ran 3 tests in 1.016s

OK (skipped=1)

--

********************
PASS: lldb-api :: commands/expression/cast_int_to_anonymous_enum/TestCastIntToAnonymousEnum.py (42 of 2109)
PASS: lldb-api :: commands/expression/call-function/TestCallUserDefinedFunction.py (43 of 2109)
UNSUPPORTED: lldb-api :: commands/expression/completion-crash-invalid-iterator/TestInvalidIteratorCompletionCrash.py (44 of 2109)
PASS: lldb-api :: commands/expression/codegen-crash-import-def-arraytype-element/TestImportDefinitionArrayType.py (45 of 2109)
PASS: lldb-api :: commands/expression/class_template_specialization_empty_pack/TestClassTemplateSpecializationParametersHandling.py (46 of 2109)
PASS: lldb-api :: commands/expression/completion-crash-incomplete-record/TestCompletionCrashIncompleteRecord.py (47 of 2109)
PASS: lldb-api :: commands/expression/codegen-crash-typedefdecl-not-in_declcontext/TestCodegenCrashTypedefDeclNotInDeclContext.py (48 of 2109)
UNSUPPORTED: lldb-api :: commands/expression/context-object-objc/TestContextObjectObjc.py (49 of 2109)
PASS: lldb-api :: commands/expression/completion-in-lambda-and-unnamed-class/TestCompletionInLambdaAndUnnamedClass.py (50 of 2109)
PASS: lldb-api :: commands/expression/char/TestExprsChar.py (51 of 2109)

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.

4 participants