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

disallow variable declaration of `_`, prevent `_` from shadowing itself, and prevent read access of `_` #1204

Closed
Hejsil opened this Issue Jul 6, 2018 · 8 comments

Comments

Projects
None yet
5 participants
@Hejsil
Member

Hejsil commented Jul 6, 2018

Currently, this is allowed:

test "" {
    var _: usize = 2;
    _ = false;
    var b = _;
}

This works because _ can be declared and used, but when assigned it is treated as a discard variable. I propose that _ should only work for discarding, and you shouldn't be able to declare it. Also, you should be able to discard payloads so that examples like these compile:

test "" {
    for ([]void{}) |_, i| {
        for ([]void{}) |_, j| { // error: redeclaration of variable '_'
            
        }
    }
}

@andrewrk andrewrk added the bug label Jul 7, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Jul 7, 2018

@tiehuis

This comment has been minimized.

Member

tiehuis commented Jul 10, 2018

What about when using _ as a member name in a struct or enum?

Lately I wrote something like this, which isn't that bad really and displays the intent clearly. Reordering however is a small issue. I'd consider this more about the consistency here than wanting it for the reordering issue.

const S = enum {
    ...
    _reserved18,
    _reserved19,
    avx512_4vnniw,
    avx512_4fmaps,
    _reserved20,
    _reserved21,
    _reserved22,
    _reserved23,
    _reserved24,
    _reserved25,
    _reserved26,
    _reserved27,
    _reserved28,
    _reserved29,
    _reserved30,
    _reserved31,
    _reserved32,
    _reserved33,
    pconfig,
    ...
};

would become:

const S = enum {
    ...
    _,
    _,
    avx512_4vnniw,
    avx512_4fmaps,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    _,
    pconfig,
    ...
};

Slight concern that this is less clear in intent.

@Hejsil

This comment has been minimized.

Member

Hejsil commented Jul 10, 2018

When I say disallow declaring _, I mean don't allow the name to be declared so it can be used. Your example would not declare 10 enum fields named _ but would treat _ as discard or padding (depending on how we look it this). #676 is related to this, right?

@tiehuis

This comment has been minimized.

Member

tiehuis commented Jul 10, 2018

Yeah, it would be for padding. That issue is exactly it and has already been mentioned it seems.

kristate added a commit to kristate/zig that referenced this issue Aug 2, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 2, 2018

@kristate

This comment has been minimized.

Contributor

kristate commented Aug 2, 2018

@Hejsil could you please open another issue for for loops? Thanks.

test "" {
    for ([]void{}) |_, i| {
        for ([]void{}) |_, j| { // error: redeclaration of variable '_'
            
        }
    }
}
@andrewrk

This comment has been minimized.

Member

andrewrk commented Aug 2, 2018

@kristate let's fix the for loop syntax too (and any other place where we create a variable with |syntax|), before we close this issue.

@BarabasGitHub

This comment has been minimized.

Contributor

BarabasGitHub commented Aug 2, 2018

@andrewrk What's wrong with the loop syntax? It's kinda neat. At least the |variable| part.

@ghost

This comment has been minimized.

ghost commented Aug 2, 2018

_ = false; actually makes sende in the context of
#1268 (comment)

custom_allocator actually implements the allocator interface, there can easily be added a small syntax to ensure this or a small hack like in golang using an anonymous variable

@andrewrk

This comment has been minimized.

Member

andrewrk commented Aug 2, 2018

@BarabasGitHub what's wrong is that it doesn't compile, but it should. error: redeclaration of variable '_' is an incorrect compile error.

@andrewrk andrewrk changed the title from Disallow declaring `_` to disallow variable declaration of `_`, prevent `_` from shadowing itself, and prevent read access of `_` Aug 2, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

kristate added a commit to kristate/zig that referenced this issue Aug 3, 2018

@andrewrk andrewrk closed this in 298abbc Aug 3, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment