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

Can't use const value for fixed array that is a struct's field if const was declared below struct declaration #19593

Closed
ArtemkaKun opened this issue Oct 18, 2023 · 4 comments · Fixed by #19893
Labels
Bug This tag is applied to issues which reports bugs. Build V build error on any OS/CPU architecture. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@ArtemkaKun
Copy link
Contributor

ArtemkaKun commented Oct 18, 2023

Describe the bug

If you have a struct with a field of type of fixed array, you must provide the size of the array. If you want to use the const value for this, and this const is declared below the declaration of this struct, the compiler will output an error.

Reproduction Steps

struct Test {
	posts [max_posts_count]int
}

const max_posts_count = 5

Expected Behavior

Compiled successfully

Current Behavior

code.v:2:9: error: non-constant array bound `max_posts_count`
    1 | struct Test {
    2 |     posts [max_posts_count]int
      |            ~~~~~~~~~~~~~~~
    3 | }
    4 |

Possible Solution

const max_posts_count = 5

struct Test {
	posts [max_posts_count]int
}

Additional Information/Context

No response

V version

V 0.4.2 fae46a8

Environment details (OS name and version, etc.)

Playground

Note

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@ArtemkaKun ArtemkaKun added Bug This tag is applied to issues which reports bugs. Unit: Type System Bugs/feature requests, that are related to the V types system. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Build V build error on any OS/CPU architecture. labels Oct 18, 2023
@Delta456
Copy link
Member

I feel this is some parse issue.

@yuyi98
Copy link
Member

yuyi98 commented Oct 31, 2023

It is recommended that const must be defined before use so that the complexity of processing is avoided.

@medvednikov
Copy link
Member

In V the order shouldn't matter. It's a bug in the const evaluation phase.

@ttytm
Copy link
Member

ttytm commented Oct 31, 2023

Agree @medvednikov. Constants also span files, which makes it hard to tell which definition in which file has taken precedence before its constant is used in another file in the module.

Working on this might also be a step towards fixing #19149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Build V build error on any OS/CPU architecture. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants