c++: wrong ambiguity in accessing static field [PR112744]
commit725c68c54c265fe7f6fc7babff7139f3161bdfa6
authorMarek Polacek <polacek@redhat.com>
Tue, 28 Nov 2023 19:44:24 +0000 (28 14:44 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 30 Nov 2023 21:40:57 +0000 (30 16:40 -0500)
treeb0c3d7d83da08349379e61d79a77fad9c10b0283
parente81e84e02be2f9d6d87c41a512539214cb1a8bdc
c++: wrong ambiguity in accessing static field [PR112744]

Given

  struct A { constexpr static int a = 0; };
  struct B : A {};
  struct C : A {};
  struct D : B, C {};

we give the "'A' is an ambiguous base of 'D'" error for

  D{}.A::a;

which seems wrong: 'a' is a static data member so there is only one copy
so it can be unambiguously referred to even if there are multiple A
objects.  clang++/MSVC/icx agree.

This patch uses ba_any: [class.access.base] requires conversion to a unique
base subobject for non-static data members, but it does not require that the
base be unique or accessible for static data members.

PR c++/112744

gcc/cp/ChangeLog:

* typeck.cc (finish_class_member_access_expr): When accessing
a static data member, use ba_any for lookup_base.

gcc/testsuite/ChangeLog:

* g++.dg/lookup/scoped11.C: New test.
* g++.dg/lookup/scoped12.C: New test.
* g++.dg/lookup/scoped13.C: New test.
* g++.dg/lookup/scoped14.C: New test.
* g++.dg/lookup/scoped15.C: New test.
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/lookup/scoped11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/lookup/scoped12.C [new file with mode: 0644]
gcc/testsuite/g++.dg/lookup/scoped13.C [new file with mode: 0644]
gcc/testsuite/g++.dg/lookup/scoped14.C [new file with mode: 0644]
gcc/testsuite/g++.dg/lookup/scoped15.C [new file with mode: 0644]