Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Urgau
Copy link
Member

@Urgau Urgau commented Mar 30, 2024

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.

pub struct Test {
    data: [u8],
}

pub fn test_len(t: *const Test) -> usize {
    unsafe { (*t).data.len() }  // this calls <[T]>::len(&self)
}

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:

  1. A deref followed by any non-deref place projection (that intermediate deref will typically be auto-inserted)
  2. A method call annotated with #[rustc_no_implicit_refs].
  3. A deref followed by a addr_of! or addr_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?
  • Are "index" and "field" enough?

cc @JakobDegen @WaffleLapkin
r? @RalfJung

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 30, 2024
@rustbot
Copy link
Collaborator

rustbot commented Mar 30, 2024

The Miri subtree was changed

cc @rust-lang/miri

@RalfJung
Copy link
Member

Sorry, I can't take on more reviews currently.
r? compiler
(or feel free to pick someone specific who's suited)

@rustbot rustbot assigned fmease and unassigned RalfJung Mar 30, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as outdated.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 2b3fe45 to 57f6416 Compare May 14, 2024 17:22
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 57f6416 to 824c1f5 Compare May 14, 2024 18:04
/// When working with raw pointers it's usually undesirable to create references,
/// since they inflict a lot of safety requirement. Unfortunately, it's possible
/// to take a reference to a dereference of a raw pointer implicitly, which inflicts
/// the usual reference requirements without you even knowing that.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// the usual reference requirements without you even knowing that.
/// the usual reference requirements potentially without you realizing that.

More neutral, less “condescending” imo.

Comment on lines +9 to +12
help: try using a raw pointer method instead; or if this reference is intentional, make it explicit
|
LL | addr_of!((&(*ptr))[..16])
| ++ +
Copy link
Member

@fmease fmease May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While catching up with the comments posted in the now closed PR, I thought the plan was to avoid suggesting explicit borrows and to instead suggest #[allow]'ing the lint: Sth. like if this reference is intentional make it explicit by adding #[allow(implicit_unsafe_autorefs)].

CC 1st paragraph of #103735 (comment); #103735 (comment); #103735 (comment)

Copy link
Member Author

@Urgau Urgau May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There has been concern from T-lang member about having only #[allow] as an option for lint, cc #118833 (comment) (and maybe a bit #122759 (comment)).

I think it would make sense to ask T-lang about this in the nomination, to see what they want, worst case it's just removing some code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, nothing in #122759 is meant to imply that we shouldn't have lints whose suggestion is #[allow].

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to ask T-lang about this in the nomination, to see what they want, worst case it's just removing some code.

@Urgau how do you want to proceed here, should we ask for T-lang feedback?

Copy link
Member

@fmease fmease Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to nominate this for T-lang in any case -- once I've reviewed this PR ^^' I'll really try to get to it tonight.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @fmease, just wanted to remind you to take a look at this PR if you are still interested. Thanks! 🙂

@bors

This comment was marked as outdated.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 824c1f5 to c2d6e62 Compare May 23, 2024 18:49
@bors

This comment was marked as outdated.

@Dylan-DPC
Copy link
Member

@Urgau if you can rebase the latest conflicts we can push this forward and maybe get it reviewed by another reviewer

@Dylan-DPC Dylan-DPC added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2024
@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from c2d6e62 to 78288af Compare August 12, 2024 15:49
@Urgau
Copy link
Member Author

Urgau commented Aug 12, 2024

@Dylan-DPC rebased.

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 12, 2024
@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 78288af to d060615 Compare October 9, 2024 13:58
@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from afe6661 to 0db3f30 Compare March 3, 2025 18:56
@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Mar 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 3, 2025

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

@bors

This comment was marked as resolved.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 0db3f30 to 6ce9761 Compare March 5, 2025 19:29
@RalfJung
Copy link
Member

RalfJung commented Mar 5, 2025

@Urgau I recommend asking on Zulip to find another reviewer. I would generally recommend doing that after about 2 weeks of silence; it's been half a year since we last heard from @fmease so I think you've been unreasonably patient already. :)

@Urgau Urgau unassigned fmease Mar 6, 2025
@Urgau
Copy link
Member Author

Urgau commented Mar 6, 2025

@rustbot

This comment was marked as resolved.

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Mar 6, 2025

Heya, this looks somewhat interesting and I think I understand most parts involved so I'm willing to review it :)

@jdonszelmann
Copy link
Contributor

It seems like there's still an open comment asking for T-lang feedback here? It's from august, did that ever happen? Not here at least it seems.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 8, 2025
@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 6ce9761 to 1e9582a Compare March 8, 2025 20:27
@Urgau
Copy link
Member Author

Urgau commented Mar 8, 2025

It seems like there's still an open comment asking for T-lang feedback here? It's from august, did that ever happen? Not here at least it seems.

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.

@Urgau Urgau added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 8, 2025
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from 1e9582a to ab6f4af Compare March 8, 2025 20:40
@jdonszelmann
Copy link
Contributor

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.

@Urgau Urgau force-pushed the dangerous_implicit_autorefs branch from ab6f4af to 148db48 Compare March 9, 2025 11:49

/// Peel derefs adjustments
fn peel_derefs_adjustments<'a>(mut adjs: &'a [Adjustment<'a>]) -> &'a [Adjustment<'a>] {
while let [Adjustment { kind: Adjust::Deref(None), .. }, end @ ..] = adjs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure asserting None here is correct? I wrote some tests and can't find an example going wrong but I'm not 100% sure to be honest when this would be Some.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check the Some() case in has_implicit_borrow so we can't peel it otherwise we will miss it in has_implicit_borrow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yea that makes sense!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could it ever happen that there are multiple Some cases in a row before a deref of a raw pointer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(since has_implicit_borrow only strips one layer)

@bors
Copy link
Contributor

bors commented Mar 9, 2025

☔ The latest upstream changes (presumably #138267) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.