]> gcc.gnu.org Git - gcc.git/commit
c++: Diagnose [basic.scope.block]/2 violations even in compound-stmt of function...
authorJakub Jelinek <jakub@redhat.com>
Tue, 5 Sep 2023 15:26:59 +0000 (17:26 +0200)
committerJakub Jelinek <jakub@redhat.com>
Tue, 5 Sep 2023 15:26:59 +0000 (17:26 +0200)
commitc24982689b8af19da9a64f2283fb99764a1c5db0
tree017b63e4d4661390c270698def251576e8aeecde
parente87212ead5e9f36945b5e2d290187e2adca34da5
c++: Diagnose [basic.scope.block]/2 violations even in compound-stmt of function-try-block [PR52953]

As the following testcase shows, while check_local_shadow diagnoses most of
the [basic.scope.block]/2 violations, it doesn't diagnose when parameter's
name is redeclared inside of the compound-stmt of a function-try-block.

There is in that case an extra scope (sk_try with parent artificial
sk_block with for FUNCTION_NEEDS_BODY_BLOCK another sk_block and only then
sk_function_param).

The in_function_try_handler case doesn't work correctly
void
foo (int x)
try {
}
catch (int)
{
  try {
  } catch (int x)
  {
  }
  try {
  } catch (int)
  {
    int x;
  }
}
(which is valid) is rejected, because
                || (TREE_CODE (old) == PARM_DECL
                    && (current_binding_level->kind == sk_catch
                        || current_binding_level->level_chain->kind == sk_catch)
                    && in_function_try_handler))
is true but nothing verified that for the first case
current_binding_level->level_chain->kind == sk_function_params
(with perhaps artificial scopes in between and in the latter case
with one extra level in between).

The patch also changes behavior where for catch handlers of function-try-block
the diagnostics will have the shadows function parameter wording as pedwarn
rather than the old redeclaration permerror.

2023-09-05  Jakub Jelinek  <jakub@redhat.com>

PR c++/52953
* name-lookup.h (struct cp_binding_level): Add artificial bit-field.
Formatting fixes.
* name-lookup.cc (check_local_shadow): Skip artificial bindings when
checking if parameter scope is parent scope.  Don't special case
FUNCTION_NEEDS_BODY_BLOCK.  Diagnose the in_function_try_handler
cases in the b->kind == sk_function_parms test and verify no
non-artificial intervening scopes.  Add missing auto_diagnostic_group.
* decl.cc (begin_function_body): Set
current_binding_level->artificial.
* semantics.cc (begin_function_try_block): Likewise.

* g++.dg/diagnostic/redeclaration-1.C: Expect different diagnostic
wording.
* g++.dg/diagnostic/redeclaration-3.C: New test.
* g++.dg/parse/pr31952-1.C: Expect different diagnostic wording.
* g++.dg/parse/pr31952-3.C: Likewise.
gcc/cp/decl.cc
gcc/cp/name-lookup.cc
gcc/cp/name-lookup.h
gcc/cp/semantics.cc
gcc/testsuite/g++.dg/diagnostic/redeclaration-1.C
gcc/testsuite/g++.dg/diagnostic/redeclaration-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/pr31952-1.C
gcc/testsuite/g++.dg/parse/pr31952-3.C
This page took 0.06277 seconds and 5 git commands to generate.