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

feat(compiler): Add warning for non-integer array indices #2077

Merged
merged 3 commits into from
Mar 28, 2024

Conversation

spotandjake
Copy link
Member

This pr adds a basic warning for arr[1.5] and arr[1.5] = 1 along with rational use cases. The purpose of this is to better catch instances where a user is indexing with floats ahead of time and prevent more runtime bugs. You can still achieve the runtime Index not an integer error by doing something like let i = 1.5; arr[i] though. The biggest place thing this helps to prevent is arr[1.0] where a user might assume we cast that to an int like number.

@ospencer
Copy link
Member

Because our number system does allow users to end up in situations where they might try to index by a non-integer, I don't think this should be a well-formedness error, but rather a warning. Making arr[1.0] fail to compile introduces some confusion about what the compiler is going to guarantee you.

@spotandjake
Copy link
Member Author

Because our number system does allow users to end up in situations where they might try to index by a non-integer, I don't think this should be a well-formedness error, but rather a warning. Making arr[1.0] fail to compile introduces some confusion about what the compiler is going to guarantee you.

do we currently support raising warnings in normal well_formdness? I can't see this functionality anywhere or would this need to be implemented?

@ospencer
Copy link
Member

It's supported, we just don't do it. You can see how to do it in typed_well_formedness.re: https://github.com/grain-lang/grain/blob/main/compiler/src/typed/typed_well_formedness.re#L270-L278

@spotandjake
Copy link
Member Author

Made that change.

@ospencer ospencer changed the title feat(compiler): Add well formedness errors for non float array index's feat(compiler): Add warning for non-integer array indices Mar 27, 2024
Co-authored-by: Oscar Spencer <oscar.spen@gmail.com>
@ospencer ospencer added this pull request to the merge queue Mar 28, 2024
Merged via the queue into grain-lang:main with commit 520dd8a Mar 28, 2024
12 checks passed
@github-actions github-actions bot mentioned this pull request Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants