https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123728

            Bug ID: 123728
           Summary: Wsubobject-linkage warns despite no anonymous
                    namespaces at all in a header
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luto at kernel dot org
  Target Milestone: ---

I reduced a really old warning from a test case using gtest and anonymous
namespaces.  Here's the reduced version:

--- file.cpp ---

#include "header.h"

namespace { struct LocalEnum {}; }

struct Test {
  typedef LocalEnum ParamType;
  static void AddToRegistry() {
    TestMetaFactory<Test> factory;
  }
};

--- header.h ---

template <class TestClass>
struct ParameterizedTestFactory {
  typedef typename TestClass::ParamType ParamType;
  explicit ParameterizedTestFactory(ParamType parameter) :
      parameter_(parameter) {}
  const ParamType parameter_;
};

template <class TestCase>
struct TestMetaFactory {
  virtual void CreateTestFactory(typename TestCase::ParamType parameter) {
    ParameterizedTestFactory<TestCase> f(parameter);
  }
};

--- end ---

$ g++ -c -Wsubobject-linkage file.cpp
In file included from file.cpp:1:
header.h: In instantiation of 'struct ParameterizedTestFactory<Test>':
header.h:12:40:   required from 'void
TestMetaFactory<TestCase>::CreateTestFactory(typename TestCase::ParamType)
[with TestCase = Test; typename TestCase::ParamType = {anonymous}::LocalEnum]'
header.h:11:16:   required from here
header.h:2:8: warning: 'ParameterizedTestFactory<Test>' has a field 'const
ParameterizedTestFactory<Test>::ParamType
ParameterizedTestFactory<Test>::parameter_' whose type uses the anonymous
namespace [-Wsubobject-linkage]
    2 | struct ParameterizedTestFactory {
      |        ^~~~~~~~~~~~~~~~~~~~~~~~

This warning predates C++11, I believe, but I can trigger it just fine on gcc
trunk using c++23 on Compiler Explorer.

It's hard for me to imagine that there's any possibility of an ODR violation
here.  The header file contains only templates.  The offending instance of the
template is parametrized on a type that isn't in a header (and in the Compiler
Explorer version is in the actual main C++ file), and that template in turn has
a typedef to the offending internal linkage type.  But there is no way to refer
to this instance of the template outside the C++ file.

The virtual function seems to be an important -- removing the keyword virtual
from the header silences the warning.  But even if one could someone call that
virtual function without naming TestMetaFactory<Test> (which is impossible in
this example but would be possible if it had a non-template base class), there
should still not be an ODR violation, as can't be another definition of
TestMetaFactory<Test>.


Here it is, approximately, on Compiler Explorer.  I'm not a Compiler Explorer
expert and this is the first time I've every tried putting more than one file
into it.

https://godbolt.org/z/7z8xcMMGP

https://godbolt.org/#z:OYLghAFBqd5QCxAFwE4FN0BoCWIIDGAtgIYDW6AgqsAM4gDkAtACIDCAspQNICiA%2BgCEAqgEkAMi34AVAJoAFXgFIAzCwBK6ADYB1HMgQt0AI1EA7AGYB7BlgJWiABxxb0qcSTPAAriWDpRABMQAiUAJkFwwTtvWmQHAHlvZEdkgDEXdDMSInQUBHR%2BdAAPdAJkkmNXLAtM%2Blh0QP0rVCCQADYazLaARgAWLtds3JBSHDMAOgJHRyxQBlscWnMCLW9AxsYABkXaDhJxgGUrb1QCPIYe3YTHLO3bLU9gNoWASiwIRubW4J7BgN%2BAFZ/sM8pxyOhxEtkLQJshisg5oxHKgrAArMrICC0BAkW60V5KLaUImUYgQ/hEcY4IjeIj8DAAR28OAwgQgADVeOpDqIEgA5ADUKgmgMJxNJJECgSKpXKyEqrmxuPxzJIGFJgq1gpxePQtAA9ARWat0FMZprtbr8QaMAQFV5XObHJatWNJtNHOKScSFTR0Mh%2BFpxmQgzhjKh1Th9cq9bQ1RridrBRYiMhAt7dis1htggwdrg9gczMdTud7oWbnd8w8nm1yRR3p8mvEfiABrVXG0wlsQTk8gUpW4JggkQtC9n1psa4X9kcTmcLldK7czBXHl4XgxXu8luCKPJURj7RW0SA/mZ0AB3DJd4JhHrvLSMQG2NczqyMaQYdCC8IqZdkHuHcsDIEAe1FPoAHYAE4%2BkBHtAWguCkKwZ8GD6N8K0/cd6F7ICaybGAoGIiAkHsDZeBbFpcHwTssn7UZi2dGorDMZBDgIEhXHPDs2OQYRaHQeRilPDo0KeEIoiiLAhNce0cDY/AskCNgrDWIg1xUXsVKhS9%2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hnJvRBWj%2BKDj5YPASfQ51KIoVCY6x9j7Hvn36MApQFuDSHAQ9FFICCnlOqcU86OhOteOyUE8CytsCsEMUIS2EhWC8FEK08YGEAl1cSWKeR5ZvFAuGdvqZ8ToNlklJ9CAA%3D

(Interestingly, GPT-5.2 Codex was unable to reduce this test case.  creduce
successfully reduced it to a variant that seemed to have a legitimate
subobject-linkage warning.  I ended up reducing it by hand.)

Reply via email to