-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Confusing diagnostic from const eval when offsetting a pointer out of bounds #93881
Comments
Locally, I get an improved (but still not the desired, the offsets are weird) message like this now on nightly (nightly-2022-07-23): error[E0080]: evaluation of constant value failed
--> /home/nilsh/projects/rust/library/core/src/ptr/const_ptr.rs:457:18
|
457 | unsafe { intrinsics::offset(self, count) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| out-of-bounds pointer arithmetic: alloc3 has size 1, so pointer to 3 bytes starting at offset 0 is out-of-bounds
| inside `ptr::const_ptr::<impl *const u8>::offset` at /home/nilsh/projects/rust/library/core/src/ptr/const_ptr.rs:457:18
|
::: lol.rs:4:14
|
4 | unsafe { ptr.offset(3) }
| ------------- inside `demo` at lol.rs:4:14
...
7 | const P: *const u8 = demo();
| ------ inside `P` at lol.rs:7:22 But for some reason, there is no error message at all on the playground and it just says error[E0080]: evaluation of constant value failed
|
::: src/lib.rs:4:14
|
4 | unsafe { ptr.offset(3) }
| ------------- inside `demo` at src/lib.rs:4:14
...
7 | pub const P: *const u8 = demo();
| ------ inside `P` at src/lib.rs:7:26 on nightly on the playground. |
Output after #136503:
I believe that that output will be enough to close this ticket. Would you agree? |
I don't even remember filing this issue, and if I could I would emphasize the fact that the panic message itself from const eval is confusing. But the backtrace is quite helpful. |
The pointee type of a raw pointer is irrelevant, and shouldn't affect the error message.
Happy for suggestions for how to improve it, but the wording you suggested would not be correct. Specifically, the byte after the new pointer ("1 byte starting at offset 3") does not even have be be in-bounds. |
The problem is that the diagnostic refers to the trick that the interpreter is using to check this condition, not recognizable attributes of the program. Whether or not the pointee type is relevant to the opsem, it is highly relevant to the user because the behavior of
|
Again, it doesn't matter whether the produced pointer is out-of-bounds. The produced pointer can actually be in-bounds when this error occurs, e.g. if you start with a pointer at offset 10 and add -5 and the allocation has size 8. But I agree the wording is confusing. I just don't want it to become incorrect. :) So what about something like: |
We don't have to use the same error message template for every scenario 😉 |
Given that we have contiguous allocations, the error always implies that either beginning or end pointer are out-of-bounds. So yeah we could use that for the error.
That said, the docs do say: "the entire memory range between self and the result must be in bounds of that allocated object". So it would not be unreasonable to phrase the error in those terms.
|
Given the following code: playground link
The current output is:
Ideally the output should look like:
I originally found this through Miri, in huonw/primal#35, though the diagnostic is generated by rustc so I'm opening an issue here. I spent a while in the original example trying to figure out how a
*const u8
became a pointer to 3 bytes. Reading over the code that implements this diagnostic, it almost looks like some generic pointer out-of-bounds code was repurposed to provide a diagnostic for invalid offsets. I'd implement an improvement myself but I really can't figure out how to get the size of the pointee type.The text was updated successfully, but these errors were encountered: