googletest icon indicating copy to clipboard operation
googletest copied to clipboard

Work around GCC name lookup bug with GCC 6~11 (StreamPrinter::PrintValue)

Open pgroke-dt opened this issue 3 years ago • 0 comments

Does the feature exist in the most recent commit?

No.

Why do we need this feature?

GCC versions up to (including) 11 have a bug where GCC will sometimes find an operator in the global namespace, when doing ADL for a type defined in a namespace: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51577

This currently prevents code like this from working with GTest:

namespace foo {
    struct bar { int x; };

    bool operator == (bar a, bar b) {
        return a.x == b.x;
    }
}

std::ostream& operator <<(std::ostream& os, foo::bar) {
    return os;
}

void fun() {
    EXPECT_EQ(foo::bar{1}, foo::bar{2});
}

https://godbolt.org/z/Ken6ha16z

In this example, the RawBytesPrinter should be selected because the operator << is not applicable for ADL. However GCC <= 11 will select the StreamPrinter but then fail when compiling the actual call of StreamPrinter::PrintValue.

We have a large code base and some of our tests failed to compile because of this. Updating to GCC 12 is unfortunately not possible for us at this time. We currently have a local patch with a workaround for this issue and would like to upstream it if possible.

Describe the proposal

Change StreamPrinter::PrintValue to do the SFINAE check via the return type instead of using a dummy template parameter. For some reason, GCC seems to do the name lookup property there.

Is the feature specific to an operating system, compiler, or build system version?

GCC versions 6 through 11

pgroke-dt avatar Aug 31 '22 14:08 pgroke-dt