public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE with -Wshadow and enumerator in template [PR99120]
@ 2021-03-05 16:44 Marek Polacek
  2021-03-05 21:22 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2021-03-05 16:44 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

We crash here, because in a template, an enumerator doesn't have
a type until we've called finish_enum_value_list.  But our -Wshadow
implementation, check_local_shadow, is called when we pushdecl in
build_enumerator, which takes place before finish_enum_value_list.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/10?

gcc/cp/ChangeLog:

	PR c++/99120
	* name-lookup.c (check_local_shadow): Check if the type of decl
	is non-null before checking TYPE_PTR*.

gcc/testsuite/ChangeLog:

	PR c++/99120
	* g++.dg/warn/Wshadow-17.C: New test.
---
 gcc/cp/name-lookup.c                   |  7 ++++---
 gcc/testsuite/g++.dg/warn/Wshadow-17.C | 11 +++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wshadow-17.C

diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index f57708700c2..092fa6b8768 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -3309,7 +3309,7 @@ check_local_shadow (tree decl)
   /* Don't warn for artificial things that are not implicit typedefs.  */
   if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
     return;
-  
+
   if (nonlambda_method_basetype ())
     if (tree member = lookup_member (current_nonlambda_class_type (),
 				     DECL_NAME (decl), /*protect=*/0,
@@ -3321,8 +3321,9 @@ check_local_shadow (tree decl)
 	   is a function or a pointer-to-function.  */
 	if (!OVL_P (member)
 	    || TREE_CODE (decl) == FUNCTION_DECL
-	    || TYPE_PTRFN_P (TREE_TYPE (decl))
-	    || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
+	    || (TREE_TYPE (decl)
+		&& (TYPE_PTRFN_P (TREE_TYPE (decl))
+		    || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
 	  {
 	    auto_diagnostic_group d;
 	    if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-17.C b/gcc/testsuite/g++.dg/warn/Wshadow-17.C
new file mode 100644
index 00000000000..0dee397796f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wshadow-17.C
@@ -0,0 +1,11 @@
+// PR c++/99120
+// { dg-options "-Wshadow" }
+
+struct S {
+  void X();
+
+  template<typename T>
+  void fn () {
+    enum { X };
+  }
+};

base-commit: 4d66685e49d20e0c7a87c5fa0757c7eb63ffcdaa
-- 
2.29.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: ICE with -Wshadow and enumerator in template [PR99120]
  2021-03-05 16:44 [PATCH] c++: ICE with -Wshadow and enumerator in template [PR99120] Marek Polacek
@ 2021-03-05 21:22 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-03-05 21:22 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 3/5/21 11:44 AM, Marek Polacek wrote:
> We crash here, because in a template, an enumerator doesn't have
> a type until we've called finish_enum_value_list.  But our -Wshadow
> implementation, check_local_shadow, is called when we pushdecl in
> build_enumerator, which takes place before finish_enum_value_list.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/10?

OK.

> gcc/cp/ChangeLog:
> 
> 	PR c++/99120
> 	* name-lookup.c (check_local_shadow): Check if the type of decl
> 	is non-null before checking TYPE_PTR*.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c++/99120
> 	* g++.dg/warn/Wshadow-17.C: New test.
> ---
>   gcc/cp/name-lookup.c                   |  7 ++++---
>   gcc/testsuite/g++.dg/warn/Wshadow-17.C | 11 +++++++++++
>   2 files changed, 15 insertions(+), 3 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wshadow-17.C
> 
> diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
> index f57708700c2..092fa6b8768 100644
> --- a/gcc/cp/name-lookup.c
> +++ b/gcc/cp/name-lookup.c
> @@ -3309,7 +3309,7 @@ check_local_shadow (tree decl)
>     /* Don't warn for artificial things that are not implicit typedefs.  */
>     if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl))
>       return;
> -
> +
>     if (nonlambda_method_basetype ())
>       if (tree member = lookup_member (current_nonlambda_class_type (),
>   				     DECL_NAME (decl), /*protect=*/0,
> @@ -3321,8 +3321,9 @@ check_local_shadow (tree decl)
>   	   is a function or a pointer-to-function.  */
>   	if (!OVL_P (member)
>   	    || TREE_CODE (decl) == FUNCTION_DECL
> -	    || TYPE_PTRFN_P (TREE_TYPE (decl))
> -	    || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))
> +	    || (TREE_TYPE (decl)
> +		&& (TYPE_PTRFN_P (TREE_TYPE (decl))
> +		    || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)))))
>   	  {
>   	    auto_diagnostic_group d;
>   	    if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wshadow,
> diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-17.C b/gcc/testsuite/g++.dg/warn/Wshadow-17.C
> new file mode 100644
> index 00000000000..0dee397796f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wshadow-17.C
> @@ -0,0 +1,11 @@
> +// PR c++/99120
> +// { dg-options "-Wshadow" }
> +
> +struct S {
> +  void X();
> +
> +  template<typename T>
> +  void fn () {
> +    enum { X };
> +  }
> +};
> 
> base-commit: 4d66685e49d20e0c7a87c5fa0757c7eb63ffcdaa
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-05 21:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-05 16:44 [PATCH] c++: ICE with -Wshadow and enumerator in template [PR99120] Marek Polacek
2021-03-05 21:22 ` 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).