Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stage2: variable shadowing detection #6969

Merged
merged 6 commits into from Dec 6, 2020
Merged

stage2: variable shadowing detection #6969

merged 6 commits into from Dec 6, 2020

Conversation

@g-w1
Copy link
Contributor

@g-w1 g-w1 commented Nov 4, 2020

This allows detection of variable shadowing in stage2, fixing a TODO in varDecl function. I added a compileError test to test/stage2/test.zig, but it didn't seem to run.

Edit:
The test actually did run and pass, but it said that it ran the wrong number of tests?

[jacob@archlinux ~co/zig/zig]$ zig build test-stage2
Test [1/17] test "self-hosted"... tests [1/28] referencing decls which appear later in the file [1/1] updAll 17 tests passed.
[jacob@archlinux ~co/zig/zig]$

Why does it say 1/17 and 1/28? and then only 17 tests pass? I think this is a bug in the testing code.

@EleanorNB
Copy link
Contributor

@EleanorNB EleanorNB commented Nov 9, 2020

Nice.

@g-w1 g-w1 force-pushed the g-w1:shadowing branch from 86265cb to 2b0caec Nov 9, 2020
test/stage2/test.zig Outdated Show resolved Hide resolved
@g-w1 g-w1 force-pushed the g-w1:shadowing branch from 2b0caec to 1aacfe9 Nov 19, 2020
@Vexu Vexu merged commit 6294c11 into ziglang:master Dec 6, 2020
3 checks passed
3 checks passed
builds.hut.lavatech.top: freebsd.yml builds.sr.ht job completed successfully
Details
continuous-integration/drone/pr Build is passing
Details
ziglang.zig #20201119.23 succeeded
Details
@g-w1 g-w1 deleted the g-w1:shadowing branch Dec 6, 2020
@LemonBoy
Copy link
Contributor

@LemonBoy LemonBoy commented Dec 6, 2020

This implementation has the same two-step scope lookup as stage1, including the flaw noticed in #7141.
I'd expect the stage2 compiler to implement the correct shadowing logic, or are we playing the "It doesn't have to be perfect" card?

@g-w1 g-w1 restored the g-w1:shadowing branch Dec 6, 2020
@g-w1
Copy link
Contributor Author

@g-w1 g-w1 commented Dec 6, 2020

I just used the logic from the identifier resolution code here, so if we want to implement the more "advanced" lookup logic we should probably abstract it into a "lookup" function and then use it in both places.

@LemonBoy
Copy link
Contributor

@LemonBoy LemonBoy commented Dec 6, 2020

It's not a "basic" vs "more advanced" thing, it's a "according to the specification" vs "stage1's behaviour" case.
Keep in mind that a lot of code will be broken by this change, don't forget to buckle up.

@Vexu
Copy link
Member

@Vexu Vexu commented Dec 6, 2020

The problem with stage1 is that declarations only look for redefinitions in the same container, not with local variables.

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Dec 6, 2020

The logic looks OK to me here. Let me clarify the language specification (cc @SpexGuy):

Declarations are allowed to collide, so that it is possible to declare, for example, both of these:

  • a.b.c
  • a.c
pub const a = struct {
    pub const b = struct {
        pub const c = struct {};
    };
    pub const c = struct {};
};

This is allowed, however, ambiguous references to c would be a compile error. So, inside b, one must use a.c or b.c in order to refer to c. Referring to c directly would be a compile error.

Parameters and local variables are never allowed to shadow anything, including each other, or any declarations, no exceptions.

@LemonBoy
Copy link
Contributor

@LemonBoy LemonBoy commented Dec 6, 2020

So, if I understand correctly your example, #7140 is legal but the use of print inside print should trigger an error?

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Dec 6, 2020

Yes

@andrewrk
Copy link
Member

@andrewrk andrewrk commented Dec 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants