-
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
Implement a lint for implicit autoref of raw pointer dereference - take 2 #123239
base: master
Are you sure you want to change the base?
Conversation
The Miri subtree was changed cc @rust-lang/miri |
Sorry, I can't take on more reviews currently. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bb6ab41
to
2b3fe45
Compare
This comment was marked as outdated.
This comment was marked as outdated.
2b3fe45
to
57f6416
Compare
This comment has been minimized.
This comment has been minimized.
57f6416
to
824c1f5
Compare
This comment was marked as outdated.
This comment was marked as outdated.
824c1f5
to
c2d6e62
Compare
This comment was marked as outdated.
This comment was marked as outdated.
@Urgau if you can rebase the latest conflicts we can push this forward and maybe get it reviewed by another reviewer |
c2d6e62
to
78288af
Compare
@Dylan-DPC rebased. @rustbot review |
78288af
to
d060615
Compare
This comment has been minimized.
This comment has been minimized.
6ce9761
to
1e9582a
Compare
Not yet. That will be a question for the T-lang nomination, which will be after the logic in this PR is checked to be correct-ish. |
This comment has been minimized.
This comment has been minimized.
1e9582a
to
ab6f4af
Compare
Hmm, okay. I'll give it one more look but I'm reasonably confident this now at least implements the logic from the linked comment. |
ab6f4af
to
148db48
Compare
This comment was marked as resolved.
This comment was marked as resolved.
f7d2ca2
to
294d9d7
Compare
|
||
/// Peel derefs adjustments until the last last element. | ||
fn peel_derefs_adjustments<'a>(mut adjs: &'a [Adjustment<'a>]) -> &'a [Adjustment<'a>] { | ||
while let [Adjustment { kind: Adjust::Deref(_), .. }, end @ ..] = adjs |
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 could add an underscore to the pattern to represent the !is_empty
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'm not following. _
mean "exactly one", but we want "one or more" so end @ _
won't work, and since we need to keep the ..
(to get a slice) I don't see another way but to use !adjs.is_empty()
.
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.
apart from that, the logic looks about right to me! let's ask for T-lang?
This comment was marked as resolved.
This comment was marked as resolved.
294d9d7
to
653c98b
Compare
Dear @rust-lang/lang (and @rust-lang/lang-advisors), this PR is implementing a lint Examples and algorithm: #103735 (comment) The goal is to catch cases like this, where the user probably doesn't realise it just created a reference. pub struct Test {
data: [u8],
}
pub fn test_len(t: *const Test) -> usize {
unsafe { (*t).data.len() } // this calls <[T]>::len(&self)
} #103735 (the previous attempt) already went through you 2 times (#103735 (comment) and #103735 (comment)) which prompted this summary and suggestions from You can see in One difference from Jakob suggestion is that this PR suggestion is to add explicit @rustbot labels +I-lang-nominated +S-waiting-on-team -S-waiting-on-review -T-libs -T-compiler |
compiler/rustc_lint/src/autorefs.rs
Outdated
/// ```rust | ||
/// #![feature(slice_ptr_get)] | ||
/// | ||
/// unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] { | ||
/// ptr.get_unchecked_mut(..16) | ||
/// } | ||
/// ``` |
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.
Surely we shouldn't be suggesting to people to turn on nightly features as a replacement for what we're linting against.
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.
Switch this example to slice::from_raw_parts_mut
, but more generally we don't always have a great story around raw pointers (as can be seen by the slice_ptr_get
feature being nightly-only).
653c98b
to
101c9b8
Compare
This PR aims at implementing a lint for implicit autoref of raw pointer dereference, it is based on #103735 with suggestion and improvements from #103735 (comment).
The goal is to catch cases like this, where the user probably doesn't realise it just created a reference.
Since #103735 already went 2 times through T-lang, where they T-lang ended-up asking for a more restricted version (which is what this PR does), I would prefer this PR to be reviewed first before re-nominating it for T-lang.
Compared to the PR it is as based on, this PR adds 3 restrictions on the outer most expression, which must either be:
#[rustc_no_implicit_refs]
.addr_of!
oraddr_of_mut!
. See bottom of post for details.There are several points that are not 100% clear to me when implementing the modifications:
"4. Any number of automatically inserted deref/derefmut calls." I as never able to trigger this. Am I missing something?Fixedcc @JakobDegen @WaffleLapkin
r? @RalfJung