Skip to content

[Clang] skip default argument instantiation for non-defining friend declarations without specialization info to meet [dcl.fct.default] p4 #113777

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 1 commit into from
Nov 7, 2024

Conversation

a-tarasyuk
Copy link
Member

@a-tarasyuk a-tarasyuk commented Oct 26, 2024

This fixes a crash when instantiating default arguments for templated friend function declarations which lack a definition.
There are implementation limits which prevents us from finding the pattern for such functions, and this causes difficulties
setting up the instantiation scope for the function parameters.

This patch skips instantiating the default argument in these cases, which causes a minor regression in error recovery, but otherwise avoids the crash.

Fixes #113324

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

llvmbot commented Oct 26, 2024

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #113324


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+4-4)
  • (modified) clang/test/CXX/temp/temp.res/p4.cpp (+7)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6a95337815174b..428ec8c87a432e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -561,6 +561,8 @@ Bug Fixes to C++ Support
   const-default-constructible even if a union member has a default member initializer.
   (#GH95854).
 - Fixed an assertion failure when evaluating an invalid expression in an array initializer (#GH112140)
+- Fixed an assertion failure caused by an invalid template instantiation pattern
+  for adding instantiated parameters to the scope in friend functions with defaulted parameters. (#GH113324).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 6a55861fe5af3b..cddfcc48312042 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -3435,10 +3435,10 @@ bool Sema::SubstDefaultArgument(
       //   template<typename T> void f(T a, int = decltype(a)());
       //   void g() { f(0); }
       LIS = std::make_unique<LocalInstantiationScope>(*this);
-      FunctionDecl *PatternFD = FD->getTemplateInstantiationPattern(
-          /*ForDefinition*/ false);
-      if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
-        return true;
+      if (FunctionDecl *PatternFD =
+              FD->getTemplateInstantiationPattern(/*ForDefinition*/ false))
+        if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
+          return true;
     }
 
     runWithSufficientStackSpace(Loc, [&] {
diff --git a/clang/test/CXX/temp/temp.res/p4.cpp b/clang/test/CXX/temp/temp.res/p4.cpp
index f54d8649f5da88..62bd766e7e1140 100644
--- a/clang/test/CXX/temp/temp.res/p4.cpp
+++ b/clang/test/CXX/temp/temp.res/p4.cpp
@@ -185,3 +185,10 @@ template<typename T> struct S {
   friend void X::f(T::type);
 };
 }
+
+namespace GH113324 {
+template <typename = int> struct ct {
+  friend void f(ct, int = 0); // expected-error {{friend declaration specifying a default argument must be a definition}}
+};
+void test() { f(ct<>{}); }
+}

@a-tarasyuk a-tarasyuk requested a review from zyn0217 November 1, 2024 20:02
@a-tarasyuk a-tarasyuk changed the title [Clang] prevent assertion failure from an invalid template instantiation pattern when adding instantiated params to the scope in friend functions with defaulted params [Clang] Ensure default arguments in friend declarations are only allowed in defining declarations to prevent multiple reachable declarations Nov 6, 2024
@a-tarasyuk a-tarasyuk requested a review from mizvekov November 6, 2024 14:14
@a-tarasyuk a-tarasyuk requested a review from mizvekov November 6, 2024 17:51
@a-tarasyuk a-tarasyuk requested a review from mizvekov November 6, 2024 21:58
Copy link
Contributor

@mizvekov mizvekov left a comment

Choose a reason for hiding this comment

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

LGTM except for a couple of nits.

Thanks!

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.

LGTM as well, thanks for working on it!

@zyn0217
Copy link
Contributor

zyn0217 commented Nov 7, 2024

BTW, can you flesh out the commit message so we don't just have a single issue link there? Thanks

@a-tarasyuk
Copy link
Member Author

@zyn0217 should all the commits be squashed into one?

@zyn0217
Copy link
Contributor

zyn0217 commented Nov 7, 2024

@zyn0217 should all the commits be squashed into one?

Of course.

By saying “flesh out the commit message”, I meant to edit the PR description on github, which would become the final commit message when we merge the PR.

…eclarations without specialization info to meet [dcl.fct.default]p4
@a-tarasyuk a-tarasyuk changed the title [Clang] Ensure default arguments in friend declarations are only allowed in defining declarations to prevent multiple reachable declarations [Clang] skip default argument instantiation for non-defining friend declarations without specialization info to meet [dcl.fct.default] p4 Nov 7, 2024
@mizvekov
Copy link
Contributor

mizvekov commented Nov 7, 2024

@zyn0217 meant the PR description, which is the body of the commit message, not the title.
I already fixed it for you.

@mizvekov mizvekov merged commit f8b9616 into llvm:main Nov 7, 2024
9 checks passed
@a-tarasyuk
Copy link
Member Author

@mizvekov thanks

@alanzhao1
Copy link
Contributor

FYI this is causing Clang to crash with Chrome: https://crbug.com/377956048

I'm currently working on getting a reduced repro with CReduce.

@a-tarasyuk
Copy link
Member Author

@alanzhao1 thanks for catching that. should I revert it?

@alanzhao1
Copy link
Contributor

Based on the patch reversion policy, I think reverting is the right thing to do.

(I'm working on the reduced repro, but creduce is really slow. In the meantime, I attached the unreduced files).

unreduced.tar.gz

@alanzhao1
Copy link
Contributor

For posterity, here's the entire stacktrace I got:

/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang++ -MMD -MF obj/third_party/dawn/src/dawn/tests/dawn_unittests/CacheRequestTests.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_OZONE=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -DCR_LIBCXX_REVISION=8e31ad42561900383e10dbefc1d3e8f38cedfbe9 -DTMP_REBUILD_HACK -DCR_SYSROOT_KEY=20230611T210420Z-2 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DDAWN_ABORT_ON_ASSERT -DDAWN_ENABLE_BACKEND_NULL -DDAWN_ENABLE_BACKEND_OPENGL -DDAWN_ENABLE_BACKEND_DESKTOP_GL -DDAWN_ENABLE_BACKEND_OPENGLES -DDAWN_ENABLE_BACKEND_VULKAN -DDAWN_USE_X11 -DTINT_BUILD_SPV_READER=0 -DTINT_BUILD_SPV_WRITER=1 -DTINT_BUILD_WGSL_READER=1 -DTINT_BUILD_WGSL_WRITER=1 -DTINT_BUILD_MSL_WRITER=1 -DTINT_BUILD_HLSL_WRITER=1 -DTINT_BUILD_GLSL_WRITER=1 -DTINT_BUILD_GLSL_VALIDATOR=1 -DTINT_BUILD_SYNTAX_TREE_WRITER=0 -DTINT_BUILD_IR_BINARY=1 -DTINT_BUILD_IR_FUZZER=0 -DTINT_BUILD_IS_WIN=0 -DTINT_BUILD_IS_MAC=0 -DTINT_BUILD_IS_LINUX=1 -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_56 -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_56 -DBENCHMARK_STATIC_DEFINE -DCR_CXX_INCLUDE=\"third_party/rust/chromium_crates_io/vendor/cxx-1.0.129/include/cxx.h\" -DU_USING_ICU_NAMESPACE=0 -DU_ENABLE_DYLOAD=0 -DUSE_CHROMIUM_ICU=1 -DU_ENABLE_TRACING=1 -DU_ENABLE_RESOURCE_TRACING=0 -DU_STATIC_IMPLEMENTATION -DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE -DGTEST_API_= -DGTEST_HAS_POSIX_RE=0 -DGTEST_LANG_CXX11=1 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_ABSL -DGTEST_NO_ABSL_FLAGS -DUNIT_TEST -DVK_USE_PLATFORM_XCB_KHR -DVK_USE_PLATFORM_WAYLAND_KHR -I../.. -Igen -I../../buildtools/third_party/libc++ -Igen/third_party/dawn/src -I../../third_party/dawn/src -I../../third_party/dawn -I../../third_party/dawn/include -Igen/third_party/dawn -I../../third_party/perfetto/include -Igen/third_party/perfetto/build_config -Igen/third_party/perfetto -I../../third_party/google_benchmark/src/include -Igen/third_party/dawn/include -I../../base/allocator/partition_allocator/src -Igen/base/allocator/partition_allocator/src -I../../third_party/abseil-cpp -I../../third_party/boringssl/src/include -I../../third_party/protobuf/src -I../../third_party/ced/src -I../../third_party/icu/source/common -I../../third_party/icu/source/i18n -I../../third_party/googletest/custom -I../../third_party/googletest/src/googlemock/include -I../../third_party/googletest/src/googletest/include -I../../third_party/re2/src -I../../third_party/vulkan-headers/src/include -I../../third_party/wayland/src/src -I../../third_party/wayland/include/src -I../../third_party/dawn/third_party/khronos/EGL-Registry/api -Wall -Wextra -Wimplicit-fallthrough -Wextra-semi -Wunreachable-code-aggressive -Wthread-safety -Wgnu -Wno-gnu-anonymous-struct -Wno-gnu-conditional-omitted-operand -Wno-gnu-include-next -Wno-gnu-label-as-value -Wno-gnu-redeclared-enum -Wno-gnu-statement-expression -Wno-gnu-zero-variadic-macro-arguments -Wno-zero-length-array -Wno-missing-field-initializers -Wno-unused-parameter -Wno-psabi -Wloop-analysis -Wno-unneeded-internal-declaration -Wno-cast-function-type -Wno-thread-safety-reference-return -Wshadow -Werror -fno-delete-null-pointer-checks -fno-ident -fno-strict-aliasing -fstack-protector -funwind-tables -fPIC -pthread -fcolor-diagnostics -fmerge-all-constants -fno-sized-deallocation -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -mllvm -split-threshold-for-reg-with-hint=0 -ffp-contract=off -Wa,--crel,--allow-experimental-crel -fcomplete-member-pointers -m64 -msse3 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -ffile-compilation-dir=. -no-canonical-prefixes -ftrivial-auto-var-init=pattern -O2 -fdata-sections -ffunction-sections -fno-unique-section-names -fno-math-errno -fno-omit-frame-pointer -g0 -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -isystem../../build/linux/debian_bullseye_amd64-sysroot/usr/include/glib-2.0 -isystem../../build/linux/debian_bullseye_amd64-sysroot/usr/lib/x86_64-linux-gnu/glib-2.0/include -DPROTOBUF_ALLOW_DEPRECATED=1 -Wno-inconsistent-missing-override -Wno-redundant-parens -Wno-invalid-offsetof -Wenum-compare-conditional -Wno-c++11-narrowing-const-reference -Wno-missing-template-arg-list-after-template-kw -Wno-dangling-assignment-gsl -Wno-nontrivial-memaccess -std=c++20 -Wno-trigraphs -gsimple-template-names -fno-exceptions -fno-rtti -nostdinc++ -isystem../../third_party/libc++/src/include -isystem../../third_party/libc++abi/src/include --sysroot=../../build/linux/debian_bullseye_amd64-sysroot -fvisibility-inlines-hidden -c ../../third_party/dawn/src/dawn/tests/unittests/native/CacheRequestTests.cpp -o obj/third_party/dawn/src/dawn/tests/dawn_unittests/CacheRequestTests.o
clang++: /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/AST/ASTContext.cpp:2398: TypeInfo clang::ASTContext::getTypeInfoImpl(const Type *) const: Assertion `!A->getDeducedType().isNull() && "cannot request the size of an undeduced or dependent auto type"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang++ -MMD -MF obj/third_party/dawn/src/dawn/tests/dawn_unittests/CacheRequestTests.o.d -DUSE_UDEV -DUSE_AURA=1 -DUSE_GLIB=1 -DUSE_OZONE=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D_FORTIFY_SOURCE=2 -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_GNU_SOURCE -D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE -D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS -D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS -DCR_LIBCXX_REVISION=8e31ad42561900383e10dbefc1d3e8f38cedfbe9 -DTMP_REBUILD_HACK -DCR_SYSROOT_KEY=20230611T210420Z-2 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DDAWN_ABORT_ON_ASSERT -DDAWN_ENABLE_BACKEND_NULL -DDAWN_ENABLE_BACKEND_OPENGL -DDAWN_ENABLE_BACKEND_DESKTOP_GL -DDAWN_ENABLE_BACKEND_OPENGLES -DDAWN_ENABLE_BACKEND_VULKAN -DDAWN_USE_X11 -DTINT_BUILD_SPV_READER=0 -DTINT_BUILD_SPV_WRITER=1 -DTINT_BUILD_WGSL_READER=1 -DTINT_BUILD_WGSL_WRITER=1 -DTINT_BUILD_MSL_WRITER=1 -DTINT_BUILD_HLSL_WRITER=1 -DTINT_BUILD_GLSL_WRITER=1 -DTINT_BUILD_GLSL_VALIDATOR=1 -DTINT_BUILD_SYNTAX_TREE_WRITER=0 -DTINT_BUILD_IR_BINARY=1 -DTINT_BUILD_IR_FUZZER=0 -DTINT_BUILD_IS_WIN=0 -DTINT_BUILD_IS_MAC=0 -DTINT_BUILD_IS_LINUX=1 -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_56 -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_56 -DBENCHMARK_STATIC_DEFINE -DCR_CXX_INCLUDE=\"third_party/rust/chromium_crates_io/vendor/cxx-1.0.129/include/cxx.h\" -DU_USING_ICU_NAMESPACE=0 -DU_ENABLE_DYLOAD=0 -DUSE_CHROMIUM_ICU=1 -DU_ENABLE_TRACING=1 -DU_ENABLE_RESOURCE_TRACING=0 -DU_STATIC_IMPLEMENTATION -DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE -DGTEST_API_= -DGTEST_HAS_POSIX_RE=0 -DGTEST_LANG_CXX11=1 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_ABSL -DGTEST_NO_ABSL_FLAGS -DUNIT_TEST -DVK_USE_PLATFORM_XCB_KHR -DVK_USE_PLATFORM_WAYLAND_KHR -I../.. -Igen -I../../buildtools/third_party/libc++ -Igen/third_party/dawn/src -I../../third_party/dawn/src -I../../third_party/dawn -I../../third_party/dawn/include -Igen/third_party/dawn -I../../third_party/perfetto/include -Igen/third_party/perfetto/build_config -Igen/third_party/perfetto -I../../third_party/google_benchmark/src/include -Igen/third_party/dawn/include -I../../base/allocator/partition_allocator/src -Igen/base/allocator/partition_allocator/src -I../../third_party/abseil-cpp -I../../third_party/boringssl/src/include -I../../third_party/protobuf/src -I../../third_party/ced/src -I../../third_party/icu/source/common -I../../third_party/icu/source/i18n -I../../third_party/googletest/custom -I../../third_party/googletest/src/googlemock/include -I../../third_party/googletest/src/googletest/include -I../../third_party/re2/src -I../../third_party/vulkan-headers/src/include -I../../third_party/wayland/src/src -I../../third_party/wayland/include/src -I../../third_party/dawn/third_party/khronos/EGL-Registry/api -Wall -Wextra -Wimplicit-fallthrough -Wextra-semi -Wunreachable-code-aggressive -Wthread-safety -Wgnu -Wno-gnu-anonymous-struct -Wno-gnu-conditional-omitted-operand -Wno-gnu-include-next -Wno-gnu-label-as-value -Wno-gnu-redeclared-enum -Wno-gnu-statement-expression -Wno-gnu-zero-variadic-macro-arguments -Wno-zero-length-array -Wno-missing-field-initializers -Wno-unused-parameter -Wno-psabi -Wloop-analysis -Wno-unneeded-internal-declaration -Wno-cast-function-type -Wno-thread-safety-reference-return -Wshadow -Werror -fno-delete-null-pointer-checks -fno-ident -fno-strict-aliasing -fstack-protector -funwind-tables -fPIC -pthread -fcolor-diagnostics -fmerge-all-constants -fno-sized-deallocation -fcrash-diagnostics-dir=../../tools/clang/crashreports -mllvm -instcombine-lower-dbg-declare=0 -mllvm -split-threshold-for-reg-with-hint=0 -ffp-contract=off -Wa,--crel,--allow-experimental-crel -fcomplete-member-pointers -m64 -msse3 -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -ffile-compilation-dir=. -no-canonical-prefixes -ftrivial-auto-var-init=pattern -O2 -fdata-sections -ffunction-sections -fno-unique-section-names -fno-math-errno -fno-omit-frame-pointer -g0 -fvisibility=hidden -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -isystem../../build/linux/debian_bullseye_amd64-sysroot/usr/include/glib-2.0 -isystem../../build/linux/debian_bullseye_amd64-sysroot/usr/lib/x86_64-linux-gnu/glib-2.0/include -DPROTOBUF_ALLOW_DEPRECATED=1 -Wno-inconsistent-missing-override -Wno-redundant-parens -Wno-invalid-offsetof -Wenum-compare-conditional -Wno-c++11-narrowing-const-reference -Wno-missing-template-arg-list-after-template-kw -Wno-dangling-assignment-gsl -Wno-nontrivial-memaccess -std=c++20 -Wno-trigraphs -gsimple-template-names -fno-exceptions -fno-rtti -nostdinc++ -isystem../../third_party/libc++/src/include -isystem../../third_party/libc++abi/src/include --sysroot=../../build/linux/debian_bullseye_amd64-sysroot -fvisibility-inlines-hidden -c ../../third_party/dawn/src/dawn/tests/unittests/native/CacheRequestTests.cpp -o obj/third_party/dawn/src/dawn/tests/dawn_unittests/CacheRequestTests.o
1.      <eof> parser at end of file
2.      Per-file LLVM IR generation
3.      ../../third_party/dawn/src/dawn/tests/unittests/native/CacheRequestTests.cpp:76:1 <Spelling=../../third_party/googletest/src/googletest/include/gtest/internal/gtest-internal.h:1519:60>: Generating code for declaration 'dawn::native::(anonymous namespace)::CacheRequestTests_CacheResultTypes_Test::TestBody'
 #0 0x0000565135d3a488 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x503c488)
 #1 0x0000565135d3803e llvm::sys::RunSignalHandlers() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x503a03e)
 #2 0x0000565135ca8ab6 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007fb049e591a0 (/lib/x86_64-linux-gnu/libc.so.6+0x3d1a0)
 #4 0x00007fb049ea70ec __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x00007fb049e59102 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #6 0x00007fb049e424f2 abort ./stdlib/abort.c:81:7
 #7 0x00007fb049e42415 _nl_load_domain ./intl/loadmsgcat.c:1177:9
 #8 0x00007fb049e51d32 (/lib/x86_64-linux-gnu/libc.so.6+0x35d32)
 #9 0x00005651395a6220 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x88a8220)
#10 0x00005651395a8150 clang::ASTContext::getTypeInfo(clang::Type const*) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x88aa150)
#11 0x00005651395a76db clang::ASTContext::getPreferredTypeAlign(clang::Type const*) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x88a96db)
#12 0x00005651395a57f7 clang::ASTContext::getDeclAlign(clang::Decl const*, bool) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x88a77f7)
#13 0x000056513614f754 clang::CodeGen::CodeGenFunction::EmitAutoVarAlloca(clang::VarDecl const&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5451754)
#14 0x000056513614ba2e clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x544da2e)
#15 0x000056513614b4b7 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x544d4b7)
#16 0x00005651360d578b clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x53d778b)
#17 0x00005651360c8aed clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x53caaed)
#18 0x00005651360c7dee clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x53c9dee)
#19 0x00005651360d6b70 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x53d8b70)
#20 0x00005651360b1e72 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x53b3e72)
#21 0x0000565135f6e870 clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5270870)
#22 0x0000565135f66332 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5268332)
#23 0x0000565135f5759f clang::CodeGen::CodeGenModule::EmitDeferred() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x525959f)
#24 0x0000565135f575cb clang::CodeGen::CodeGenModule::EmitDeferred() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x52595cb)
#25 0x0000565135f575cb clang::CodeGen::CodeGenModule::EmitDeferred() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x52595cb)
#26 0x0000565135f575cb clang::CodeGen::CodeGenModule::EmitDeferred() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x52595cb)
#27 0x0000565135f575cb clang::CodeGen::CodeGenModule::EmitDeferred() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x52595cb)
#28 0x0000565135f575cb clang::CodeGen::CodeGenModule::EmitDeferred() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x52595cb)
#29 0x0000565135f543fa clang::CodeGen::CodeGenModule::Release() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x52563fa)
#30 0x00005651365b3928 (anonymous namespace)::CodeGeneratorImpl::HandleTranslationUnit(clang::ASTContext&) ModuleBuilder.cpp:0:0
#31 0x00005651365abb3e clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x58adb3e)
#32 0x000056513859d599 clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x789f599)
#33 0x0000565136a3c64f clang::FrontendAction::Execute() (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5d3e64f)
#34 0x00005651369ae10d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5cb010d)
#35 0x0000565136b25c37 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5e27c37)
#36 0x00005651344b5a5e cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x37b7a5e)
#37 0x00005651344b1ffe ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#38 0x0000565136800249 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::$_0>(long) Job.cpp:0:0
#39 0x0000565135ca87f6 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x4faa7f6)
#40 0x00005651367ff922 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5b01922)
#41 0x00005651367bee1c clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5ac0e1c)
#42 0x00005651367bf127 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5ac1127)
#43 0x00005651367dc818 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x5ade818)
#44 0x00005651344b1641 clang_main(int, char**, llvm::ToolContext const&) (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x37b3641)
#45 0x00005651344c1927 main (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x37c3927)
#46 0x00007fb049e43b8a __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#47 0x00007fb049e43c45 call_init ./csu/../csu/libc-start.c:128:20
#48 0x00007fb049e43c45 __libc_start_main ./csu/../csu/libc-start.c:347:5
#49 0x00005651344afbe1 _start (/usr/local/google/home/ayzhao/src/llvm-project/build/bin/clang+++0x37b1be1)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
clang version 20.0.0git ([email protected]:llvm/llvm-project.git e8b70e97447dc0d93a277b0373345d3a1bae1aa9)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/ayzhao/src/llvm-project/build/bin
Build config: +assertions
clang++: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang++: note: diagnostic msg: ../../tools/clang/crashreports/CacheRequestTests-732518.cpp
clang++: note: diagnostic msg: ../../tools/clang/crashreports/CacheRequestTests-732518.sh
clang++: note: diagnostic msg:

********************
ninja: build stopped: subcommand failed.

mizvekov added a commit that referenced this pull request Nov 8, 2024
…friend declarations without specialization info to meet [dcl.fct.default] p4" (#115404)

Reverts #113777

Reverted due to regression reported here:
#113777 (comment)
@mizvekov
Copy link
Contributor

mizvekov commented Nov 8, 2024

Reverted. The issue looks like the instantiation of the default argument was skipped, but without any previous error actually being produced, so it proceeded to CodeGen with invalid AST.

@alanzhao1
Copy link
Contributor

Reverted. The issue looks like the instantiation of the default argument was skipped, but without any previous error actually being produced, so it proceeded to CodeGen with invalid AST.

Thanks! Still waiting on CReduce for that repro.

@aeubanks
Copy link
Contributor

aeubanks commented Nov 8, 2024

I was also reducing this, got

template <bool, class> using __enable_if_t = int;
template <int __v> struct integral_constant {
  static const int value = __v;
};
struct basic_string {
  template <__enable_if_t<integral_constant<true>::value, int> = 0>
  basic_string(char *);
};
struct CacheRequestImpl {
  template <typename CacheHitFn, typename CacheMissFn>
  friend void LoadOrRun(int, CacheRequestImpl, CacheHitFn, CacheMissFn,
                        basic_string = "") {}
};
void CacheRequestTests_MakesCacheKey_TestTestBody() {
  CacheRequestImpl req;
  auto result = LoadOrRun(
      0, req, [] {}, [] {});
}

@zyn0217
Copy link
Contributor

zyn0217 commented Nov 8, 2024

Further reduced to:

struct CacheRequestImpl {
  friend void LoadOrRun(CacheRequestImpl, int = 42) {}
};
void CacheRequestTests_MakesCacheKey_TestTestBody() {
  CacheRequestImpl req;
  LoadOrRun(req);
}

There is no template in this case, so obviously, FDecl->getMemberSpecializationInfo() would be null, hence the absence of the default arguments with this patch.

As I have suggested initially, we should probably put the checking in Sema::InstantiateDefaultArgument() because that is where the instantiation of default arguments in templates starts.

@mstorsjo
Copy link
Member

mstorsjo commented Nov 8, 2024

Thanks for the revert - FWIW, I also ran into issues with this change when building Qt.

Building the https://github.com/qt/qtbase repo fails with an error like this:

[790/790] Linking CXX shared library lib/libQt6Gui.so.6.8.0
FAILED: lib/libQt6Gui.so.6.8.0 
/usr/bin/ld: src/gui/CMakeFiles/Gui.dir/painting/qpdf.cpp.o: in function `decltype (((std::declval<std::enable_if<((7)>(0)), void>::type>()),(((qHash(std::declval<QByteArray, QByteArray, int, int, int, QList<QFont::Tag>, QList<float> const&>())),...))),unsigned long{}) qHashMulti<QByteArray, QByteArray, int, int, int, QList<QFont::Tag>, QList<float> >(unsigned long, QByteArray const&, QByteArray const&, int const&, int const&, int const&, QList<QFont::Tag> const&, QList<float> const&)':
qpdf.cpp:(.text._Z10qHashMultiIJ10QByteArrayS0_iii5QListIN5QFont3TagEES1_IfEEEDTcmcmclsr3stdE7declvalINSt9enable_ifIXgtsPDpT_ELi0EEvE4typeEEEfrcmcl5qHashclsr3stdE7declvalIRKS7_EEEtlmEEmDpSC_[_Z10qHashMultiIJ10QByteArrayS0_iii5QListIN5QFont3TagEES1_IfEEEDTcmcmclsr3stdE7declvalINSt9enable_ifIXgtsPDpT_ELi0EEvE4typeEEEfrcmcl5qHashclsr3stdE7declvalIRKS7_EEEtlmEEmDpSC_]+0x9a): undefined reference to `unsigned long QtPrivate::QHashCombine::operator()<QFont::Tag>(unsigned long, QFont::Tag const&) const'
/usr/bin/ld: src/gui/CMakeFiles/Gui.dir/text/qfont.cpp.o: in function `decltype (((std::declval<std::enable_if<((13)>(0)), void>::type>()),(((qHash(std::declval<long long, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, QList<QString>, QString, unsigned int, QList<QFont::Tag>, QList<float> const&>())),...))),unsigned long{}) qHashMulti<long long, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, QList<QString>, QString, unsigned int, QList<QFont::Tag>, QList<float> >(unsigned long, long long const&, unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&, unsigned int const&, QList<QString> const&, QString const&, unsigned int const&, QList<QFont::Tag> const&, QList<float> const&)':
qfont.cpp:(.text._Z10qHashMultiIJxjjjjjjj5QListI7QStringES1_jS0_IN5QFont3TagEES0_IfEEEDTcmcmclsr3stdE7declvalINSt9enable_ifIXgtsPDpT_ELi0EEvE4typeEEEfrcmcl5qHashclsr3stdE7declvalIRKS8_EEEtlmEEmDpSD_[_Z10qHashMultiIJxjjjjjjj5QListI7QStringES1_jS0_IN5QFont3TagEES0_IfEEEDTcmcmclsr3stdE7declvalINSt9enable_ifIXgtsPDpT_ELi0EEvE4typeEEEfrcmcl5qHashclsr3stdE7declvalIRKS8_EEEtlmEEmDpSD_]+0x13a): undefined reference to `unsigned long QtPrivate::QHashCombine::operator()<QFont::Tag>(unsigned long, QFont::Tag const&) const'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

In a build with static libraries, where this didn't cause immediate errors, I also ran into build errors while building qtdeclarative. I didn't bisect that failure, but it seems closely related:

qtdeclarative/src/qmlcompiler/qqmljsregistercontent_p.h:250:20: error: no matching function for call to 'qHashMulti'
  250 |             return qHashMulti(
      |                    ^~~~~~~~~~
aarch64-w64-mingw32/include/QtCore/qhashfunctions.h:344:1: note: candidate template ignored: substitution failure [with T = <QQmlJSMetaProperty, int, int>]
  344 | qHashMulti(size_t seed, const T &... args)
      | ^
aarch64-w64-mingw32/include/QtCore/qhashfunctions.h:390:12: error: no matching function for call to 'qHashMulti'
  390 |     return qHashMulti(seed, key.first, key.second);
      |            ^~~~~~~~~~
qtdeclarative/src/qmlcompiler/qqmljsregistercontent_p.h:157:20: note: in instantiation of function template specialization 'qHash<QQmlJSMetaEnum, QString>' requested here
  157 |             return qHash(std::get<std::pair<QQmlJSMetaEnum, QString>>(registerContent.m_content),
      |                    ^
aarch64-w64-mingw32/include/QtCore/qhashfunctions.h:344:1: note: candidate template ignored: substitution failure [with T = <QQmlJSMetaEnum, QString>]
  344 | qHashMulti(size_t seed, const T &... args)
      | ^
4 errors generated.

Before relanding, I would appreciate if you could try building qtbase with the new version of the suggested change!

Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…eclarations (llvm#113777)

This fixes a crash when instantiating default arguments for templated
friend function declarations which lack a definition.
There are implementation limits which prevents us from finding the
pattern for such functions, and this causes difficulties
setting up the instantiation scope for the function parameters.

This patch skips instantiating the default argument in these cases,
which causes a minor regression in error recovery, but otherwise avoids
the crash.

Fixes llvm#113324
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…friend declarations without specialization info to meet [dcl.fct.default] p4" (llvm#115404)

Reverts llvm#113777

Reverted due to regression reported here:
llvm#113777 (comment)
a-tarasyuk added a commit that referenced this pull request Dec 18, 2024
…riend declarations to meet [dcl.fct.default] p4 (#115487)

This fixes a crash when instantiating default arguments for templated
friend function declarations which lack a definition.
There are implementation limits which prevents us from finding the
pattern for such functions, and this causes difficulties
setting up the instantiation scope for the function parameters.

This patch skips instantiating the default argument in these cases,
which causes a minor regression in error recovery, but otherwise avoids
the crash.

The previous attempt #113777 accidentally skipped all default argument
constructions, causing some regressions. This patch resolves that by
moving the guard to InstantiateDefaultArgument() where the handling of
templates takes place.

Fixes #113324
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] clang trunk crashes at clang::FunctionDecl::getNumParams()
7 participants