Bug 31952 - parameters may be redeclared in a function try-block
Summary: parameters may be redeclared in a function try-block
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.1
: P3 normal
Target Milestone: 4.9.0
Assignee: Paolo Carlini
URL:
Keywords: accepts-invalid, diagnostic
: 5605 (view as bug list)
Depends on:
Blocks:
 
Reported: 2007-05-16 14:13 UTC by Andrew Stubbs
Modified: 2013-05-15 16:11 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2007-05-17 15:51:41


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Stubbs 2007-05-16 14:13:01 UTC
According to the C++ standard, clause 3.3.2, paragraph 2, sentence 3, the following program should be invalid:

int
foo (int bar)
try
{
  return 0;
}
catch (...)
{
  int bar = 0; // invalid
  return 1;
}

Specifically, `bar' may not be redeclared in the outermost block of a handler in a function try-block.

GCC 4.1.1 gives no errors or warnings (other than unused parameters and variables).
Comment 1 David Fang 2007-05-16 20:46:50 UTC
Poor man's workaround: -Wshadow -Werror
Comment 2 Andrew Stubbs 2007-05-17 15:34:55 UTC
Another example perhaps?

void
foo()
{
  try
    {
    }
  catch (void *e)
    {
      void *e; // invalid
    }
}

The C++ standard, clause 3.3.2 paragraph 3, states that catch exception-declarations may not be redeclared in the outermost block of the catch block.
Comment 3 Wolfgang Bangerth 2007-05-17 15:51:41 UTC
Confirmed.
Comment 4 Andrew Stubbs 2007-11-01 18:15:46 UTC
It gets worse :(

The following example used to be detected by GCC 4.1.1, but is now permitted by GCC 4.1.2, 4.2.1, 4.2.2, and 4.3-20071026, and hence is a regression.

int
foo (int bar)
try
{
  return 0;
}
catch (int bar)  // invalid
{
  return 1;
}


-Wshadow still detects it, but it is no longer an error.
Comment 5 Nan Wang 2009-11-06 06:58:03 UTC
still not solved at 4.3.4
Comment 6 Paolo Carlini 2012-08-20 10:29:11 UTC
Comment #2 is PR5605.

Comparing to Janis' fix for PR2288 (http://gcc.gnu.org/ml/gcc-patches/2010-03/msg00838.html) looks like the pushdecl_maybe_friend_1 checks must be beefed up, maybe we need an additional sk_* for Comment #2. Seems doable.

Adding Janis in CC in case she wants to beat me (or somebody else) on this.
Comment 7 Paolo Carlini 2012-08-20 10:40:59 UTC
*** Bug 5605 has been marked as a duplicate of this bug. ***
Comment 8 Paolo Carlini 2013-05-13 17:13:21 UTC
On it.
Comment 9 Paolo Carlini 2013-05-13 19:29:10 UTC
Jason, I'm on this issue but I'm not sure how we want to resolve it.

After Janis' patch (see Comment #6) in pushdecl_maybe_friend_1 we issue hard errors for some kinds of shadowings but not for others. For comparison, clang issues hard errors for all three testcases here, ICC warnings (like current GCC). If we wanted, I could tweak the parser to carefully do_pushlevel (sk_catch) and therefore change to errors only the specific shadowings at issue in this PR, but I'm also aiming for some consistency.
Comment 10 Jason Merrill 2013-05-14 15:55:47 UTC
(In reply to Paolo Carlini from comment #9)
> After Janis' patch (see Comment #6) in pushdecl_maybe_friend_1 we issue hard
> errors for some kinds of shadowings but not for others. For comparison,
> clang issues hard errors for all three testcases here, ICC warnings (like
> current GCC). If we wanted, I could tweak the parser to carefully
> do_pushlevel (sk_catch) and therefore change to errors only the specific
> shadowings at issue in this PR, but I'm also aiming for some consistency.

Well, the language requires diagnostics for some shadowings and not others, so it makes sense for us to mirror that.  But we probably want permerror rather than error.
Comment 11 Paolo Carlini 2013-05-15 16:11:26 UTC
Fixed for 4.9.0.