llvm / llvm/llvm-project

clang accepts conflicting static/extern variable declarations in same translation unit without diagnostic

Open
#178,863 2 comments 0 reactions 0 assignees View on GitHub
clang:frontend
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

**What code / commands /steps will reproduce the problem?**
Compile below sample code for C++17.
Godbolt compile explorer: https://godbolt.org/z/5YKqshsdf

`
static int k = 3;

extern int foo(int);

int main() {
return foo(k);
}

extern int k;

int foo(int p) {
return p == k;
}

`

**What is the expected result?**
Per C++17 this must be a compile-time error (not just a warning)

**What happens instead?**
Compilation is successful, without any warning/error.

As per the standard:
A type without linkage shall not be used as the type of a variable or function with external linkage unless
-the entity has C language linkage, or
- the entity is declared within an unnamed namespace, or
- the entity is not odr-used or is defined in the same translation unit.

Translation unit declares `k` with internal linkage (`static int k = 3;`) and then redeclares the same name with external linkage (`extern int k;`). That mix of linkage is ill-formed in C++17. If an entity is declared with internal linkage, then any redeclaration in the same translation unit must also give it internal linkage; giving it external linkage instead makes the program ill‑formed. The above `static` then `extern` redeclarations violate that rule.

Accepting the code without diagnostic is non‑conforming.

Contributor guide

Open the contributing guide

Research direction

Start by compiling the C++17 reproducer from the issue, also available through the linked Godbolt example, and confirm that no diagnostic is emitted. Trace the compiler's handling of the static and extern declarations in the same translation unit; done means the sample produces a compile-time diagnostic for the conflicting linkage.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.