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

patterns: reject raw pointers that are not just integers #116930

Merged
merged 5 commits into from Nov 8, 2023

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Oct 19, 2023

Matching against 0 as *const i32 is fine, matching against &42 as *const i32 is not.

This extends the existing check against function pointers and wide pointers: we now uniformly reject all these pointer types during valtree construction, and then later lint because of that. See here for some more explanation and context.

Also fixes #116929.

Cc @oli-obk @lcnr

@rustbot
Copy link
Collaborator

rustbot commented Oct 19, 2023

r? @davidtwco

(rustbot has picked a reviewer for you, use r? to override)

@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. labels Oct 19, 2023
pub POINTER_STRUCTURAL_MATCH,
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.

I didn't realize this lint is still allow-by-default oO. Seems high time we make it warn-by-default?

@RalfJung
Copy link
Member Author

The 2nd commit makes this forbid-by-default so we can crater it.
@bors try

@bors
Copy link
Contributor

bors commented Oct 19, 2023

⌛ Trying commit 0787e62 with merge ebb1189...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 19, 2023
patterns: reject raw pointers that are not just integers

Matching against `0 as *const i32` is fine, matching against `&42 as *const i32` is not.

Cc `@oli-obk` `@lcnr`
@RalfJung
Copy link
Member Author

Due to #116929 this lint still has a big gap, it'll only find bad raw pointers "at the root".

I wonder if with this change to valtree construction, we can make it so that some lint always fires when a const is used as a pattern and valtree construction fails? I do think those cases should all become hard errors eventually...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Oct 19, 2023

☀️ Try build successful - checks-actions
Build commit: ebb1189 (ebb118907fa893a392c699ddf120848fa0a8d6c2)

@RalfJung
Copy link
Member Author

Let's see how much fallout we're getting from this weak form of the lint.
@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-116930 created and queued.
🤖 Automatically detected try build ebb1189
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 19, 2023
@rustbot
Copy link
Collaborator

rustbot commented Oct 19, 2023

Some changes might have occurred in exhaustiveness checking

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member Author

Lol, the doctest for StructuralEq uses a function pointer as a bad example.^^

@@ -247,6 +247,7 @@ marker_impls! {
///
/// const CFN: Wrap<fn(&())> = Wrap(higher_order);
///
/// #[allow(pointer_structural_match)]
Copy link
Member Author

Choose a reason for hiding this comment

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

FWIW these entire trait docs are outdated; that's tracked in #115881.

@craterbot
Copy link
Collaborator

🚧 Experiment pr-116930 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-116930 is completed!
📊 876 regressed and 3 fixed (379825 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Oct 22, 2023
@RalfJung
Copy link
Member Author

@craterbot

This comment was marked as outdated.

@craterbot craterbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 22, 2023
@RalfJung
Copy link
Member Author

Those seem to be 3 legit regressions:

  • this matched on a fn ptr (so already triggered the lint before this PR, but the lint was allow-by-default)
  • same here
  • and same here

In particular, none of them is matching on a raw pointer that was not derived from an integer. So IMO we should land this PR to make sure people don't start doing that.

@RalfJung RalfJung added the I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. label Oct 30, 2023
@RalfJung
Copy link
Member Author

RalfJung commented Oct 30, 2023

@rust-lang/lang this PR does three things:

  • it fixes our old lint that detects match on function pointers and wide pointers to also catch cases where those pointers are hidden inside other types
  • it makes that lint warn-by-default (but doesn't yet make it show up in dependencies)
  • it makes the lint also trigger when encountering a raw pointer that was not constructed via int as *const/mut T, but e.g. via &something as *const T -- those allocations don't have guarantees on their ptr equality just like function pointers and vtables, so we should reject them for the same reason.

We haven't reached a proper conclusion in the recent "match on const" meeting, but the general direction seems to have been towards "it should still work like a pattern" (and not like sugar for ==), and this PR is a crucial step towards ensuring that they do indeed behave like a pattern, with structural properties and all that. Even if the goal is just to future-proof against both options (as has been the strategy so far), we want this warning.

Crater found only 3 cases where the warning triggers, in the entire ecosystem. All of them would already warn on stable if they enabled the lint; the new cases covered by this PR were not detected at all by crater.

@RalfJung RalfJung added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Oct 30, 2023
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Implementation changes look good to me, r=me after decisions from t-lang.

);
}
_ => {}
if !have_valtree {
Copy link
Member

Choose a reason for hiding this comment

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

nit: could be part of the condition on the previous line

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 8, 2023

I'm in favor of this. (I also have thoughts on the semantics of matching on constants -- tl;dr I think I'm ready to accept "match is not an extensible mechanism" and hence will not act the same as == so we can stop talking about it -- but those are best discussed elsewhere, I assume)

@WaffleLapkin
Copy link
Member

T-lang meeting consensus: let's do this, no FCP needed.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 8, 2023

@bors r=davidtwco

@bors
Copy link
Contributor

bors commented Nov 8, 2023

📌 Commit 3058865 has been approved by davidtwco

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Nov 8, 2023
@bors
Copy link
Contributor

bors commented Nov 8, 2023

⌛ Testing commit 3058865 with merge fdaaaf9...

@bors
Copy link
Contributor

bors commented Nov 8, 2023

☀️ Test successful - checks-actions
Approved by: davidtwco
Pushing fdaaaf9 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 8, 2023
@bors bors merged commit fdaaaf9 into rust-lang:master Nov 8, 2023
12 checks passed
@rustbot rustbot added this to the 1.75.0 milestone Nov 8, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (fdaaaf9): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This 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.

mean range count
Regressions ❌
(primary)
0.6% [0.4%, 1.4%] 5
Regressions ❌
(secondary)
3.6% [3.6%, 3.6%] 1
Improvements ✅
(primary)
-0.7% [-1.1%, -0.5%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-1.1%, 1.4%] 12

Cycles

Results

This 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.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.5%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.4%, 0.5%] 3

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 662.394s -> 663.159s (0.12%)
Artifact size: 308.74 MiB -> 308.75 MiB (0.00%)

@RalfJung RalfJung deleted the raw-ptr-match branch November 11, 2023 09:58
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Nov 15, 2023
patterns: reject raw pointers that are not just integers

Matching against `0 as *const i32` is fine, matching against `&42 as *const i32` is not.

This extends the existing check against function pointers and wide pointers: we now uniformly reject all these pointer types during valtree construction, and then later lint because of that. See [here](rust-lang/rust#116930 (comment)) for some more explanation and context.

Also fixes rust-lang/rust#116929.

Cc `@oli-obk` `@lcnr`
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Apr 7, 2024
patterns: reject raw pointers that are not just integers

Matching against `0 as *const i32` is fine, matching against `&42 as *const i32` is not.

This extends the existing check against function pointers and wide pointers: we now uniformly reject all these pointer types during valtree construction, and then later lint because of that. See [here](rust-lang/rust#116930 (comment)) for some more explanation and context.

Also fixes rust-lang/rust#116929.

Cc `@oli-obk` `@lcnr`
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
patterns: reject raw pointers that are not just integers

Matching against `0 as *const i32` is fine, matching against `&42 as *const i32` is not.

This extends the existing check against function pointers and wide pointers: we now uniformly reject all these pointer types during valtree construction, and then later lint because of that. See [here](rust-lang/rust#116930 (comment)) for some more explanation and context.

Also fixes rust-lang/rust#116929.

Cc `@oli-obk` `@lcnr`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-lang-nominated The issue / PR has been nominated for discussion during a lang team meeting. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

We're not warning against fn ptr and wide raw ptr nested inside ADTs
9 participants