Improve 'slice of single-item pointer' error message - #14673
Conversation
|
Fixed up to only emit note when the user has typed |
| const start_val = try sema.resolveDefinedValue(block, start_src, uncasted_start) orelse { | ||
| break :emit_note false; | ||
| }; | ||
| const start = start_val.toUnsignedInt(sema.mod.getTarget()); |
There was a problem hiding this comment.
The types of the the start and end values have not yet been validated at this point so it is not safe to use toUnsignedInt.
There was a problem hiding this comment.
Thanks. What is the proper way to validate and handle invalid values here?
There was a problem hiding this comment.
Calling getUnsignedIntAdvanced will give you an optional int that will be null if it can't fit in a u64 which should be enough for this, just check that it's not undefined first.
There was a problem hiding this comment.
Thanks! I've made changes. Have I understood correctly?
Offer a correct explicit coercion syntax to help the user avoid other footgunnable workarounds when they encounter this error
There was a problem hiding this comment.
Inspired by this PR, I decided to make the language allow slicing a single-item pointer from [0..1] if the start and end are comptime-known: #16075
I believe this will make the compile error note much more straightforward to implement, and give better advice.
Let us move this compile error enhancement effort into the implementation for that proposal.
| // target=native | ||
| // | ||
| // :2:22: error: slice of single-item pointer | ||
| // :2:22: note: single-item pointer can be coerced to array using '@as(*[1]i32, ptr)' |
There was a problem hiding this comment.
*[1]i32 is not an array, it is a pointer to an array. Compile errors must be pedantically precise.
Offer a correct explicit coercion syntax to help the user avoid other footgunnable workarounds when they encounter this error.
Arguably, there's no need for this if we allowed explicit coercion using slice syntax [0..] or similar (also see #8197 and #3156) but this provides immediate value to users today per the status quo.