llvm / llvm/llvm-project

[clang] Heap-allocated volatile variables are diagnosed as volatile temporaries

Open
#160,468 1 comment 0 reactions 0 assignees View on GitHub
clang:diagnostics clang:frontend constexpr
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

See https://godbolt.org/z/r1s5ds5v5
```c++
constexpr int foo() {
for (int i = 0; i != 10; ++i) {
auto *k = new volatile int{0};

int *p = (int *)k;
*p = 10;
delete k;
}
return 1;
}
static_assert(foo() == 1);
```

```console
:11:15: error: static assertion expression is not an integral constant expression
11 | static_assert(foo() == 1);
| ^~~~~~~~~~
:6:8: note: assignment to volatile temporary is not allowed in a constant expression
6 | *p = 10;
| ^
:11:15: note: in call to 'foo()'
11 | static_assert(foo() == 1);
| ^~~~~
note: volatile temporary created here
```

It's not a temporary though and the source location for the "created here" note is invalid.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the constexpr example from the issue, using the linked Godbolt case or the shown static_assert. Trace Clang's diagnostic for the assignment through the constant-expression evaluation path; done means the heap-allocated volatile variable is not called a temporary and the creation note points to a valid source location.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.