D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13216 - Failed anti-hijacking of with statement
Summary: Failed anti-hijacking of with statement
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, diagnostic
Depends on:
Blocks:
 
Reported: 2014-07-27 14:55 UTC by bearophile_hugs
Modified: 2020-11-09 01:38 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2014-07-27 14:55:49 UTC
This compiles with no errors with dmd 2.066beta5. This seems a bug, a failure of the anti-hijacking mechanism of with():



struct S { int k = 1; }
struct T { int k = 2; }
void main() {
    S s;
    T t;
    with (t) {
        assert(k == 2);
        with(s) {
            assert(k == 1);
        }
    }
}


Adapted from code by monarch_dodra:
http://forum.dlang.org/thread/txxqrxhaylixotsqimap@forum.dlang.org
Comment 1 Mathias LANG 2020-11-09 01:38:04 UTC
This works as intended. I think you are confusing two notions: hijacking and shadowing. We prevent hijacking by making sure a change in a different module will not suddenly change the symbol being called, but keep the same behavior or result in an error. There's no hijacking protection within a module itself.

However we have shadowing protection. We make sure not to allow a more nested scope to override a scope name from an outer scope, as long as this outer scope is in the same function.

However, `with`'s whole point is to bypass this. An object used in a `with` clause will be searched first for this scope. Erroring on shadowing would render the feature completely useless and unreliable.