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

Fix stray trait mismatch in resolve_associated_item for AsyncFn #121350

Merged
merged 1 commit into from
Feb 21, 2024
Merged
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
18 changes: 3 additions & 15 deletions compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn resolve_associated_item<'tcx>(
span: tcx.def_span(trait_item_id),
})
}
} else if tcx.fn_trait_kind_from_def_id(trait_ref.def_id).is_some() {
} else if let Some(target_kind) = tcx.fn_trait_kind_from_def_id(trait_ref.def_id) {
// FIXME: This doesn't check for malformed libcore that defines, e.g.,
// `trait Fn { fn call_once(&self) { .. } }`. This is mostly for extension
// methods.
Expand All @@ -265,13 +265,7 @@ fn resolve_associated_item<'tcx>(
}
match *rcvr_args.type_at(0).kind() {
ty::Closure(closure_def_id, args) => {
let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap();
Some(Instance::resolve_closure(
tcx,
closure_def_id,
args,
trait_closure_kind,
))
Some(Instance::resolve_closure(tcx, closure_def_id, args, target_kind))
}
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
def: ty::InstanceDef::FnPtrShim(trait_item_id, rcvr_args.type_at(0)),
Expand Down Expand Up @@ -324,13 +318,7 @@ fn resolve_associated_item<'tcx>(
}
}
ty::Closure(closure_def_id, args) => {
let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap();
Some(Instance::resolve_closure(
tcx,
closure_def_id,
args,
trait_closure_kind,
))
Some(Instance::resolve_closure(tcx, closure_def_id, args, target_kind))
}
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
def: ty::InstanceDef::FnPtrShim(trait_item_id, rcvr_args.type_at(0)),
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/async-await/async-fn/auxiliary/block-on.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//@ edition: 2021

#![feature(async_closure, noop_waker)]

use std::future::Future;
use std::pin::pin;
use std::task::*;

pub fn block_on<T>(fut: impl Future<Output = T>) -> T {
let mut fut = pin!(fut);
// Poll loop, just to test the future...
let ctx = &mut Context::from_waker(Waker::noop());

loop {
match unsafe { fut.as_mut().poll(ctx) } {
Poll::Pending => {}
Poll::Ready(t) => break t,
}
}
}
7 changes: 6 additions & 1 deletion tests/ui/async-await/async-fn/simple.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//@ aux-build:block-on.rs
//@ edition: 2021
//@ build-pass

#![feature(async_fn_traits)]

extern crate block_on;

use std::ops::AsyncFn;

async fn foo() {}
Expand All @@ -12,5 +15,7 @@ async fn call_asyncly(f: impl AsyncFn(i32) -> i32) -> i32 {
}

fn main() {
let fut = call_asyncly(|x| async move { x + 1 });
block_on::block_on(async {
call_asyncly(|x| async move { x + 1 }).await;
Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out that this wasn't caught because we weren't actually codegenning <{closure} as AsyncFn>::async_call here, since we weren't awaiting the future.

Makes me wish we had a first-class async main to make testing here easier.

});
}
Loading