@@ -6599,6 +6599,7 @@ extern tree strip_top_quals (tree);
extern bool reference_related_p (tree, tree);
extern bool reference_compatible_p (tree, tree);
extern int remaining_arguments (tree);
+extern tree build_implicit_conv_flags (tree, tree, int);
extern tree perform_implicit_conversion (tree, tree, tsubst_flags_t);
extern tree perform_implicit_conversion_flags (tree, tree, tsubst_flags_t, int);
extern tree build_converted_constant_expr (tree, tree, tsubst_flags_t);
@@ -12638,6 +12638,25 @@ can_convert_arg_bad (tree to, tree from, tree arg, int flags,
return t != NULL;
}
+/* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
+ resolution FLAGS. */
+
+tree
+build_implicit_conv_flags (tree type, tree expr, int flags)
+{
+ /* In a template, we are only concerned about determining the
+ type of non-dependent expressions, so we do not have to
+ perform the actual conversion. But for initializers, we
+ need to be able to perform it at instantiation
+ (or instantiate_non_dependent_expr) time. */
+ expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
+ if (!(flags & LOOKUP_ONLYCONVERTING))
+ IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
+ if (flags & LOOKUP_NO_NARROWING)
+ IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
+ return expr;
+}
+
/* Convert EXPR to TYPE. Return the converted expression.
Note that we allow bad conversions here because by the time we get to
@@ -12674,18 +12693,7 @@ perform_implicit_conversion_flags (tree type, tree expr,
expr = error_mark_node;
}
else if (processing_template_decl && conv->kind != ck_identity)
- {
- /* In a template, we are only concerned about determining the
- type of non-dependent expressions, so we do not have to
- perform the actual conversion. But for initializers, we
- need to be able to perform it at instantiation
- (or instantiate_non_dependent_expr) time. */
- expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
- if (!(flags & LOOKUP_ONLYCONVERTING))
- IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
- if (flags & LOOKUP_NO_NARROWING)
- IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
- }
+ expr = build_implicit_conv_flags (type, expr, flags);
else
{
/* Give a conversion call the same location as expr. */
@@ -7235,7 +7235,12 @@ check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
/* In C++20, the call to build_aggr_init could have created
an INIT_EXPR with a CONSTRUCTOR as the RHS to handle
A(1, 2). */
- init = TREE_OPERAND (init_code, 1);
+ tree rhs = TREE_OPERAND (init_code, 1);
+ if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR)
+ /* Avoid leaking TARGET_EXPR into template trees. */
+ rhs = build_implicit_conv_flags (type, init, flags);
+ init = rhs;
+
init_code = NULL_TREE;
/* Don't call digest_init; it's unnecessary and will complain
about aggregate initialization of non-aggregate classes. */
@@ -21151,15 +21151,6 @@ tsubst_copy_and_build (tree t,
RETURN (build_lambda_object (r));
}
- case TARGET_EXPR:
- /* We can get here for a constant initializer of non-dependent type.
- FIXME stop folding in cp_parser_initializer_clause. */
- {
- tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
- complain);
- RETURN (r);
- }
-
case TRANSACTION_EXPR:
RETURN (tsubst_expr(t, args, complain, in_decl,
integral_constant_expression_p));
@@ -3272,12 +3272,17 @@ finish_compound_literal (tree type, tree compound_literal,
/* Represent other compound literals with TARGET_EXPR so we produce
a prvalue, and can elide copies. */
- if (!VECTOR_TYPE_P (type))
+ if (TREE_CODE (compound_literal) == CONSTRUCTOR
+ || TREE_CODE (compound_literal) == VEC_INIT_EXPR)
{
/* The CONSTRUCTOR is now an initializer, not a compound literal. */
- TREE_HAS_CONSTRUCTOR (compound_literal) = false;
+ if (TREE_CODE (compound_literal) == CONSTRUCTOR)
+ TREE_HAS_CONSTRUCTOR (compound_literal) = false;
compound_literal = get_target_expr_sfinae (compound_literal, complain);
}
+ else
+ /* For e.g. int{42} just make sure it's a prvalue. */
+ compound_literal = rvalue (compound_literal);
return compound_literal;
}
new file mode 100644
@@ -0,0 +1,11 @@
+// PR c++/101072
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -fno-elide-constructors }
+
+struct S {};
+
+template <class T> void
+foo (S s)
+{
+ constexpr S x = s;
+}