public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: -Wshadow=compatible-local type vs var [PR100608]
@ 2022-04-06 14:30 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2022-04-06 14:30 UTC (permalink / raw)
  To: gcc-patches

The patch for PR92024 changed -Wshadow=compatible-local to warn if either
new or old decl was a type, but the rationale only talked about the case
where both are types.  If only one is, they aren't compatible.

Tested x86_64-pc-linux-gnu, applying to trunk.

	PR c++/100608

gcc/cp/ChangeLog:

	* name-lookup.cc (check_local_shadow): Use -Wshadow=local
	if exactly one of 'old' and 'decl' is a type.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wshadow-compatible-local-3.C: New test.
---
 gcc/cp/name-lookup.cc                                  |  4 ++++
 gcc/testsuite/g++.dg/warn/Wshadow-compatible-local-3.C | 10 ++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wshadow-compatible-local-3.C

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index c833b84ca8a..d16c577c029 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -3249,6 +3249,10 @@ check_local_shadow (tree decl)
       enum opt_code warning_code;
       if (warn_shadow)
 	warning_code = OPT_Wshadow;
+      else if ((TREE_CODE (decl) == TYPE_DECL)
+	       ^ (TREE_CODE (old) == TYPE_DECL))
+	/* If exactly one is a type, they aren't compatible.  */
+	warning_code = OPT_Wshadow_local;
       else if ((TREE_TYPE (old)
 		&& TREE_TYPE (decl)
 		&& same_type_p (TREE_TYPE (old), TREE_TYPE (decl)))
diff --git a/gcc/testsuite/g++.dg/warn/Wshadow-compatible-local-3.C b/gcc/testsuite/g++.dg/warn/Wshadow-compatible-local-3.C
new file mode 100644
index 00000000000..0e5ece74b37
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wshadow-compatible-local-3.C
@@ -0,0 +1,10 @@
+// PR c++/100608
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wshadow=compatible-local" }
+
+template <typename> class X {};
+
+void foo()
+{
+  auto a = X<class a>{};	// no warning, not compatible
+}

base-commit: e1a5e7562d53a8d2256f754714b06595bea72196
-- 
2.27.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-06 14:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-06 14:30 [pushed] c++: -Wshadow=compatible-local type vs var [PR100608] 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).