diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 315f706de73..9322e087345 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -3326,7 +3326,15 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring, { /* 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); diff --git a/gcc/testsuite/g++.dg/warn/effc5.C b/gcc/testsuite/g++.dg/warn/effc5.C new file mode 100644 index 00000000000..43fdd095216 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/effc5.C @@ -0,0 +1,17 @@ +// PR c++/98841 +// { dg-do compile } +// { dg-options "-Weffc++" } + +struct S { + template + 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; +}