Hi,

this PR is about the missing -Wnarrowing warning for:

struct X
{
  constexpr operator int() { return __INT_MAX__; }
};

signed char c { X{} };

The core issue seems pretty simple: check_narrowing, called by check_initializer, cannot handle something as complex as a class type with a type conversion operator (essentially it can only handle scalars) and gives up. Thus, it seems to me that one way or the other we have to help it. A simple, hackish, patch like the attached works on the testcase and passes the C++ testsuite. Is it far from what we want to do here?

Thanks!
Paolo.

////////////////////
Index: decl.c
===================================================================
--- decl.c      (revision 187733)
+++ decl.c      (working copy)
@@ -5557,9 +5557,11 @@ check_initializer (tree decl, tree init, int flags
            }
          else
            {
+             tree tinit = init;
              init = reshape_init (type, init, tf_warning_or_error);
              if (SCALAR_TYPE_P (type))
-               check_narrowing (type, init);
+               check_narrowing (type, perform_implicit_conversion
+                                (type, tinit, tf_none));
            }
        }
 

Reply via email to