D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4889 - Declarator in "if" statement allows name shadowing
Summary: Declarator in "if" statement allows name shadowing
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-18 13:24 UTC by Tomash Brechko
Modified: 2018-10-19 05:48 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tomash Brechko 2010-09-18 13:24:55 UTC
The following compiles without error with dmd 2.049:

void
main()
{
  int i = 1;
  if (int i = 2)
    {
      assert(i == 2);
    }
  assert(i == 1);
}

It seems prohibiting such shadowing would be in line with block shadowing rules.
Comment 1 Stewart Gordon 2010-09-18 17:26:28 UTC
I'm not sure if the classification of this as enhancement is correct.  The mention of the restriction is under ScopeStatement

"Even though a new scope is introduced, local symbol declarations cannot shadow (hide) other local symbol declarations in the same function."

but was the intent of this statement that it would apply just as well to a variable declared in an if statement?  After all, it seems to me that reality its scope is that of the ScopeStatement, even though it isn't lexically within it.
Comment 2 yebblies 2011-06-15 08:45:40 UTC
Errors in D2 (2.053) but still allowed in D1 (1.068)
Comment 3 Walter Bright 2012-01-23 23:28:02 UTC
D1 only bug, not a spec issue.
Comment 4 Mathias LANG 2018-10-19 05:48:06 UTC
Since this works in D2 but not D1, closing.