Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 268 additions & 19 deletions compiler/rustc_mir_transform/src/coroutine.rs

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions src/tools/clippy/clippy_lints/src/await_holding_invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdMap};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::CoroutineLayout;
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::{TyCtxt, Ty};
use rustc_session::impl_lint_pass;
use rustc_span::Span;

Expand Down Expand Up @@ -213,7 +213,8 @@ impl<'tcx> LateLintPass<'tcx> for AwaitHolding {
impl AwaitHolding {
fn check_interior_types(&self, cx: &LateContext<'_>, coroutine: &CoroutineLayout<'_>) {
for (ty_index, ty_cause) in coroutine.field_tys.iter_enumerated() {
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
let coroutine_field_ty = maybe_unwrap_unsafe_pinned(ty_cause.ty);
if let rustc_middle::ty::Adt(adt, _) = coroutine_field_ty.kind() {
let await_points = || {
coroutine
.variant_source_info
Expand All @@ -226,6 +227,7 @@ impl AwaitHolding {
})
.collect::<Vec<_>>()
};

if is_mutex_guard(cx, adt.did()) {
span_lint_and_then(
cx,
Expand Down Expand Up @@ -263,6 +265,18 @@ impl AwaitHolding {
}
}
}

}


// self-referential coroutine fields are wrapped in `UnsafePinned`.
fn maybe_unwrap_unsafe_pinned<'tcx>(ty: Ty<'tcx>) -> Ty<'tcx> {
match ty.kind() {
rustc_middle::ty::Adt(adt, args) if adt.is_unsafe_pinned() => {
args.type_at(0)
}
_ => ty
}
}

fn emit_invalid_type(
Expand Down
36 changes: 0 additions & 36 deletions src/tools/miri/tests/fail/async-shared-mutable.stack.stderr

This file was deleted.

44 changes: 0 additions & 44 deletions src/tools/miri/tests/fail/async-shared-mutable.tree.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! FIXME: This test should pass! However, `async fn` does not yet use `UnsafePinned`.
//! This is a regression test for <https://github.com/rust-lang/rust/issues/137750>:
//! `UnsafePinned` must include the effects of `UnsafeCell`.
//@revisions: stack tree
Expand All @@ -13,7 +12,7 @@ fn main() {
let mut f = pin!(async move {
let x = &mut 0u8;
core::future::poll_fn(move |_| {
*x = 1; //~ERROR: write access
*x = 1;
Poll::<()>::Pending
})
.await
Expand Down
1 change: 0 additions & 1 deletion src/tools/miri/tests/pass/packed-struct-dyn-trait.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// run-pass
use std::ptr::addr_of;

// When the unsized tail is a `dyn Trait`, its alignments is only dynamically known. This means the
Expand Down
30 changes: 30 additions & 0 deletions tests/codegen-llvm/non-self-ref-coroutine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Tests that the coroutine struct is not noalias if it has no self-referential
// fields.
// NOTE: this should eventually use noalias

//@ compile-flags: -C opt-level=3
//@ edition: 2021

#![crate_type = "lib"]

use std::future::Future;
use std::pin::Pin;

async fn inner() {}

// CHECK-LABEL: ; non_self_ref_coroutine::my_async_fn::{closure#0}
// CHECK-LABEL: my_async_fn
// CHECK-NOT: noalias
// CHECK-SAME: %_1
async fn my_async_fn(b: bool) -> i32 {
let x = Box::new(5);
if b {
inner().await;
}
*x + 1
}

#[no_mangle]
pub fn create_future_as_trait(b: bool) -> Pin<Box<dyn Future<Output = i32>>> {
Box::pin(my_async_fn(b))
}
31 changes: 31 additions & 0 deletions tests/codegen-llvm/self-ref-coroutine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Tests that the coroutine struct is not noalias if it has self-referential
// fields.

//@ compile-flags: -C opt-level=3
//@ edition: 2021

#![crate_type = "lib"]

use std::future::Future;
use std::pin::Pin;

async fn inner() {}

// CHECK-LABEL: ; self_ref_coroutine::my_async_fn::{closure#0}
// CHECK-LABEL: my_async_fn
// CHECK-NOT: noalias
// CHECK-SAME: %_1
async fn my_async_fn(b: bool) -> i32 {
let x = Box::new(5);
let y = &x;
if b {
inner().await;
std::hint::black_box(y);
}
*x + 1
}

#[no_mangle]
pub fn create_future_as_trait(b: bool) -> Pin<Box<dyn Future<Output = i32>>> {
Box::pin(my_async_fn(b))
}
Loading
Loading