From: Jason Merrill Date: Tue, 5 Apr 2022 20:02:04 +0000 (-0400) Subject: c++: -Wshadow=compatible-local type vs var [PR100608] X-Git-Tag: releases/gcc-11.3.0~64 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c52cd0b35d356565f67f7956f4defc022dfa2172;p=thirdparty%2Fgcc.git c++: -Wshadow=compatible-local type vs var [PR100608] 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. PR c++/100608 gcc/cp/ChangeLog: * name-lookup.c (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. --- diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index e9af670c28f4..2901e048b8d2 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -3259,6 +3259,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 000000000000..0e5ece74b37b --- /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 class X {}; + +void foo() +{ + auto a = X{}; // no warning, not compatible +}