Skip to content

Conversation

@chqrlie
Copy link
Contributor

@chqrlie chqrlie commented Dec 10, 2025

  • arrays defined with an enum type as the length must be indexed using enum values of this type
  • enum values are scoped in array subscript expressions and initializers

Implements #403

printf("%d\n", a[Enum.max]);
i32 i = 1;
i32[] aa = {
printf("%d\n", a[No + 1]), // @error{use of undeclared identifier 'No'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could change the diagnostic 'use of undeclared identifier' to 'array subscript must have enum type 'test.Enum'' also? It's weird that a[No] works and a[No +1] says that No is unknown..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the diagnostic is confusing but this problem is not new: Enum e = No + 1; has the same problem.

To solve this, we would need to add the enum at the top of the scope stack so the expression can be resolved. Yet doing so it not completely correct as No would then resolve even in complex expressions where it is possibly ambiguous and inappropriate: Enum e = some_function_that_takes_another_enum(No);

* arrays defined with an enum type as the length must be indexed using
  enum values of this type
* enum values are scoped in array subscript expressions and initializers

Implements c2lang#403
@bvdberg
Copy link
Member

bvdberg commented Dec 11, 2025

I think the number of tests are a bit limited. I can think of many more:

u32[Enum] b = { 10, 20, 30 }

static_assert(3, sizeof(char[Enum]));

in function:
b[Enum.Yes] = 10;

inside a struct:
type Struct struct {
char[Enum] cols;
}

as type:
type Foo char[Enum];
public type Bar char[Enum]; // @error{public declaration using non-public variable 'test.Color'}

In function scope:
Struct s;
s.cols[(Enum)10] = 20;
-> error: array out-of-bounds access [10] in array of [3]
seems a good error, but good to know that that keeps working in unit test

multi-dimensional, both one/both dimensions the Enum.
Pointer to Enum: u32[Enum*] ..

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants