Issue 178863
Summary clang accepts conflicting static/extern variable declarations in same translation unit without diagnostic
Labels clang
Assignees
Reporter vrukesh
    **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.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to