* init.c (get_nsdmi): Add complain parm.
* typeck2.c (digest_nsdmi_init): Add complain parm.
(process_init_constructor_record): Pass complain to get_nsdmi.
* pt.c (maybe_instantiate_noexcept): Add complain parm, return bool.
* method.c (get_defaulted_eh_spec): Add complain parm. Pass it into
synthesized_method_walk.
(synthesized_method_walk): Adjust.
(walk_field_subobs): Pass complain to get_nsdmi.
(defaulted_late_check): Skip other checks if deleted.
* decl2.c (mark_used): Pass complain to maybe_instantiate_noexcept.
* call.c (build_aggr_conv): Pass complain to get_nsdmi.
* parser.c (defarg_location): New.
* error.c (location_of): Use it.
From-SVN: r250994
+2017-08-09 Jason Merrill <jason@redhat.com>
+
+ PR c++/81359 - Unparsed NSDMI error from SFINAE context.
+ * init.c (get_nsdmi): Add complain parm.
+ * typeck2.c (digest_nsdmi_init): Add complain parm.
+ (process_init_constructor_record): Pass complain to get_nsdmi.
+ * pt.c (maybe_instantiate_noexcept): Add complain parm, return bool.
+ * method.c (get_defaulted_eh_spec): Add complain parm. Pass it into
+ synthesized_method_walk.
+ (synthesized_method_walk): Adjust.
+ (walk_field_subobs): Pass complain to get_nsdmi.
+ (defaulted_late_check): Skip other checks if deleted.
+ * decl2.c (mark_used): Pass complain to maybe_instantiate_noexcept.
+ * call.c (build_aggr_conv): Pass complain to get_nsdmi.
+ * parser.c (defarg_location): New.
+ * error.c (location_of): Use it.
+
2017-08-09 Marek Polacek <polacek@redhat.com>
* parser.c (cp_parser_perform_range_for_lookup): Use false instead of 0.
if (i < CONSTRUCTOR_NELTS (ctor))
val = CONSTRUCTOR_ELT (ctor, i)->value;
else if (DECL_INITIAL (field))
- val = get_nsdmi (field, /*ctor*/false);
+ val = get_nsdmi (field, /*ctor*/false, complain);
else if (TREE_CODE (ftype) == REFERENCE_TYPE)
/* Value-initialization of reference is ill-formed. */
return NULL;
continue;
/* In C++17 we need the noexcept-qualifier to compare types. */
- if (flag_noexcept_type)
- maybe_instantiate_noexcept (fn);
+ if (flag_noexcept_type
+ && !maybe_instantiate_noexcept (fn, complain))
+ continue;
/* See if there's a match. */
tree fntype = static_fn_type (fn);
/* In C++17 we need the noexcept-qualifier to compare types. */
if (flag_noexcept_type)
- maybe_instantiate_noexcept (instantiation);
+ maybe_instantiate_noexcept (instantiation, complain);
/* See if there's a match. */
tree fntype = static_fn_type (instantiation);
extern tree build_zero_init (tree, tree, bool);
extern tree build_value_init (tree, tsubst_flags_t);
extern tree build_value_init_noctor (tree, tsubst_flags_t);
-extern tree get_nsdmi (tree, bool);
+extern tree get_nsdmi (tree, bool, tsubst_flags_t);
extern tree build_offset_ref (tree, tree, bool,
tsubst_flags_t);
extern tree throw_bad_array_new_length (void);
extern tree forward_parm (tree);
extern bool is_trivially_xible (enum tree_code, tree, tree);
extern bool is_xible (enum tree_code, tree, tree);
-extern tree get_defaulted_eh_spec (tree);
+extern tree get_defaulted_eh_spec (tree, tsubst_flags_t = tf_warning_or_error);
extern void after_nsdmi_defaulted_late_checks (tree);
extern bool maybe_explain_implicit_delete (tree);
extern void explain_implicit_non_constexpr (tree);
extern bool parsing_nsdmi (void);
extern bool parsing_default_capturing_generic_lambda_in_template (void);
extern void inject_this_parameter (tree, cp_cv_quals);
+extern location_t defarg_location (tree);
/* in pt.c */
extern bool check_template_shadow (tree);
extern void do_decl_instantiation (tree, tree);
extern void do_type_instantiation (tree, tree, tsubst_flags_t);
extern bool always_instantiate_p (tree);
-extern void maybe_instantiate_noexcept (tree);
+extern bool maybe_instantiate_noexcept (tree, tsubst_flags_t = tf_warning_or_error);
extern tree instantiate_decl (tree, bool, bool);
extern int comp_template_parms (const_tree, const_tree);
extern bool builtin_pack_fn_p (tree);
extern bool check_narrowing (tree, tree, tsubst_flags_t);
extern tree digest_init (tree, tree, tsubst_flags_t);
extern tree digest_init_flags (tree, tree, int, tsubst_flags_t);
-extern tree digest_nsdmi_init (tree, tree);
+extern tree digest_nsdmi_init (tree, tree, tsubst_flags_t);
extern tree build_scoped_ref (tree, tree, tree *);
extern tree build_x_arrow (location_t, tree,
tsubst_flags_t);
if (TREE_CODE (decl) == CONST_DECL)
used_types_insert (DECL_CONTEXT (decl));
- if (TREE_CODE (decl) == FUNCTION_DECL)
- maybe_instantiate_noexcept (decl);
+ if (TREE_CODE (decl) == FUNCTION_DECL
+ && !maybe_instantiate_noexcept (decl, complain))
+ return false;
if (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_DELETED_FN (decl))
if (DECL_P (t))
return DECL_SOURCE_LOCATION (t);
+ if (TREE_CODE (t) == DEFAULT_ARG)
+ return defarg_location (t);
return EXPR_LOC_OR_LOC (t, input_location);
}
/* Return the non-static data initializer for FIELD_DECL MEMBER. */
tree
-get_nsdmi (tree member, bool in_ctor)
+get_nsdmi (tree member, bool in_ctor, tsubst_flags_t complain)
{
tree init;
tree save_ccp = current_class_ptr;
{
init = DECL_INITIAL (DECL_TI_TEMPLATE (member));
if (TREE_CODE (init) == DEFAULT_ARG)
- goto unparsed;
-
+ /* Unparsed. */;
/* Check recursive instantiation. */
- if (DECL_INSTANTIATING_NSDMI_P (member))
+ else if (DECL_INSTANTIATING_NSDMI_P (member))
{
- error ("recursive instantiation of non-static data member "
- "initializer for %qD", member);
+ if (complain & tf_error)
+ error ("recursive instantiation of default member "
+ "initializer for %qD", member);
init = error_mark_node;
}
else
{
DECL_INSTANTIATING_NSDMI_P (member) = 1;
-
+
/* Do deferred instantiation of the NSDMI. */
init = (tsubst_copy_and_build
(init, DECL_TI_ARGS (member),
- tf_warning_or_error, member, /*function_p=*/false,
+ complain, member, /*function_p=*/false,
/*integral_constant_expression_p=*/false));
- init = digest_nsdmi_init (member, init);
+ init = digest_nsdmi_init (member, init, complain);
DECL_INSTANTIATING_NSDMI_P (member) = 0;
}
}
else
+ init = DECL_INITIAL (member);
+
+ if (init && TREE_CODE (init) == DEFAULT_ARG)
{
- init = DECL_INITIAL (member);
- if (init && TREE_CODE (init) == DEFAULT_ARG)
+ if (complain & tf_error)
{
- unparsed:
- error ("constructor required before non-static data member "
- "for %qD has been parsed", member);
+ error ("default member initializer for %qD required before the end "
+ "of its enclosing class", member);
+ inform (location_of (init), "defined here");
DECL_INITIAL (member) = error_mark_node;
- init = error_mark_node;
}
- /* Strip redundant TARGET_EXPR so we don't need to remap it, and
- so the aggregate init code below will see a CONSTRUCTOR. */
- bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
- if (simple_target)
- init = TARGET_EXPR_INITIAL (init);
- init = break_out_target_exprs (init);
- if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
- /* Now put it back so C++17 copy elision works. */
- init = get_target_expr (init);
+ init = error_mark_node;
}
+
+ /* Strip redundant TARGET_EXPR so we don't need to remap it, and
+ so the aggregate init code below will see a CONSTRUCTOR. */
+ bool simple_target = (init && SIMPLE_TARGET_EXPR_P (init));
+ if (simple_target)
+ init = TARGET_EXPR_INITIAL (init);
+ init = break_out_target_exprs (init);
+ if (simple_target && TREE_CODE (init) != CONSTRUCTOR)
+ /* Now put it back so C++17 copy elision works. */
+ init = get_target_expr (init);
+
current_class_ptr = save_ccp;
current_class_ref = save_ccr;
return init;
/* Use the non-static data member initializer if there was no
mem-initializer for this field. */
if (init == NULL_TREE)
- init = get_nsdmi (member, /*ctor*/true);
+ init = get_nsdmi (member, /*ctor*/true, tf_warning_or_error);
if (init == error_mark_node)
return;
default constructor is noexcept(false). */
if (spec_p)
{
- tree nsdmi = get_nsdmi (field, /*ctor*/false);
+ tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
if (!expr_noexcept_p (nsdmi, complain))
*spec_p = noexcept_false_spec;
}
flags |= LOOKUP_DEFAULTED;
tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
+ if (diag && spec_p)
+ /* We're in get_defaulted_eh_spec; we don't actually want any walking
+ diagnostics, we just want complain set. */
+ diag = false;
int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
for (binfo = TYPE_BINFO (ctype), i = 0;
needed. Return what it should be. */
tree
-get_defaulted_eh_spec (tree decl)
+get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
{
if (DECL_CLONED_FUNCTION_P (decl))
decl = DECL_CLONED_FUNCTION (decl);
tree parm_type = TREE_VALUE (parms);
bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
tree spec = empty_except_spec;
+ bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
- NULL, false, DECL_INHERITED_CTOR (decl),
+ NULL, diag, DECL_INHERITED_CTOR (decl),
parms);
return spec;
}
"does not match expected signature %qD", implicit_fn);
}
+ if (DECL_DELETED_FN (implicit_fn))
+ {
+ DECL_DELETED_FN (fn) = 1;
+ return;
+ }
+
/* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
exception-specification only if it is compatible (15.4) with the
exception-specification on the implicit declaration. If a function
}
DECL_DECLARED_CONSTEXPR_P (fn) = false;
}
-
- if (DECL_DELETED_FN (implicit_fn))
- DECL_DELETED_FN (fn) = 1;
}
/* OK, we've parsed the NSDMI for class T, now we can check any explicit
else if (maybe_reject_flexarray_init (decl, parsed_arg))
parsed_arg = error_mark_node;
else
- parsed_arg = digest_nsdmi_init (decl, parsed_arg);
+ parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
}
/* If the token stream has not been completely used up, then
return default_argument;
}
+/* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
+
+location_t
+defarg_location (tree default_argument)
+{
+ cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
+ location_t start = tokens->first->location;
+ location_t end = tokens->last->location;
+ return make_location (start, start, end);
+}
+
/* Begin parsing tentatively. We always save tokens while parsing
tentatively so that if the tentative parsing fails we can restore the
tokens. */
}
/* If FN has a noexcept-specifier that hasn't been instantiated yet,
- instantiate it now, modifying TREE_TYPE (fn). */
+ instantiate it now, modifying TREE_TYPE (fn). Returns false on
+ error, true otherwise. */
-void
-maybe_instantiate_noexcept (tree fn)
+bool
+maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
{
tree fntype, spec, noex, clone;
/* Don't instantiate a noexcept-specification from template context. */
if (processing_template_decl)
- return;
+ return true;
if (DECL_CLONED_FUNCTION_P (fn))
fn = DECL_CLONED_FUNCTION (fn);
spec = TYPE_RAISES_EXCEPTIONS (fntype);
if (!spec || !TREE_PURPOSE (spec))
- return;
+ return true;
noex = TREE_PURPOSE (spec);
static hash_set<tree>* fns = new hash_set<tree>;
bool added = false;
if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
- spec = get_defaulted_eh_spec (fn);
+ spec = get_defaulted_eh_spec (fn, complain);
else if (!(added = !fns->add (fn)))
{
/* If hash_set::add returns true, the element was already there. */
if (added)
fns->remove (fn);
+ if (spec == error_mark_node)
+ return false;
+
TREE_TYPE (fn) = build_exception_variant (fntype, spec);
}
else
TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
}
+
+ return true;
}
/* Produce the definition of D, a _DECL generated from a template. If
/* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL). */
tree
-digest_nsdmi_init (tree decl, tree init)
+digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
{
gcc_assert (TREE_CODE (decl) == FIELD_DECL);
flags = LOOKUP_NORMAL;
if (BRACE_ENCLOSED_INITIALIZER_P (init)
&& CP_AGGREGATE_TYPE_P (type))
- init = reshape_init (type, init, tf_warning_or_error);
- init = digest_init_flags (type, init, flags, tf_warning_or_error);
+ init = reshape_init (type, init, complain);
+ init = digest_init_flags (type, init, flags, complain);
if (TREE_CODE (init) == TARGET_EXPR)
/* This represents the whole initialization. */
TARGET_EXPR_DIRECT_INIT_P (init) = true;
goto restart;
}
/* C++14 aggregate NSDMI. */
- next = get_nsdmi (field, /*ctor*/false);
+ next = get_nsdmi (field, /*ctor*/false, complain);
}
else if (type_build_ctor_call (TREE_TYPE (field)))
{
{
CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
field,
- get_nsdmi (field, /*in_ctor=*/false));
+ get_nsdmi (field, /*in_ctor=*/false,
+ complain));
break;
}
}
struct A
{
- int i = (A(), 42); // { dg-error "constructor required" }
+ int i = (A(), 42); // { dg-error "" }
};
A a;
template<int> struct A
{
- int i = (A<0>(), 0); // { dg-error "recursive instantiation of non-static data" }
+ int i = (A<0>(), 0); // { dg-error "recursive instantiation of default" }
};
A<0> a;
template<int N> struct B
{
- B* p = new B<N>; // { dg-error "recursive instantiation of non-static data" }
+ B* p = new B<N>; // { dg-error "recursive instantiation of default" }
};
B<1> x;
int y1 = 1;
};
- A1(const B1& opts = B1()) {} // { dg-error "constructor" }
+ A1(const B1& opts = B1()) {} // { dg-error "default member initializer" }
};
struct A2 {
int x2, y2 = 1;
};
- A2(const B2& opts = B2()) {} // { dg-error "constructor" }
+ A2(const B2& opts = B2()) {} // { dg-error "default member initializer" }
};
--- /dev/null
+// PR c++/81359
+// { dg-do compile { target c++11 } }
+
+template<typename _Tp, typename = decltype(_Tp())>
+static int test(int);
+
+template<typename>
+static void test(...);
+
+template <class T, class = decltype(test<T>(0))>
+struct A { };
+
+struct B
+{
+ struct C {
+ int i = 0;
+ };
+ A<C> a;
+};