On Thu, Jan 28, 2021 at 10:04:12AM -0500, Jason Merrill wrote: > > We emit a bogus warning on the following testcase, suggesting that the > > operator should return *this even when it does that already. > > The problem is that normally cp_build_indirect_ref_1 ensures that *this > > is folded as current_class_ref, but in templates (if return type is > > non-dependent, otherwise check_return_expr doesn't check it) it didn't > > go through cp_build_indirect_ref_1, but just built another INDIRECT_REF. > > That seems like a bug in build_x_indirect_ref.
So do you want this instead (if it passes bootstrap/regtest)? 2021-01-28 Jakub Jelinek <ja...@redhat.com> PR c++/98841 * typeck.c (build_x_indirect_ref): For *this, return current_class_ref. * g++.dg/warn/effc5.C: New test. --- gcc/cp/typeck.c.jj 2021-01-27 11:48:49.715890458 +0100 +++ gcc/cp/typeck.c 2021-01-28 16:17:18.712755173 +0100 @@ -3326,7 +3326,15 @@ build_x_indirect_ref (location_t loc, tr { /* Retain the type if we know the operand is a pointer. */ if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr))) - return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); + { + if (expr == current_class_ptr + || (TREE_CODE (expr) == NOP_EXPR + && TREE_OPERAND (expr, 0) == current_class_ptr + && (same_type_ignoring_top_level_qualifiers_p + (TREE_TYPE (expr), TREE_TYPE (current_class_ptr))))) + return current_class_ref; + return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr); + } if (type_dependent_expression_p (expr)) return build_min_nt_loc (loc, INDIRECT_REF, expr); expr = build_non_dependent_expr (expr); --- gcc/testsuite/g++.dg/warn/effc5.C.jj 2021-01-28 16:15:05.820256255 +0100 +++ gcc/testsuite/g++.dg/warn/effc5.C 2021-01-28 16:15:05.820256255 +0100 @@ -0,0 +1,17 @@ +// PR c++/98841 +// { dg-do compile } +// { dg-options "-Weffc++" } + +struct S { + template <typename T> + S& operator=(const T&) { return *this; } // { dg-bogus "should return a reference to" } + S& operator=(const S&) { return *this; } +}; + +void +foo () +{ + S s, t; + s = 1; + s = t; +} Jakub