llvm / llvm/llvm-project

[clang][PCH] Incorrect template specialization from precompiled header gets used in source file

Open
#180,527 1 comment 0 reactions 0 assignees View on GitHub
clang:modules clang:PCH
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When template member function is specialized inside the template class scope and this class definition is in the precompiled header then only first member function template specialization can be used. To reproduce:
```
# build.sh - build script, define LLVM and CLANG_VER variables before use
$LLVM/bin/clang -cc1 -internal-isystem $LLVM/lib/clang/$CLANG_VER/include -nostdsysteminc \
-internal-isystem $LLVM/../clang/test/Headers/Inputs/include -fmodules -x c++-header -emit-pch -o stdafx.pch stdafx.h
$LLVM/bin/clang -cc1 -internal-isystem $LLVM/lib/clang/$CLANG_VER/include -nostdsysteminc -fmodules \
-x c++ -include-pch stdafx.pch main.cpp
```
```
// stdafx.h - precompiled header
#ifndef STDAFX_H
#define STDAFX_H

template
class Test {
public:

template constexpr int get(T t) const;
template <> constexpr int get<0>(T t) const { return t + 10; }
template <> constexpr int get<1>(T t) const { return t + 11; }
};

#endif
```
```
// main.cpp - source file
#include "stdafx.h"

constexpr Test t;
static_assert(t.get<0>(10) == 20);
static_assert(t.get<1>(10) == 21); // This fails

int main(int argc, char **argv) {
return 0;
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction files stdafx.h and main.cpp, then run the provided clang -cc1 PCH build commands from build.sh. Investigate how the two explicit member-template specializations are recorded and retrieved after PCH loading. Done means both static_assert expressions pass when compiling main.cpp.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.