llvm / llvm/llvm-project

Missing diagnostic for use of non-literal type as argument to undefined constructor

Open
#224,116 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

When visiting a `CXXConstructExpr` during constant evaluation, we [never visit argument expressions](https://github.com/llvm/llvm-project/blob/c9c1ed7e41b245d15ee339b51696c546d5dc6d18/clang/lib/AST/ExprConstant.cpp#L11746-L11747) if the constructor itself is not suitable for constant evaluation. Consequently (when compiling with `--std=c++20` or earlier), we fail to diagnose non-literal types that appear in argument expressions for calls to such constructors ([Godbolt](https://godbolt.org/z/qeqGsreTf)).

```cpp
struct NonLiteral {
NonLiteral() {}
};

struct Swallow {
template
constexpr Swallow(T);
};

constexpr int g() { Swallow s(NonLiteral{}); return 42; }

template
constexpr Swallow::Swallow(T) {}

int main() { return g(); }
```

Contributor guide

Open the contributing guide

Research direction

Start in clang/lib/AST/ExprConstant.cpp at the CXXConstructExpr handling linked in the issue, then run the supplied C++20 reproducer. Trace how argument expressions are visited when the constructor is not suitable for constant evaluation. Done means the non-literal argument is diagnosed under C++20 and earlier without regressing valid constant evaluation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.