Skip to content

Commit a13bcf3

Browse files
[clang] Do not serialize function definitions without a body (#121550)
An instantiated templated function definition may not have a body due to parsing errors inside the templated function. When serializing, an assert is triggered inside `ASTRecordWriter::AddFunctionDefinition`. The instantiation may happen on an intermediate module. The test case was reduced from `mp-units`.
1 parent 3134045 commit a13bcf3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7230,6 +7230,10 @@ void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
72307230
if (!D->isFromASTFile())
72317231
return; // Declaration not imported from PCH.
72327232

7233+
// The function definition may not have a body due to parsing errors.
7234+
if (!D->doesThisDeclarationHaveABody())
7235+
return;
7236+
72337237
// Implicit function decl from a PCH was defined.
72347238
DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
72357239
}
@@ -7249,6 +7253,10 @@ void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
72497253
if (!D->isFromASTFile())
72507254
return;
72517255

7256+
// The function definition may not have a body due to parsing errors.
7257+
if (!D->doesThisDeclarationHaveABody())
7258+
return;
7259+
72527260
DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
72537261
}
72547262

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
5+
// RUN: %clang_cc1 -std=c++23 mod1.cppm -emit-module-interface -o mod1.pcm -fallow-pcm-with-compiler-errors -verify
6+
// RUN: %clang_cc1 -std=c++23 mod2.cppm -emit-module-interface -o mod2.pcm -fmodule-file=mod1=mod1.pcm -verify -fallow-pcm-with-compiler-errors
7+
// RUN: %clang_cc1 -std=c++23 mod3.cppm -emit-module-interface -o mod3.pcm -fmodule-file=mod1=mod1.pcm -fmodule-file=mod2=mod2.pcm -verify -fallow-pcm-with-compiler-errors
8+
// RUN: %clang_cc1 -std=c++23 main.cpp -fmodule-file=mod1=mod1.pcm -fmodule-file=mod2=mod2.pcm -fmodule-file=mod3=mod3.pcm -verify -fallow-pcm-with-compiler-errors -ast-dump-all
9+
10+
//--- mod1.cppm
11+
export module mod1;
12+
13+
export template <unsigned N, unsigned M>
14+
class A {
15+
public:
16+
constexpr A(const char[], const char[]) {
17+
auto x = BrokenExpr; // expected-error {{use of undeclared identifier 'BrokenExpr'}}
18+
}
19+
};
20+
21+
export template<A<1,1> NTTP>
22+
struct B {};
23+
24+
template < unsigned N, unsigned M >
25+
A(const char (&)[N], const char (&)[M]) -> A< 1, 1 >;
26+
27+
//--- mod2.cppm
28+
export module mod2;
29+
import mod1;
30+
31+
struct C: B <A{"a", "b"}> { // expected-error {{non-type template argument is not a constant expression}}
32+
constexpr C(int a) { }
33+
};
34+
35+
//--- mod3.cppm
36+
// expected-no-diagnostics
37+
export module mod3;
38+
export import mod2;
39+
40+
//--- main.cpp
41+
// expected-no-diagnostics
42+
import mod3; // no crash

0 commit comments

Comments
 (0)