-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Use attributes for dangling_pointers_from_temporaries
lint
#132732
Conversation
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 (
|
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.
Could you also please add test cases for the newly marked methods to tests/ui/lint/dangling-pointers-from-temporaries/types.rs
?
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.
@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?
|
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 |
|
compiler/rustc_lint/src/dangling.rs
Outdated
@@ -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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
The doc comment on lines 46–48
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>
0d54723
to
24cc924
Compare
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, |
(Back ping to #132281) |
@gavincrawford Is the PR ready for review? (I ask because it's still marked as waiting on author, do @ |
Yes, sorry. @rustbot review |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we still need to take the derefs into account.
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.
LGTM. Let's fix the nit #132732 (comment) and defer the simplification to a follow-up PR. @rustbot author |
97e1b82
to
01fd384
Compare
Just fixed that comment. I squashed it with the other comment commit for clarity, as they're both just comment edits. |
@bors r+ |
Rollup of 5 pull requests Successful merges: - rust-lang#132732 (Use attributes for `dangling_pointers_from_temporaries` lint) - rust-lang#133108 (lints_that_dont_need_to_run: never skip future-compat-reported lints) - rust-lang#133190 (CI: use free runner in dist-aarch64-msvc) - rust-lang#133196 (Make rustc --explain compatible with BusyBox less) - rust-lang#133216 (Implement `~const Fn` trait goal in the new solver) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#132732 - gavincrawford:as_ptr_attribute, r=Urgau Use attributes for `dangling_pointers_from_temporaries` lint Checking for dangling pointers by function name isn't ideal, and leaves out certain pointer-returning methods that don't follow the `as_ptr` naming convention. Using an attribute for this lint cleans things up and allows more thorough coverage of other methods, such as `UnsafeCell::get()`.
Checking for dangling pointers by function name isn't ideal, and leaves out certain pointer-returning methods that don't follow the
as_ptr
naming convention. Using an attribute for this lint cleans things up and allows more thorough coverage of other methods, such asUnsafeCell::get()
.