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

solve the grammar ambiguity with enum literals inside array literals and struct literals #2235

Closed
schroffl opened this issue Apr 10, 2019 · 12 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@schroffl
Copy link
Contributor

schroffl commented Apr 10, 2019

Accepted Proposal


The new anonymous enum literals are great, but they don't to seem work with arrays. I assume that this is a bug.

Let's take this enum as an example:

const MyEnum = enum {Ok, Error};

Declaring a single variable works just fine:

var enum_instance: MyEnum = .Ok;

But an array of MyEnum does not:

var enum_instances: []MyEnum = {.Error};

Which leads to the following compiler output:

error: expected token '=', found '}'
@schroffl schroffl changed the title Anonymous enum literals aren't working in arrays Anonymous enum literals don't work with arrays Apr 10, 2019
@hryx
Copy link
Sponsor Contributor

hryx commented Apr 10, 2019

I'd expect that to work too. Enum literals seem to be missing from the grammar - are they supposed to be included? That might make this easier to address.

@andrewrk
Copy link
Member

andrewrk commented Apr 10, 2019

Enum literals aren't finished because they are missing from the grammar and documentation: #683

This is invalid syntax:

var enum_instances: []MyEnum = {.Error};

Correct syntax would be:

test "aoeu" {
    var enum_instances = [1]@typeOf(.x){.Error};
}

Which would give a compile error of error: variable of type '(enum literal)' must be const or comptime, which you could fix with using const instead of var.

However there is a grammar issue here, which is that {.Error is getting parsed as the beginning of a struct literal rather than an array literal with an enum literal in it.

Related: #208

@andrewrk andrewrk added this to the 0.5.0 milestone Apr 10, 2019
@andrewrk andrewrk changed the title Anonymous enum literals don't work with arrays grammar ambiguity with enum literals inside array literals and struct literals Apr 10, 2019
@andrewrk
Copy link
Member

cc @Hejsil I think you might have an opinion on how to resolve this.

@thejoshwolfe
Copy link
Sponsor Contributor

my proposal:

  • implement anonymous array literals.
  • remove current syntax for array literals.
  • then T{ is always the start of a struct initializer.

@schroffl
Copy link
Contributor Author

@thejoshwolfe I'm assuming by "anonymous array literals" you mean array initializers without the type prefix, right? Like this:

const arr: []u8 = {4, 2};

That would be really nice to have when the type can be inferred by the compiler, since you wouldn't have to specify it manually then. This actually sounds like a great solution to me.

Off topic: Is there an existing issue about anonymous array literals? I couldn't find any.

@Hejsil
Copy link
Sponsor Contributor

Hejsil commented Apr 13, 2019

@thejoshwolfe might be a good idea, but i haven't thought much about this. I'm out travelling and will have time to look more into this in a weeks time.

@andrewrk andrewrk added accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. labels Apr 30, 2019
@andrewrk
Copy link
Member

I'm accepting #2235 (comment)

@andrewrk andrewrk changed the title grammar ambiguity with enum literals inside array literals and struct literals solve the grammar ambiguity with enum literals inside array literals and struct literals Apr 30, 2019
@Hejsil
Copy link
Sponsor Contributor

Hejsil commented May 1, 2019

@andrewrk well, if we are gonna have inferred struct initializes as well, then this is not gonna work. Related #208.

@daurnimator
Copy link
Collaborator

@andrewrk well, if we are gonna have inferred struct initializes as well, then this is not gonna work. Related #208.

A better link might be #685 (comment)

@Hejsil
Copy link
Sponsor Contributor

Hejsil commented May 11, 2019

If we add enum literals with this patch:

--- a/grammar/grammar.y
+++ b/grammar/grammar.y
@@ -121,6 +121,7 @@ PrimaryTypeExpr
     <- BUILTINIDENTIFIER FnCallArguments
      / CHAR_LITERAL
      / ContainerDecl
+     / DOT IDENTIFIER
      / ErrorSetDecl
      / FLOAT
      / FnProto

then the peg parser parses []Enum {.Tag} succesfully as an array literal. So this is not a grammar problem, and just an implementation problem with the stage 1 parser. This is therefor a bug.

@Hejsil Hejsil closed this as completed in 6cf7fb1 May 11, 2019
@Hejsil
Copy link
Sponsor Contributor

Hejsil commented May 11, 2019

No ambiguity. This was just a bug in the implementation of the parser. You probably want to implement this in your stage 2 parser PR @hryx :)

@hryx
Copy link
Sponsor Contributor

hryx commented May 12, 2019

'Tis now done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

6 participants