-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add help for trying to do C-like pointer arithmetics #112261
Add help for trying to do C-like pointer arithmetics #112261
Conversation
r? @eholk (rustbot has picked a reviewer for you, use r? to override) |
@@ -2870,6 +2870,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
); | |||
} | |||
} | |||
|
|||
if let ty::RawPtr(..) = base_t.kind() && idx_t.is_integral() { | |||
err.span_help( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be a span_suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hesistant to make them suggestions because ptr::add
and friends are unsafe functions.
If I were to make it into a suggestion, it would become something like
error[E0608]: cannot index into a value of type `*const u32`
--> tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs:9:13
|
9 | let d = ptr1[5]; //~ ERROR cannot index
| ^^^^^^^ help: consider using `add` for indexing into raw pointer: `unsafe { ptr1.add(5) }`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrapping_add
isn't unsafe. Although C pointer arithmetic does have the same preconditions as add
I think. But I don't think suggestions should include unsafe code. add
should still be mentioned in the help though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrapping_add
isn't unsafe.
Good catch, I'll refer to that instead of add
.
But I don't think suggestions should include unsafe code.
I think I agree with this. I can undo the change to make them suggestions and revert them to being help notes if this is deemed more suitable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can have both a suggestion and a note although having add
in the suggestion text is also fine
compiler/rustc_hir_typeck/src/op.rs
Outdated
if op.node == hir::BinOpKind::Add { | ||
match (lhs_ty.kind(), rhs_ty) { | ||
(RawPtr(..), rhs_ty) if rhs_ty.is_integral() => { | ||
err.span_help( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similarly, and all other usages of span_help
50e4123
to
d09f33d
Compare
d09f33d
to
7ab91e5
Compare
compiler/rustc_hir_typeck/src/op.rs
Outdated
err.span_suggestion( | ||
expr.span, | ||
"consider using `offset_from` for pointer - pointer", | ||
format!("unsafe {{ {lhs_snip}.offset_from({rhs_snip}) }}"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a safe suggestion we could make here? I didn't find one immediately after poking around the docs a little...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find one either...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should at least add "if the pointers point to the same allocation"
7ab91e5
to
4ae70a7
Compare
4ae70a7
to
63d643d
Compare
@bors r+ rollup |
☀️ Test successful - checks-actions |
Finished benchmarking commit (fd0a331): comparison URL. Overall result: ❌✅ regressions and improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 647.92s -> 648.279s (0.06%) |
This PR adds help messages for these cases:
Current Output
Output After This PR
Closes #112252.