This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2] c++: Fix ICE with template codes in check_narrowing [PR91465]


On 2/11/20 5:06 PM, Marek Polacek wrote:
On Sun, Feb 09, 2020 at 01:51:13PM +0100, Jason Merrill wrote:
On 2/6/20 7:30 PM, Marek Polacek wrote:
In ed4f2c001a883b2456fc607a33f1c59f9c4ee65d I changed the call to
fold_non_dependent_expr in check_narrowing to maybe_constant_value.
That was the wrong thing to do as these tests show: check_narrowing
bails out for dependent expressions but we can still have template
codes like CAST_EXPR that don't have anything dependent in it so are
considered non-dependent.  But cxx_eval_* don't grok template codes,
so we need to call fold_non_dependent_expr instead which knows what
to do with template codes.  (I fully accept a "told you so".)

I'm passing tf_none to it, otherwise we'd emit a bogus error for
constexpr-ex4.C: there INIT is "A::operator int(&a)" and while
instantiating this CALL_EXPR (in a template) we call finish_call_expr
and that sees a BASELINK and so emits a new dummy object for 'this',
and then we complain about the wrong number of arguments, because now
we basically have two 'this's.  Which is exactly the problem I saw
recently in c++/92948.

Yeah, the problem continues to be that build_converted_constant_expr is
breaking the boundary between template and non-template codes:
convert_like_real produces trees that aren't suitable for later
substitution, so substituting them breaks.  Perhaps if we're looking at a
non-dependent constant expression in a template,
build_converted_constant_expr should instantiate_non_dependent_expr, pass
the result to convert_like, and then if successful throw away the result in
favor of an IMPLICIT_CONV_EXPR.

That seems to work (if I adjust two spots to handle an I_C_E).  So something
like this?  I don't like that we create an I_C_E in convert_nontype_argument
and in build_converted_constant_expr too, but both are important.

Hmm, the one in convert_nontype_argument shouldn't be needed.

I see the pattern in e.g. build_explicit_specifier is

  expr = instantiate_non_dependent_expr_sfinae (expr, complain);
  /* Don't let convert_like_real create more template codes.  */
  processing_template_decl_sentinel s;
  expr = build_converted_constant_bool_expr (expr, complain);
  expr = cxx_constant_value (expr);

That has always seemed unpleasantly complicated.

+      /* convert_like produces trees that aren't suitable for
+	 substitution, so use an IMPLICIT_CONV_EXPR.  */
+      if (processing_template_decl
+	  && is_nondependent_constant_expression (expr))
+	{
+	  tree e = instantiate_non_dependent_expr (expr);
+	  e = convert_like (conv, e, complain);
+	  if (e != error_mark_node)
+	    return build1 (IMPLICIT_CONV_EXPR, type, expr);
+	}

Shouldn't the above be after setting the check_narrowing flags? We could simplify the pattern above by handling all of it here: instantiate, set the sentinel, convert, maybe_constant_value, return either the constant result or an IMPLICIT_CONV_EXPR.

        conv->check_narrowing = true;
        conv->check_narrowing_const_only = true;
        expr = convert_like (conv, expr, complain);
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 31a556a0a1f..5eb91007e9e 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -10281,9 +10281,8 @@ compute_array_index_type_loc (location_t name_loc, tree name, tree size,
  	  /* Pedantically a constant expression is required here and so
  	     __builtin_is_constant_evaluated () should fold to true if it
  	     is successfully folded into a constant.  */
-	  size = maybe_constant_value (size, NULL_TREE,
-				       /*manifestly_const_eval=*/true);
-
+	  size = fold_non_dependent_expr (size, complain,
+					  /*manifestly_const_eval=*/true);

With the change I suggest above we would drop both this call and the earlier instantiate_ call. In general it's wrong to call *_non_dependent_expr twice on the same expression.

  	  if (!TREE_CONSTANT (size))
  	    size = origsize;
  	}
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index c2d3a98b1c5..5d207da2f5b 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -7099,6 +7099,13 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
  	    /* Make sure we return NULL_TREE only if we have really issued
  	       an error, as described above.  */
  	    return (complain & tf_error) ? NULL_TREE : error_mark_node;
+	  /* Don't pass any IMPLICIT_CONV_EXPRs to maybe_constant_value
+	     because that can't handle it.  */
+	  else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
+	    {
+	      IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
+	      return expr;
+	    }
  	  expr = maybe_constant_value (expr, NULL_TREE,
  				       /*manifestly_const_eval=*/true);

And this whole if block should also be able to reduce to

          expr = build_converted_constant_expr (type, expr, complain);
          expr = convert_from_reference (expr);


  	  expr = convert_from_reference (expr);
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 48920894b3b..59998d38c04 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -981,7 +981,9 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain,
        return ok;
      }
- init = maybe_constant_value (init);
+  init = fold_non_dependent_expr (init, complain);
+  if (init == error_mark_node)
+    return ok;

And this change should not be needed.

    /* If we were asked to only check constants, return early.  */
    if (const_only && !TREE_CONSTANT (init))
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr91465.C b/gcc/testsuite/g++.dg/cpp0x/pr91465.C
new file mode 100644
index 00000000000..e2021aa13e1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/pr91465.C
@@ -0,0 +1,16 @@
+// PR c++/91465 - ICE with template codes in check_narrowing.
+// { dg-do compile { target c++11 } }
+
+enum class D { X };
+enum class S { Z };
+
+D foo(S) { return D{}; }
+D foo(double) { return D{}; }
+
+template <typename>
+struct Bar {
+  D baz(S s)
+  {
+    return D{foo(s)};
+  }
+};
diff --git a/gcc/testsuite/g++.dg/cpp1z/pr91465.C b/gcc/testsuite/g++.dg/cpp1z/pr91465.C
new file mode 100644
index 00000000000..5b1205349d0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/pr91465.C
@@ -0,0 +1,10 @@
+// PR c++/91465 - ICE with template codes in check_narrowing.
+// { dg-do compile { target c++17 } }
+
+enum class E { Z };
+
+template <typename F>
+void foo(F)
+{
+  E{char(0)};
+}

base-commit: f348846e25573bc1f62f5a26317c331ad8dce041



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]