[Bug,default/29679] New: C++20 compilation failure - ambiguous comparisons

Message ID bug-29679-9487@http.sourceware.org/bugzilla/
State New
Series [Bug,default/29679] New: C++20 compilation failure - ambiguous comparisons |

Commit Message

mliska at suse dot cz Oct. 13, 2022, 8:04 a.m. UTC

            Bug ID: 29679
           Summary: C++20 compilation failure - ambiguous comparisons
           Product: libabigail
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: default
          Assignee: dodji at redhat dot com
          Reporter: gprocida at google dot com
                CC: libabigail at sourceware dot org
  Target Milestone: ---

This was reported internally, along with a proposed fix.

To reproduce, change the C++ standard in configure.ac from 11 and 20,
reconfigure and rebuild.

With GCC:

  CXX      abg-dwarf-reader.lo
../../src/abg-dwarf-reader.cc: In function ?bool
abigail::dwarf_reader::op_is_control_flow(Dwarf_Op*, size_t, size_t, size_t&,
../../src/abg-dwarf-reader.cc:9007:16: error: ambiguous overload for
?operator!=? (operand types are ?abigail::dwarf_reader::expr_result? and ?int?)
 9007 |       if (val1 != 0)
      |           ~~~~ ^~ ~
      |           |       |
      |           |       int
      |           abigail::dwarf_reader::expr_result
../../src/abg-dwarf-reader.cc:1735:3: note: candidate: ?bool
abigail::dwarf_reader::expr_result&) const? (rewritten)
 1735 |   operator==(const expr_result& o) const
      |   ^~~~~~~~
../../src/abg-dwarf-reader.cc:9007:16: note: candidate: ?operator!=(int64_t
{aka long int}, int)? (built-in)
 9007 |       if (val1 != 0)
      |           ~~~~~^~~~

With Clang there are several more errors, but they are explained by:

The fix that was proposed internally was:

And the accompanying explanation was:

1. operator==(const expr_result&, const expr_result&) is ambiguous because
C++20 could call this with args (val1, 0) or with reversed args (0, val1).
This is because of implicit conversion from int to expr_result.
Adding operator!= disables argument reversal and resolves the ambiguity.

2. 0 == val1 is then illegal because the LHS (int) does not provide operator==.
When argument reversal is disabled, the LHS should provide operator==.
(Same for !=).

I'll be honest and say that I haven't followed the details of this.
However, another way to resolve the issue might be to eliminate the
implicit conversion - these tend to be sources of other surprises



--- a/src/abg-dwarf-reader.cc
+++ b/src/abg-dwarf-reader.cc
@@ -1638,6 +1638,10 @@  public:
   {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}

+  operator!=(const expr_result& o) const
+  {return !(*this == o);}
+  bool
   operator>=(const expr_result& o) const
   {return const_value_ >= o.const_value_;}

@@ -8364,7 +8368,7 @@  op_is_control_flow(Dwarf_Op* expr,

     case DW_OP_bra:
       val1 = ctxt.pop();
-      if (val1 != 0)
+      if (0 != val1)
        index += val1.const_value() - 1;