diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -3781,9 +3781,13 @@ // If the declaration declares a template, it may inherit default arguments // from the previous declaration. - if (auto *TD = dyn_cast(D)) - inheritDefaultTemplateArguments(Reader.getContext(), - cast(Previous), TD); + if (auto *TD = dyn_cast(D)) { + // CXXDeductionGuideDecls reference the class template parameters so we need + // to make sure not to call this twice on the same template parameters. + if (!isa(TD->getTemplatedDecl())) + inheritDefaultTemplateArguments(Reader.getContext(), + cast(Previous), TD); + } // If any of the declaration in the chain contains an Inheritable attribute, // it needs to be added to all the declarations in the redeclarable chain. diff --git a/clang/test/Modules/Inputs/ctad/a.h b/clang/test/Modules/Inputs/ctad/a.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/a.h @@ -0,0 +1,7 @@ +#pragma once + +template struct A { + A(T) {} +}; + +A _a(2); diff --git a/clang/test/Modules/Inputs/ctad/b.h b/clang/test/Modules/Inputs/ctad/b.h new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/b.h @@ -0,0 +1,7 @@ +#pragma once + +template struct A { + A(T) {} +}; + +A _b(5); diff --git a/clang/test/Modules/Inputs/ctad/module.modulemap b/clang/test/Modules/Inputs/ctad/module.modulemap new file mode 100644 --- /dev/null +++ b/clang/test/Modules/Inputs/ctad/module.modulemap @@ -0,0 +1,2 @@ +module a { header "a.h" export * } +module b { header "b.h" export * } diff --git a/clang/test/Modules/ctad.cpp b/clang/test/Modules/ctad.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Modules/ctad.cpp @@ -0,0 +1,12 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -x c++ -emit-module %S/Inputs/ctad/module.modulemap -fmodule-name=a -o %t/a.pcm -std=c++17 +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -x c++ -emit-module %S/Inputs/ctad/module.modulemap -fmodule-name=b -o %t/b.pcm -std=c++17 +// RUN: %clang_cc1 -fmodules -fno-implicit-modules -x c++ -emit-llvm -I%S/Inputs/ctad -o - %s -fmodule-file=%t/a.pcm -fmodule-file=%t/b.pcm -std=c++17 | FileCheck %s + +// CHECK: @_a = global +// CHECK: @_b = global +// CHECK: call void @__cxx_global_var_init() +// CHECK: call void @__cxx_global_var_init.1() + +#include "a.h" +#include "b.h"