llvm / llvm/llvm-project

clang incorrectly emits -Wundefined-func-template warning in explicit block

Open
#175,081 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Reproduction: https://godbolt.org/z/31YW38oT3

```cpp
struct NotDefaultable {
template
[[gnu::error("Members must be explicitly initialized.")]]
operator T() const;
};
struct M {
int x = NotDefaultable();
};

struct S {
explicit(requires(void (&f)(M)) { f({}); }) S() = default;
};

int main() { S _; }
```

```
$ clang++ -Werror -Wundefined-func-template -std=c++23
:7:11: error: instantiation of function 'NotDefaultable::operator int' required here, but no definition is available [-Werror,-Wundefined-func-template]
7 | int x = NotDefaultable();
| ^
:4:3: note: forward declaration of template entity is here
4 | operator T() const;
| ^
:7:11: note: add an explicit instantiation declaration to suppress this warning if 'NotDefaultable::operator int' is explicitly instantiated in another translation unit
7 | int x = NotDefaultable();
| ^
1 error generated.
```

With Clang 20.1.0 and trunk as of this writing.

The only call to the default constructor of M is in the `explicit` specifier of S (the body is the same as `__is_implicitly_default_constructible_v`). The `[[gnu::error(...)]]` is just there for motivation, it's not relevant to the error.

AFAIK, the `explicit` clause doesn't evaluate code, so it shouldn't count as an ODR-use (and the compiler is otherwise happy to compile the code). It would make sense that it also doesn't trigger the `-Wundefined-func-template` warning.

Related bug: https://github.com/llvm/llvm-project/issues/92486 with similar issues around the warning being too eager with virtual tables.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the provided C++23 Godbolt reproduction with clang++ -Werror -Wundefined-func-template and inspect the diagnostic for the explicit specifier. Done means this reproduction no longer emits the warning in the non-evaluated context described by the report.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.