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

Emit clear error for index into zero-sized Arrays #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JulianKnodt
Copy link

@JulianKnodt JulianKnodt commented Dec 8, 2024

Previously GEP on zero-sized arrays, would fail. This change fixes arrays to instead emit runtime arrays. I do not know if this will lead to any runtime cost, but it fixes all the compile errors.

I did try out [(); 0].map(|()| 1), and found that that does not compile.

Fixes #151

Currently [T; 0] = T still compiles, will need to file some follow up issues

@LegNeato
Copy link
Collaborator

LegNeato commented Dec 8, 2024

Awesome, thanks for the PR! 🍻 I'm not super familiar with this code but the change looks reasonable to me. I'll wait for some other maintainers to look at it before reviewing 🤙

Looks like you need to run cargo fmt to pass CI.

@JulianKnodt
Copy link
Author

Somehow this passes locally but not on CI 🤣, I'll have to test a bit more

@JulianKnodt JulianKnodt changed the title Fix zero-sized Array GEPs Emit clear for index into zero-sized Arrays Dec 11, 2024
@JulianKnodt
Copy link
Author

JulianKnodt commented Dec 11, 2024

I've changed my expectation, previously it was erroring for accessing zero sized arrays, but it was hidden behind a deny that is on by default. Instead, I think we should always panic, but with a clear error message. I'm unsure what the behavior would be if that expression were allowed to be compiled.

@JulianKnodt JulianKnodt changed the title Emit clear for index into zero-sized Arrays Emit clear error for index into zero-sized Arrays Dec 11, 2024
@LegNeato
Copy link
Collaborator

Can we make the error messages match rustc? For example:

error[E0080]: evaluation of constant value failed
 --> src/main.rs:2:18
  |
2 | const VAL: u32 = ARR[0];
  |                  ^^^^^^ index out of bounds: the length is 0 but the index is 0

For more information about this error, try `rustc --explain E0080`.

@JulianKnodt
Copy link
Author

I can, I do worry that since it's not the actual error it may be more confusing

Copy link
Collaborator

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

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

Sorry, for the error messages I meant matching the same case in rustc to the case here where it makes sense. I'll leave it up to you to change them so they make sense!

A couple of small comments, thanks again for the PR and sorry for the slow response!

#[spirv(compute(threads(1, 1, 1)))]
pub fn compute() {
let mut array = [0; 0];
// for some reason this now compiles?
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should look into this case, no?

Copy link
Author

Choose a reason for hiding this comment

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

yea, I want to see if it compiles to include a panic, but I'm unsure of how to run the rest to see what happens

Is it run automatically by compiletest

Copy link
Collaborator

@LegNeato LegNeato Dec 20, 2024

Choose a reason for hiding this comment

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

No, sadly compiletest just checks it compiles. We don't have any automated runtime tests right now sadly. I usually just drop the code in one of the examples when I am doing a quick manual test.

Copy link
Author

Choose a reason for hiding this comment

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

I've checked the output spirv assembly for that example (by adding the compile flags), and it seems that the assignment is getting optimized out somehow. I checked all the spirv builder methods, and none of them seem to be hit. I've found that any reads from the array hit gep and fail, but stores do not. I feel like ideally that should give a runtime panic, but I'm not entirely sure where to inject it.

While investigating this, I also noticed that zst arrays as input parameters do not work.&mut [T; 0] also gives the old cryptic error message. I'm not sure if I should fix that in this PR, or another

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the store behavior changed by this PR? If it is unchanged I'd say just leave it and file a followup task.

Feel free to fix the input message in this PR if you want.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if this is answered or ready to review, ping if so!

Copy link
Author

Choose a reason for hiding this comment

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

Oops I forgot to reply because of the holidays, I checked but the behavior isn't changed, this previously also compiled on main

tests/ui/lang/core/array/oob_0_array.rs Show resolved Hide resolved
Previously GEP on zero-sized arrays, would fail. This change fixes arrays to instead emit
runtime arrays. I do not know if this will lead to any runtime cost, but it fixes all the
compile errors.
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.

GEP not implemented for type struct [_; 0]
2 participants