Skip to content

Introduce a no-op PlaceMention statement for let _ =.#102256

Merged
bors merged 9 commits intorust-lang:masterfrom
cjgillot:let-under
Mar 10, 2023
Merged

Introduce a no-op PlaceMention statement for let _ =.#102256
bors merged 9 commits intorust-lang:masterfrom
cjgillot:let-under

Conversation

@cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Sep 25, 2022

Fixes #54003
Fixes #80059
Split from #101500

This PR introduces a new PlaceMention statement dedicated to matches that neither introduce bindings nor ascribe types. Without this, all traces of the match would vanish from MIR, making it impossible to diagnose unsafety or use in #101500.

This allows to mark let _ = <unsafe union access or dereference> as requiring an unsafe block.
Nominating for lang team, as this introduces an extra error.

@cjgillot cjgillot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Sep 25, 2022
@rust-highfive
Copy link
Contributor

r? @TaKO8Ki

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot
Copy link
Collaborator

rustbot commented Sep 25, 2022

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 25, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 25, 2022
@lukas-code
Copy link
Member

Does this also fix #80059?

@cjgillot
Copy link
Contributor Author

@lukas-code, yes, I added a test.

@RalfJung
Copy link
Member

This sounds great!

For Miri's rust-lang/miri#2360 it'd be awesome if those FakeRead could stick around in MIR a bit longer than they currently do (i.e., the pass that removes them should not be run with -Zmir-opt-level=0). Then we can also detect UB from incorrect union accesses / derefs for a _ place. (But if it's better to do that in a future PR, that's also fine.)

@JakobDegen
Copy link
Contributor

We currently spec FakeRead as being a semantic nop; we could make an exception for the ForWildcard case, but it might be cleaner to have a new StatementKind::PlaceComputation or something like that

@cjgillot
Copy link
Contributor Author

I made this a FakeRead because that's was the easiest. If a dedicated statement is better, I can add one. How should it be named?

@cjgillot
Copy link
Contributor Author

We currently spec FakeRead as being a semantic nop; we could make an exception for the ForWildcard case, but it might be cleaner to have a new StatementKind::PlaceComputation or something like that

@JakobDegen I don't see the issue. FakeRead is an analysis helper for borrowck and unsafeck, and this PR uses it like this.

@JakobDegen
Copy link
Contributor

JakobDegen commented Sep 25, 2022

I don't see the issue. FakeRead is an analysis helper for borrowck and unsafeck, and this PR uses it like this.

Sorry, that was in response to Ralf's comment about rust-lang/miri#2360 . For the purposes in this PR what you have is completely fine (in other words, if we wanted to use this to fix the miri issue it would stop being an analysis helper)

@est31
Copy link
Member

est31 commented Sep 27, 2022

I've tried the example in #80059 for destructuring assignments to _ (so I removed the let to have fn foo(ptr: *const bool) { _ = *ptr; }), and there are similarly no errors. Does the PR fix the situation there too?

@cjgillot cjgillot force-pushed the let-under branch 2 times, most recently from dcbbb69 to 6fe81fe Compare September 27, 2022 17:50
@cjgillot
Copy link
Contributor Author

@est31 it does. Added to the test.

@est31
Copy link
Member

est31 commented Sep 28, 2022

@cjgillot thanks! Would it be useful to add it to the test for #54003 as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@est31 this is the test for #54003.

Copy link
Member

Choose a reason for hiding this comment

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

I meant one with _ = only, without a let.

@pnkfelix
Copy link
Contributor

pnkfelix commented Oct 6, 2022

We talked about this in the T-lang meeting.

The main concern, from the language side, was that we had carefully calibrated a boundary for what kinds of things should be considered "syntactic checks" (checking for missing unsafe { ... } is an instance of that), vs "flow analysis" (checking that your variable is initialized is an instance of that).
So that leads to an assumption that unsafeck should "naturally" be performed on something that reflects the structure of the AST, like THIR.

So, there's an architectural question for T-compiler: I believe this PR is adding something useful today, and so I suspect we should land it.

But: Do we think there's any chance we'll resurrect #99379, or otherwise try to do unsafeck on THIR, in the near term? I would like an answer from T-compiler about that before moving forward on this PR.

@rustbot label I-compiler-nominated

@rustbot rustbot added the I-compiler-nominated Nominated for discussion during a compiler team meeting. label Oct 6, 2022
@cjgillot
Copy link
Contributor Author

cjgillot commented Oct 6, 2022

When I originally wrote this commit, I did not intend it to be useful for unsafeck. My focus was #101500, which is "flow analysis".

While I agree with the syntactic/dataflow separation between THIR and MIR, I still think that this PR is useful:

@RalfJung
Copy link
Member

RalfJung commented Oct 7, 2022

But: Do we think there's any chance we'll resurrect #99379, or otherwise try to do unsafeck on THIR, in the near term? I would like an answer from T-compiler about that before moving forward on this PR.

If we do, then this PR just means MIR and THIR unsafety checking are more in line with each other, right? I don't quite understand what the concern is here.

@RalfJung
Copy link
Member

RalfJung commented Oct 7, 2022

While I agree with the syntactic/dataflow separation between THIR and MIR, I still think that this PR is useful:

I think this PR is crucial since it is a first step towards resolving rust-lang/miri#2360.

@RalfJung
Copy link
Member

RalfJung commented Oct 7, 2022

Btw, this will also fix #79735, won't it?

@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

cjgillot commented Oct 8, 2022

Btw, this will also fix #79735, won't it?

It should. I'll test it when I get my laptop working again.

A bit more problematic: this PR also introduces an error in this snippet, which currently passes borrow checking.

fn foo(mut n: Option<usize>) {
    let _ = if let Some(ref mut s) = n {
        s
    } else {
        &mut 0
        //~^ ERROR temporary value dropped while borrowed
    };
}

This error already happens for let _a = and match { ... } { _ => {} }. Should the error be kept or worked around?

@cjgillot
Copy link
Contributor Author

cjgillot commented Mar 9, 2023

Updated coverage info.
@bors r=lcnr

@bors
Copy link
Collaborator

bors commented Mar 9, 2023

📌 Commit 684de04 has been approved by lcnr

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Mar 9, 2023

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

@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. labels Mar 9, 2023
@bors
Copy link
Collaborator

bors commented Mar 10, 2023

⌛ Testing commit 684de04 with merge d583342...

@bors
Copy link
Collaborator

bors commented Mar 10, 2023

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing d583342 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 10, 2023
@bors bors merged commit d583342 into rust-lang:master Mar 10, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 10, 2023
@cjgillot cjgillot deleted the let-under branch March 10, 2023 14:49
This was referenced Mar 10, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d583342): 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
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.3%, -2.1%] 2
All ❌✅ (primary) - - 0

Cycles

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

@rust-log-analyzer

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. relnotes Marks issues that should be documented in the release notes of the next release. 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.

Unsafe checking skips pointer dereferences in unused places let _ = <access to unsafe field> currently type-checks