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

Disable drop range tracking in generators #93165

Merged
merged 3 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion compiler/rustc_typeck/src/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use tracing::debug;

mod drop_ranges;

// FIXME(eholk): This flag is here to give a quick way to disable drop tracking in case we find
// unexpected breakages while it's still new. It should be removed before too long.
eholk marked this conversation as resolved.
Show resolved Hide resolved
const ENABLE_DROP_TRACKING: bool = false;

struct InteriorVisitor<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>,
types: FxIndexSet<ty::GeneratorInteriorTypeCause<'tcx>>,
Expand Down Expand Up @@ -77,7 +81,10 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
yield_data.expr_and_pat_count, self.expr_count, source_span
);

if self.drop_ranges.is_dropped_at(hir_id, yield_data.expr_and_pat_count)
if ENABLE_DROP_TRACKING
&& self
.drop_ranges
.is_dropped_at(hir_id, yield_data.expr_and_pat_count)
{
debug!("value is dropped at yield point; not recording");
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/async-await/async-fn-nonsend.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// edition:2018
// compile-flags: --crate-type lib

// FIXME(eholk): temporarily disabled while drop range tracking is disabled
// (see generator_interior.rs:27)
// ignore-test

use std::{cell::RefCell, fmt::Debug, rc::Rc};

fn non_sync() -> impl Debug {
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/async-await/unresolved_type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// (rather than give a general error message)
// edition:2018

// FIXME(eholk): temporarily disabled while drop range tracking is disabled
// (see generator_interior.rs:27)
// ignore-test

async fn bar<T>() -> () {}

async fn foo() {
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/generator/drop-control-flow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// build-pass

// FIXME(eholk): temporarily disabled while drop range tracking is disabled
// (see generator_interior.rs:27)
// ignore-test

// A test to ensure generators capture values that were conditionally dropped,
// and also that values that are dropped along all paths to a yield do not get
// included in the generator type.
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/generator/issue-57478.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// check-pass

// FIXME(eholk): temporarily disabled while drop range tracking is disabled
// (see generator_interior.rs:27)
// ignore-test

#![feature(negative_impls, generators)]

struct Foo;
Expand Down
93 changes: 93 additions & 0 deletions src/test/ui/generator/issue-93161.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// edition:2021
// run-pass

#![feature(never_type)]

use std::future::Future;

// See if we can run a basic `async fn`
pub async fn foo(x: &u32, y: u32) -> u32 {
let y = &y;
let z = 9;
let z = &z;
let y = async { *y + *z }.await;
let a = 10;
let a = &a;
*x + y + *a
}

async fn add(x: u32, y: u32) -> u32 {
let a = async { x + y };
a.await
}

async fn build_aggregate(a: u32, b: u32, c: u32, d: u32) -> u32 {
let x = (add(a, b).await, add(c, d).await);
x.0 + x.1
}

enum Never {}
fn never() -> Never {
panic!()
}

async fn includes_never(crash: bool, x: u32) -> u32 {
let mut result = async { x * x }.await;
if !crash {
return result;
}
#[allow(unused)]
let bad = never();
result *= async { x + x }.await;
drop(bad);
result
}

async fn partial_init(x: u32) -> u32 {
#[allow(unreachable_code)]
let _x: (String, !) = (String::new(), return async { x + x }.await);
}

async fn read_exact(_from: &mut &[u8], _to: &mut [u8]) -> Option<()> {
Some(())
}

async fn hello_world() {
let data = [0u8; 1];
let mut reader = &data[..];

let mut marker = [0u8; 1];
read_exact(&mut reader, &mut marker).await.unwrap();
}

fn run_fut<T>(fut: impl Future<Output = T>) -> T {
use std::sync::Arc;
use std::task::{Context, Poll, Wake, Waker};

struct MyWaker;
impl Wake for MyWaker {
fn wake(self: Arc<Self>) {
unimplemented!()
}
}

let waker = Waker::from(Arc::new(MyWaker));
let mut context = Context::from_waker(&waker);

let mut pinned = Box::pin(fut);
loop {
match pinned.as_mut().poll(&mut context) {
Poll::Pending => continue,
Poll::Ready(v) => return v,
}
}
}

fn main() {
let x = 5;
assert_eq!(run_fut(foo(&x, 7)), 31);
assert_eq!(run_fut(build_aggregate(1, 2, 3, 4)), 10);
assert_eq!(run_fut(includes_never(false, 4)), 16);
assert_eq!(run_fut(partial_init(4)), 8);
run_fut(hello_world());
}
4 changes: 4 additions & 0 deletions src/test/ui/generator/partial-drop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// FIXME(eholk): temporarily disabled while drop range tracking is disabled
// (see generator_interior.rs:27)
// ignore-test

#![feature(negative_impls, generators)]

struct Foo;
Expand Down