Skip to content

Commit

Permalink
generator layout: ignore fake borrows
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Nov 8, 2023
1 parent bf360d4 commit a42eca4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_mir_dataflow/src/impls/borrowed_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use rustc_middle::mir::*;
use crate::{AnalysisDomain, GenKill, GenKillAnalysis};

/// A dataflow analysis that tracks whether a pointer or reference could possibly exist that points
/// to a given local.
/// to a given local. This analysis ignores fake borrows, so it should not be used by
/// borrowck.
///
/// At present, this is used as a very limited form of alias analysis. For example,
/// `MaybeBorrowedLocals` is used to compute which locals are live during a yield expression for
Expand Down Expand Up @@ -91,13 +92,17 @@ where
self.super_rvalue(rvalue, location);

match rvalue {
Rvalue::AddressOf(_, borrowed_place) | Rvalue::Ref(_, _, borrowed_place) => {
// We ignore fake borrows as these get removed after analysis and shouldn't effect
// the layout of generators.
Rvalue::AddressOf(_, borrowed_place)
| Rvalue::Ref(_, BorrowKind::Mut { .. } | BorrowKind::Shared, borrowed_place) => {
if !borrowed_place.is_indirect() {
self.trans.gen(borrowed_place.local);
}
}

Rvalue::Cast(..)
| Rvalue::Ref(_, BorrowKind::Shallow, _)
| Rvalue::ShallowInitBox(..)
| Rvalue::Use(..)
| Rvalue::ThreadLocalRef(..)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/cleanup_post_borrowck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! - [`AscribeUserType`]
//! - [`FakeRead`]
//! - [`Assign`] statements with a [`Shallow`] borrow
//! - [`Assign`] statements with a [`Fake`] borrow
//!
//! [`AscribeUserType`]: rustc_middle::mir::StatementKind::AscribeUserType
//! [`Assign`]: rustc_middle::mir::StatementKind::Assign
Expand Down
34 changes: 34 additions & 0 deletions tests/ui/coroutine/witness-ignore-fake-reads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// check-pass
// edition: 2021

// regression test for #117059
struct SendNotSync(*const ());
unsafe impl Send for SendNotSync {}
// impl !Sync for SendNotSync {} // automatically disabled

struct Inner {
stream: SendNotSync,
state: bool,
}

struct SendSync;
impl std::ops::Deref for SendSync {
type Target = Inner;
fn deref(&self) -> &Self::Target {
todo!();
}
}

async fn next() {
let inner = SendSync;
match inner.state {
true if false => {}
false => async {}.await,
_ => {}
}
}

fn is_send<T: Send>(_: T) {}
fn main() {
is_send(next())
}

0 comments on commit a42eca4

Please sign in to comment.