Fix #20502 - importc: macro conflicts with struct of same name (#21234)

Fixes https://github.com/dlang/dmd/issues/20502

Similar to VarDeclarations, don't let TemplateDeclarations from
C defines shadow a real symbol.
This commit is contained in:
drpriver 2025-04-15 16:27:33 -07:00 committed by GitHub
parent e5de57e518
commit 5fd0d29211
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 21 additions and 0 deletions

View file

@ -6057,6 +6057,8 @@ final class CParser(AST) : Parser!AST
//printf("addSym() %s\n", s.toChars());
if (auto v = s.isVarDeclaration())
v.isCmacro(true); // mark it as coming from a C #define
if (auto td = s.isTemplateDeclaration())
td.isCmacro = true; // mark as coming from a C #define
/* If it's already defined, replace the earlier
* definition
*/

View file

@ -593,6 +593,7 @@ extern (C++) final class TemplateDeclaration : ScopeDsymbol
bool isTrivialAliasSeq; /// matches pattern `template AliasSeq(T...) { alias AliasSeq = T; }`
bool isTrivialAlias; /// matches pattern `template Alias(T) { alias Alias = qualifiers(T); }`
bool deprecated_; /// this template declaration is deprecated
bool isCmacro; /// Whether this template is a translation of a C macro
Visibility visibility;
// threaded list of previous instantiation attempts on stack

View file

@ -1749,6 +1749,7 @@ public:
bool isTrivialAliasSeq;
bool isTrivialAlias;
bool deprecated_;
bool isCmacro;
Visibility visibility;
TemplatePrevious* previous;
Expression* lastConstraint;

View file

@ -544,6 +544,12 @@ Dsymbol handleSymbolRedeclarations(ref Scope sc, Dsymbol s, Dsymbol s2, ScopeDsy
}
}
// Don't let macros shadow real symbols
if (auto td = s.isTemplateDeclaration())
{
if (td.isCmacro) return s2;
}
auto vd = s.isVarDeclaration(); // new declaration
auto vd2 = s2.isVarDeclaration(); // existing declaration

View file

@ -71,6 +71,7 @@ public:
d_bool isTrivialAliasSeq; // matches `template AliasSeq(T...) { alias AliasSeq = T; }
d_bool isTrivialAlias; // matches pattern `template Alias(T) { alias Alias = qualifiers(T); }`
d_bool deprecated_; // this template declaration is deprecated
d_bool isCmacro; // Whether this template is a translation of a C macro
Visibility visibility;
TemplatePrevious *previous; // threaded list of previous instantiation attempts on stack

View file

@ -0,0 +1,10 @@
// https://github.com/dlang/dmd/issues/20502
struct mg_str {
};
void mg_str_s() {
}
#define mg_str(s) mg_str_s(s)