Use attributes for dangling_pointers_from_temporaries lint - #132732
Conversation
commented
Nov 7, 2024
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Urgau (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
|
Other than the 3 points above, LGTM (after the CI is green). But maybe Urgau has something add too. (Were they assigned randomly by the way? That's nice) Attribute validation turned out to be way simpler than I expected, which is a pleasant surprise. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
commented
Nov 8, 2024
|
@gavincrawford Could you add the missing As well as doing @GrigorenkoPV comment:
To locally run the tests you can use @rustbot author |
|
Fixed the slice methods, working on the test cases, and had a quick question. In order for the lint to apply to rust/compiler/rustc_lint/src/dangling.rs Lines 202 to 223 in 328b759 Which works fine, and causes the test to error out on UnsafeCell::get() as expected, but I'm struggling to find how to apply the same logic to SyncUnsafeCell. As I understand it, the synchronous version is just a wrapper around a normal UnsafeCell, and thus owns its inner allocation, but it does not have a definition that I can find in LangItem, nor a diagnostic name I can match against. Any ideas on how to handle this as of now?
|
commented
Nov 9, 2024
I think you can make it into a new diagnostic item. You will need to mark the struct & add the new tag to a list of symbols. Also you can probably remove |
commented
Nov 11, 2024
|
|
| @@ -201,22 +202,23 @@ fn is_temporary_rvalue(expr: &Expr<'_>) -> bool { | |||
|
|
|||
| // Array, Vec, String, CString, MaybeUninit, Cell, Box<[_]>, Box<str>, Box<CStr>, | |||
There was a problem hiding this comment.
Please, update this comment and, more importantly, the doc comment above that mentions that [Sync]UnsafeCell::get() methods are not accounted for.
Almost forgot about this, sorry.
Okay, makes sense. I wonder where, probably will look at this later, as it sounds interesting. UPD: nothing interesting, as it turns out, just some clippy usages, and they don't look like they can be improved by using the new attribute instead
Probably some But other than what I've already mentioned, LGTM. Once again, we'll hear from Urgau first (I do not have rights to approve PRs anyway), |
… names Co-authored-by: Urgau <3616612+Urgau@users.noreply.github.com>
It's used in a couple of Clippy lints, didn't look much more in to it then that.
Yeah, I noticed the mess. Rebased and squashed the commits down. As for the other stuff, |
commented
Nov 14, 2024
|
(Back ping to #132281) |
|
@gavincrawford Is the PR ready for review? (I ask because it's still marked as waiting on author, do @ |
| fn is_interesting(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool { | ||
| // Array, Vec, String, CString, MaybeUninit, Cell, Box<[_]>, Box<str>, Box<CStr>, UnsafeCell, | ||
| // SyncUnsafeCell, or any of the above in arbitrary many nested Box'es. | ||
| fn owns_allocation(tcx: TyCtxt<'_>, ty: Ty<'_>) -> bool { |
There was a problem hiding this comment.
With the addition of the attribute we should able to simplify this owns_allocation method to a simple check of the type and it's generic arguments.
Something like this would be a good starting point (the check should probably be further refined):
match ty.kind() {
ty::Array(_, _) => true,
ty::Adt(_def, args) => args.iter().filter_map(|g| g.as_type()).all(|ty| !ty.is_any_ptr()),
_ => false,
}There was a problem hiding this comment.
I think a Box<Box<&[T]>> would pass the check that you suggest, even though it should not
There was a problem hiding this comment.
Yeah, we still need to take the derefs into account.
Details
Probably something more like this:
let adjs = cx.typeck_results().expr_adjustments(receiver);
let origin_ty = cx.typeck_results().expr_ty(receiver);
adjs.iter()
.filter_map(|adj| match adj.kind {
Adjust::Deref(_) => Some(adj.target),
Adjust::NeverToAny
| Adjust::Borrow(_)
| Adjust::Pointer(_)
| Adjust::ReborrowPin(_) => None,
})
.all(|ty| !ty.is_any_ptr())
&& match origin_ty.kind() {
ty::Adt(_def, args) => {
args.iter().filter_map(|g| g.as_type()).all(|ty| !ty.is_any_ptr())
}
_ => !origin_ty.is_any_ptr(),
}but, let's discussed that on the follow-up issue/pr.
commented
Nov 16, 2024
|
LGTM. Let's fix the nit #132732 (comment) and defer the simplification to a follow-up PR. @rustbot author |
commented
Nov 19, 2024
|
Just fixed that comment. I squashed it with the other comment commit for clarity, as they're both just comment edits. |
commented
Nov 19, 2024
|
@bors r+ |
commented
Nov 19, 2024
commented
Nov 26, 2024
|
This PR seems to have caused a compile-time regression in the |
Checking for dangling pointers by function name isn't ideal, and leaves out certain pointer-returning methods that don't follow the
as_ptrnaming convention. Using an attribute for this lint cleans things up and allows more thorough coverage of other methods, such asUnsafeCell::get().