Skip to content

Commit 0752999

Browse files
committed
[Sema] Merge variable template specializations
Clang used to produce redefinition errors, see tests for examples. Reviewed By: ChuanqiXu Differential Revision: https://reviews.llvm.org/D131258
1 parent 6db15a8 commit 0752999

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

clang/lib/Sema/SemaDecl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4739,6 +4739,7 @@ bool Sema::checkVarDeclRedefinition(VarDecl *Old, VarDecl *New) {
47394739
if (!hasVisibleDefinition(Old) &&
47404740
(New->getFormalLinkage() == InternalLinkage ||
47414741
New->isInline() ||
4742+
isa<VarTemplateSpecializationDecl>(New) ||
47424743
New->getDescribedVarTemplate() ||
47434744
New->getNumTemplateParameterLists() ||
47444745
New->getDeclContext()->isDependentContext())) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
// RUN: split-file %s %t
4+
//
5+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/var_def.cppm -o %t/var_def.pcm
6+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface -fprebuilt-module-path=%t %t/reexport1.cppm -o %t/reexport1.pcm
7+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface -fprebuilt-module-path=%t %t/reexport2.cppm -o %t/reexport2.pcm
8+
// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %t/use.cppm -fsyntax-only -verify
9+
10+
//--- use.cppm
11+
import reexport1;
12+
import reexport2;
13+
14+
auto foo = zero<Int>;
15+
auto bar = zero<int*>;
16+
auto baz = zero<int>;
17+
18+
template <class T> constexpr T zero = 0; // expected-error-re {{declaration{{.*}}in the global module follows declaration in module var_def}}
19+
// expected-note@* {{previous}}
20+
template <> constexpr Int zero<Int> = {0}; // expected-error-re {{declaration{{.*}}in the global module follows declaration in module var_def}}
21+
// expected-note@* {{previous}}
22+
template <class T> constexpr T* zero<T*> = nullptr; // expected-error-re {{declaration{{.*}}in the global module follows declaration in module var_def}}
23+
// expected-note@* {{previous}}
24+
25+
template <> constexpr int** zero<int**> = nullptr; // ok, new specialization.
26+
template <class T> constexpr T** zero<T**> = nullptr; // ok, new partial specilization.
27+
28+
//--- var_def.cppm
29+
export module var_def;
30+
31+
export template <class T> constexpr T zero = 0;
32+
export struct Int {
33+
int value;
34+
};
35+
export template <> constexpr Int zero<Int> = {0};
36+
export template <class T> constexpr T* zero<T*> = nullptr;
37+
38+
//--- reexport1.cppm
39+
export module reexport1;
40+
export import var_def;
41+
42+
//--- reexport2.cppm
43+
export module reexport2;
44+
export import var_def;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir %t
3+
// RUN: split-file %s %t
4+
//
5+
// We need '-fmodules-local-submodule-visibility' to properly test merging when building a module from multiple
6+
// headers inside the same TU. C++20 mode would imply this flag, but we need it to set it explicitly for C++14.
7+
//
8+
// RUN: %clang_cc1 -xc++ -std=c++14 -fmodules -fmodules-local-submodule-visibility -fmodule-name=library \
9+
// RUN: -emit-module %t/modules.map \
10+
// RUN: -o %t/module.pcm
11+
//
12+
//
13+
// RUN: %clang_cc1 -xc++ -std=c++14 -fmodules -fmodules-local-submodule-visibility -fmodule-file=%t/module.pcm \
14+
// RUN: -fmodule-map-file=%t/modules.map \
15+
// RUN: -fsyntax-only -verify %t/use.cpp
16+
//
17+
//--- use.cpp
18+
19+
#include "var1.h"
20+
#include "var2.h"
21+
22+
auto foo = zero<Int>;
23+
auto bar = zero<int*>;
24+
auto baz = zero<int>;
25+
26+
template <class T> constexpr T zero = 0; // expected-error {{redefinition}} expected-note@* {{previous}}
27+
template <> constexpr Int zero<Int> = {0}; // expected-error {{redefinition}} expected-note@* {{previous}}
28+
template <class T> constexpr T* zero<T*> = nullptr; // expected-error {{redefinition}} expected-note@* {{previous}}
29+
30+
template <> constexpr int** zero<int**> = nullptr; // ok, new specialization.
31+
template <class T> constexpr T** zero<T**> = nullptr; // ok, new partial specilization.
32+
33+
//--- modules.map
34+
module "library" {
35+
export *
36+
module "var1" {
37+
export *
38+
header "var1.h"
39+
}
40+
module "var2" {
41+
export *
42+
header "var2.h"
43+
}
44+
}
45+
46+
//--- var1.h
47+
#ifndef VAR1_H
48+
#define VAR1_H
49+
50+
template <class T> constexpr T zero = 0;
51+
struct Int {
52+
int value;
53+
};
54+
template <> constexpr int zero<Int> = {0};
55+
template <class T> constexpr T* zero<T*> = nullptr;
56+
57+
#endif // VAR1_H
58+
59+
//--- var2.h
60+
#ifndef VAR2_H
61+
#define VAR2_H
62+
63+
template <class T> constexpr T zero = 0;
64+
struct Int {
65+
int value;
66+
};
67+
template <> constexpr int zero<Int> = {0};
68+
template <class T> constexpr T* zero<T*> = nullptr;
69+
70+
#endif // VAR2_H

0 commit comments

Comments
 (0)