Skip to content

[clang] Class fields with placeholder names cause incorrect instantiation of in-class initializers #114069

@groundswellaudio

Description

@groundswellaudio

MRE :

https://godbolt.org/z/Mjdnz8G31

#include <cassert>

template <class T>
struct A {
    T _ = 5;
    T _ = 4;
};

int main() {
    A<int> a;
    auto [x, y] = a;
    assert( y == 4 );
}

assert( y == 4 ) fails because Clang finds the in-class init pattern like this (in Sema::BuildCXXDefaultInitExpr) :

CXXRecordDecl *ClassPattern = ParentRD->getTemplateInstantiationPattern();
DeclContext::lookup_result Lookup = ClassPattern->lookup(Field->getDeclName());

FieldDecl *Pattern = nullptr;
for (auto *L : Lookup) {
  if ((Pattern = dyn_cast<FieldDecl>(L)))
    break;
}

Obviously this cannot works with _. I think to remedy this imply keeping track of the pattern of an instantiated field with a placeholder name in some auxiliary structure? The entry would only need to be temporary until its consumed by Sema::BuildCXXDefaultInitExpr.

Also I think a warning should be emitted that we're using a C++26 feature before its time?

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++26clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partydiverges-from:gccDoes the clang frontend diverge from gcc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions