+2020-03-09 Marek Polacek <polacek@redhat.com>
+
+ PR c++/92031 - bogus taking address of rvalue error.
+ PR c++/91465 - ICE with template codes in check_narrowing.
+ PR c++/93870 - wrong error when converting template non-type arg.
+ PR c++/94068 - ICE with template codes in check_narrowing.
+ * call.c (convert_like_real): Return IMPLICIT_CONV_EXPR
+ in a template when not ck_identity and we're dealing with a class.
+ (convert_like_real) <case ck_ref_bind>: Return IMPLICIT_CONV_EXPR
+ in a template if we need a temporary.
+ * decl.c (compute_array_index_type_loc): Remove
+ instantiate_non_dependent_expr_sfinae call. Call
+ fold_non_dependent_expr instead of maybe_constant_value.
+ (build_explicit_specifier): Don't instantiate or create a sentinel
+ before converting the expression.
+ * except.c (build_noexcept_spec): Likewise.
+ * pt.c (convert_nontype_argument): Don't build IMPLICIT_CONV_EXPR.
+ Set IMPLICIT_CONV_EXPR_NONTYPE_ARG if that's what
+ build_converted_constant_expr returned.
+ * typeck2.c (check_narrowing): Call fold_non_dependent_expr instead
+ of maybe_constant_value.
+
2020-03-09 Jakub Jelinek <jakub@redhat.com>
PR c++/94067
if (issue_conversion_warnings && (complain & tf_warning))
conversion_null_warnings (totype, expr, fn, argnum);
+ /* Creating &TARGET_EXPR<> in a template breaks when substituting,
+ and creating a CALL_EXPR in a template breaks in finish_call_expr
+ so use an IMPLICIT_CONV_EXPR for this conversion. We would have
+ created such codes e.g. when calling a user-defined conversion
+ function. */
+ if (processing_template_decl
+ && convs->kind != ck_identity
+ && (CLASS_TYPE_P (totype) || CLASS_TYPE_P (TREE_TYPE (expr))))
+ return build1 (IMPLICIT_CONV_EXPR, totype, expr);
+
switch (convs->kind)
{
case ck_user:
expr = convert_bitfield_to_declared_type (expr);
expr = fold_convert (type, expr);
}
+
+ /* Creating &TARGET_EXPR<> in a template would break when
+ tsubsting the expression, so use an IMPLICIT_CONV_EXPR
+ instead. This can happen even when there's no class
+ involved, e.g., when converting an integer to a reference
+ type. */
+ if (processing_template_decl)
+ return build1 (IMPLICIT_CONV_EXPR, totype, expr);
expr = build_target_expr_with_type (expr, type, complain);
}
NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
else
{
- size = instantiate_non_dependent_expr_sfinae (size, complain);
size = build_converted_constant_expr (size_type_node, size, complain);
/* 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);
if (!TREE_CONSTANT (size))
size = origsize;
/* Wait for instantiation, tsubst_function_decl will handle it. */
return expr;
- 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 = instantiate_non_dependent_expr_sfinae (expr, complain);
expr = cxx_constant_value (expr);
return expr;
}
if (TREE_CODE (expr) != DEFERRED_NOEXCEPT
&& !value_dependent_expression_p (expr))
{
- 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 = instantiate_non_dependent_expr_sfinae (expr, complain);
expr = cxx_constant_value (expr);
}
if (TREE_CODE (expr) == INTEGER_CST)
else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
|| cxx_dialect >= cxx17)
{
- /* Calling build_converted_constant_expr might create a call to
- a conversion function with a value-dependent argument, which
- could invoke taking the address of a temporary representing
- the result of the conversion. */
- if (!same_type_ignoring_top_level_qualifiers_p (type, expr_type)
- && ((COMPOUND_LITERAL_P (expr)
- && CONSTRUCTOR_IS_DEPENDENT (expr)
- && MAYBE_CLASS_TYPE_P (expr_type)
- && TYPE_HAS_CONVERSION (expr_type))
- /* Similarly, converting e.g. an integer to a class
- involves a constructor call. convert_like would
- create a TARGET_EXPR, but in a template we can't
- use AGGR_INIT_EXPR, and the TARGET_EXPR would lead
- to a bogus error. */
- || (val_dep_p && MAYBE_CLASS_TYPE_P (type))))
- {
- expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
- IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
- return expr;
- }
/* C++17: A template-argument for a non-type template-parameter shall
be a converted constant expression (8.20) of the type of the
template-parameter. */
/* 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;
+ 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);
expr = convert_from_reference (expr);
return ok;
}
- init = maybe_constant_value (init);
+ /* Even non-dependent expressions can still have template
+ codes like CAST_EXPR, so use *_non_dependent_expr to cope. */
+ init = fold_non_dependent_expr (init, complain);
+ if (init == error_mark_node)
+ return ok;
/* If we were asked to only check constants, return early. */
if (const_only && !TREE_CONSTANT (init))
+2020-03-09 Marek Polacek <polacek@redhat.com>
+
+ PR c++/92031 - bogus taking address of rvalue error.
+ PR c++/91465 - ICE with template codes in check_narrowing.
+ PR c++/93870 - wrong error when converting template non-type arg.
+ PR c++/94068 - ICE with template codes in check_narrowing.
+ * g++.dg/cpp0x/conv-tmpl2.C: New test.
+ * g++.dg/cpp0x/conv-tmpl3.C: New test.
+ * g++.dg/cpp0x/conv-tmpl4.C: New test.
+ * g++.dg/cpp0x/conv-tmpl5.C: New test.
+ * g++.dg/cpp0x/conv-tmpl6.C: New test.
+ * g++.dg/cpp1z/conv-tmpl1.C: New test.
+
2020-03-09 Jakub Jelinek <jakub@redhat.com>
PR c++/94067
--- /dev/null
+// PR c++/92031 - bogus taking address of rvalue error.
+// { dg-do compile { target c++11 } }
+
+struct x { const int& l; };
+
+void a(const x&) {}
+
+template<class E>
+void f() {
+ a(x { 0 });
+}
+
+void g() {
+ a(x { 0 });
+}
+
+void
+test ()
+{
+ f<int>();
+}
--- /dev/null
+// 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)};
+ }
+};
--- /dev/null
+// PR c++/93870 - wrong error when converting template non-type arg.
+// { dg-do compile { target c++11 } }
+
+template <typename ENUM> struct EnumWrapper
+{
+ ENUM value;
+
+ constexpr operator ENUM() const
+ {
+ return value;
+ }
+};
+
+enum E : int { V };
+
+constexpr EnumWrapper<E> operator ~(E a)
+{
+ return {E(~int(a))};
+}
+
+template <E X> struct R
+{
+ static void Func();
+};
+
+template <E X> struct S : R<~X>
+{
+};
+
+void Test()
+{
+ S<E::V>::Func();
+}
--- /dev/null
+// PR c++/94068 - ICE with template codes in check_narrowing.
+// { dg-do compile { target c++11 } }
+
+enum class A { A1, A2 };
+A foo ();
+long foo (int);
+
+template <typename>
+void
+bar ()
+{
+ const auto c{foo ()};
+}
--- /dev/null
+// { dg-do compile { target c++11 } }
+
+struct A
+{
+ constexpr A(int) { }
+ constexpr operator int() const { return 1; };
+};
+
+template <class T, int N>
+struct B
+{
+ static constexpr A a = A(N);
+ int ar[a];
+};
+
+B<int, 10> b;
--- /dev/null
+// 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)};
+}