* [PATCH] c++: make finish non_static_data_member SFINAE friendly [PR105351]
@ 2022-05-03 17:40 Patrick Palka
2022-05-03 18:42 ` Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2022-05-03 17:40 UTC (permalink / raw)
To: gcc-patches
Here, since finish_non_static_data_member isn't SFINAE friendly, we
incorrectly emit an error during overload resolution:
sfinae33.C: In substitution of ‘template<class T> A<T::value> f() [with T = B]’:
sfinae33.C:11:7: required from here
sfinae33.C:5:31: error: invalid use of non-static data member ‘B::value’
5 | template<class T> A<T::value> f();
| ^
This patch fixes this by making the function SFINAE friendly in the
usual way: give it a complain parameter, check it before emitting an
error, and pass it through appropriately.
Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?
PR c++/105351
gcc/cp/ChangeLog:
* cp-tree.h (finish_non_static_data_member): Add defaulted
complain parameter.
* pt.cc (tsubst_copy_and_build): Pass complain to
finish_non_static_data_member.
* semantics.cc (finish_non_static_data_member): Respect complain
parameter.
(finish_qualified_id_expr): Pass complain to
finish_non_static_data_member.
gcc/testsuite/ChangeLog:
* g++.dg/template/sfinae33.C: New test.
---
gcc/cp/cp-tree.h | 3 ++-
gcc/cp/pt.cc | 3 ++-
gcc/cp/semantics.cc | 27 ++++++++++++++----------
gcc/testsuite/g++.dg/template/sfinae33.C | 12 +++++++++++
4 files changed, 32 insertions(+), 13 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/template/sfinae33.C
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 72177999968..b29e60ee52f 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7615,7 +7615,8 @@ inline tree force_paren_expr_uneval (tree t)
{ return force_paren_expr (t, true); }
extern tree maybe_undo_parenthesized_ref (tree);
extern tree maybe_strip_ref_conversion (tree);
-extern tree finish_non_static_data_member (tree, tree, tree);
+extern tree finish_non_static_data_member (tree, tree, tree,
+ tsubst_flags_t = tf_warning_or_error);
extern tree begin_stmt_expr (void);
extern tree finish_stmt_expr_expr (tree, tree);
extern tree finish_stmt_expr (tree, bool);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 81c3c598c71..f96f8e31885 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -21059,7 +21059,8 @@ tsubst_copy_and_build (tree t,
}
else if (TREE_CODE (member) == FIELD_DECL)
{
- r = finish_non_static_data_member (member, object, NULL_TREE);
+ r = finish_non_static_data_member (member, object, NULL_TREE,
+ complain);
if (TREE_CODE (r) == COMPONENT_REF)
REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
RETURN (r);
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 377f61113c0..bfb737f665b 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -2141,7 +2141,8 @@ finish_parenthesized_expr (cp_expr expr)
preceded by `.' or `->'. */
tree
-finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
+finish_non_static_data_member (tree decl, tree object, tree qualifying_scope,
+ tsubst_flags_t complain /* = tf_warning_or_error */)
{
gcc_assert (TREE_CODE (decl) == FIELD_DECL);
bool try_omp_private = !object && omp_private_member_map;
@@ -2172,12 +2173,15 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
if (is_dummy_object (object) && cp_unevaluated_operand == 0
&& (!processing_template_decl || !current_class_ref))
{
- if (current_function_decl
- && DECL_STATIC_FUNCTION_P (current_function_decl))
- error ("invalid use of member %qD in static member function", decl);
- else
- error ("invalid use of non-static data member %qD", decl);
- inform (DECL_SOURCE_LOCATION (decl), "declared here");
+ if (complain & tf_error)
+ {
+ if (current_function_decl
+ && DECL_STATIC_FUNCTION_P (current_function_decl))
+ error ("invalid use of member %qD in static member function", decl);
+ else
+ error ("invalid use of non-static data member %qD", decl);
+ inform (DECL_SOURCE_LOCATION (decl), "declared here");
+ }
return error_mark_node;
}
@@ -2219,8 +2223,9 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
{
tree access_type = TREE_TYPE (object);
- perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
- decl, tf_warning_or_error);
+ if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
+ decl, complain))
+ return error_mark_node;
/* If the data member was named `C::M', convert `*this' to `C'
first. */
@@ -2234,7 +2239,7 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
ret = build_class_member_access_expr (object, decl,
/*access_path=*/NULL_TREE,
/*preserve_reference=*/false,
- tf_warning_or_error);
+ complain);
}
if (try_omp_private)
{
@@ -2396,7 +2401,7 @@ finish_qualified_id_expr (tree qualifying_class,
{
push_deferring_access_checks (dk_no_check);
expr = finish_non_static_data_member (expr, NULL_TREE,
- qualifying_class);
+ qualifying_class, complain);
pop_deferring_access_checks ();
}
else if (BASELINK_P (expr))
diff --git a/gcc/testsuite/g++.dg/template/sfinae33.C b/gcc/testsuite/g++.dg/template/sfinae33.C
new file mode 100644
index 00000000000..cb9d4982211
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/sfinae33.C
@@ -0,0 +1,12 @@
+// PR c++/105351
+
+template<int> struct A;
+
+template<class T> A<T::value> f();
+template<class T> void f();
+
+struct B { int value; };
+
+int main() {
+ f<B>();
+}
--
2.36.0.44.g0f828332d5
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] c++: make finish non_static_data_member SFINAE friendly [PR105351]
2022-05-03 17:40 [PATCH] c++: make finish non_static_data_member SFINAE friendly [PR105351] Patrick Palka
@ 2022-05-03 18:42 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-05-03 18:42 UTC (permalink / raw)
To: Patrick Palka, gcc-patches
On 5/3/22 13:40, Patrick Palka wrote:
> Here, since finish_non_static_data_member isn't SFINAE friendly, we
> incorrectly emit an error during overload resolution:
>
> sfinae33.C: In substitution of ‘template<class T> A<T::value> f() [with T = B]’:
> sfinae33.C:11:7: required from here
> sfinae33.C:5:31: error: invalid use of non-static data member ‘B::value’
> 5 | template<class T> A<T::value> f();
> | ^
>
> This patch fixes this by making the function SFINAE friendly in the
> usual way: give it a complain parameter, check it before emitting an
> error, and pass it through appropriately.
>
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?
OK.
> PR c++/105351
>
> gcc/cp/ChangeLog:
>
> * cp-tree.h (finish_non_static_data_member): Add defaulted
> complain parameter.
> * pt.cc (tsubst_copy_and_build): Pass complain to
> finish_non_static_data_member.
> * semantics.cc (finish_non_static_data_member): Respect complain
> parameter.
> (finish_qualified_id_expr): Pass complain to
> finish_non_static_data_member.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/template/sfinae33.C: New test.
> ---
> gcc/cp/cp-tree.h | 3 ++-
> gcc/cp/pt.cc | 3 ++-
> gcc/cp/semantics.cc | 27 ++++++++++++++----------
> gcc/testsuite/g++.dg/template/sfinae33.C | 12 +++++++++++
> 4 files changed, 32 insertions(+), 13 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/template/sfinae33.C
>
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index 72177999968..b29e60ee52f 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -7615,7 +7615,8 @@ inline tree force_paren_expr_uneval (tree t)
> { return force_paren_expr (t, true); }
> extern tree maybe_undo_parenthesized_ref (tree);
> extern tree maybe_strip_ref_conversion (tree);
> -extern tree finish_non_static_data_member (tree, tree, tree);
> +extern tree finish_non_static_data_member (tree, tree, tree,
> + tsubst_flags_t = tf_warning_or_error);
> extern tree begin_stmt_expr (void);
> extern tree finish_stmt_expr_expr (tree, tree);
> extern tree finish_stmt_expr (tree, bool);
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 81c3c598c71..f96f8e31885 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -21059,7 +21059,8 @@ tsubst_copy_and_build (tree t,
> }
> else if (TREE_CODE (member) == FIELD_DECL)
> {
> - r = finish_non_static_data_member (member, object, NULL_TREE);
> + r = finish_non_static_data_member (member, object, NULL_TREE,
> + complain);
> if (TREE_CODE (r) == COMPONENT_REF)
> REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
> RETURN (r);
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 377f61113c0..bfb737f665b 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -2141,7 +2141,8 @@ finish_parenthesized_expr (cp_expr expr)
> preceded by `.' or `->'. */
>
> tree
> -finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
> +finish_non_static_data_member (tree decl, tree object, tree qualifying_scope,
> + tsubst_flags_t complain /* = tf_warning_or_error */)
> {
> gcc_assert (TREE_CODE (decl) == FIELD_DECL);
> bool try_omp_private = !object && omp_private_member_map;
> @@ -2172,12 +2173,15 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
> if (is_dummy_object (object) && cp_unevaluated_operand == 0
> && (!processing_template_decl || !current_class_ref))
> {
> - if (current_function_decl
> - && DECL_STATIC_FUNCTION_P (current_function_decl))
> - error ("invalid use of member %qD in static member function", decl);
> - else
> - error ("invalid use of non-static data member %qD", decl);
> - inform (DECL_SOURCE_LOCATION (decl), "declared here");
> + if (complain & tf_error)
> + {
> + if (current_function_decl
> + && DECL_STATIC_FUNCTION_P (current_function_decl))
> + error ("invalid use of member %qD in static member function", decl);
> + else
> + error ("invalid use of non-static data member %qD", decl);
> + inform (DECL_SOURCE_LOCATION (decl), "declared here");
> + }
>
> return error_mark_node;
> }
> @@ -2219,8 +2223,9 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
> {
> tree access_type = TREE_TYPE (object);
>
> - perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
> - decl, tf_warning_or_error);
> + if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
> + decl, complain))
> + return error_mark_node;
>
> /* If the data member was named `C::M', convert `*this' to `C'
> first. */
> @@ -2234,7 +2239,7 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
> ret = build_class_member_access_expr (object, decl,
> /*access_path=*/NULL_TREE,
> /*preserve_reference=*/false,
> - tf_warning_or_error);
> + complain);
> }
> if (try_omp_private)
> {
> @@ -2396,7 +2401,7 @@ finish_qualified_id_expr (tree qualifying_class,
> {
> push_deferring_access_checks (dk_no_check);
> expr = finish_non_static_data_member (expr, NULL_TREE,
> - qualifying_class);
> + qualifying_class, complain);
> pop_deferring_access_checks ();
> }
> else if (BASELINK_P (expr))
> diff --git a/gcc/testsuite/g++.dg/template/sfinae33.C b/gcc/testsuite/g++.dg/template/sfinae33.C
> new file mode 100644
> index 00000000000..cb9d4982211
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/sfinae33.C
> @@ -0,0 +1,12 @@
> +// PR c++/105351
> +
> +template<int> struct A;
> +
> +template<class T> A<T::value> f();
> +template<class T> void f();
> +
> +struct B { int value; };
> +
> +int main() {
> + f<B>();
> +}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-05-03 18:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-03 17:40 [PATCH] c++: make finish non_static_data_member SFINAE friendly [PR105351] Patrick Palka
2022-05-03 18:42 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).