Skip to content

Commit 220812c

Browse files
committed
fix: Fix panicking while resolving callable sigs for AsyncFnXs
1 parent 51af7a3 commit 220812c

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

crates/hir-ty/src/infer/unify.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ use rustc_hash::FxHashSet;
99
use rustc_type_ir::{
1010
DebruijnIndex, InferConst, InferTy, RegionVid, TyVid, TypeFoldable, TypeFolder,
1111
TypeSuperFoldable, TypeVisitableExt, UpcastFrom,
12-
inherent::{Const as _, IntoKind, Ty as _},
12+
inherent::{Const as _, GenericArgs as _, IntoKind, Ty as _},
1313
solve::{Certainty, GoalSource},
1414
};
1515
use smallvec::SmallVec;
16+
use stdx::never;
1617
use triomphe::Arc;
1718

1819
use crate::{
@@ -674,10 +675,21 @@ impl<'db> InferenceTable<'db> {
674675
let args = [ty, arg_ty];
675676
let trait_ref = TraitRef::new(self.interner(), fn_trait.into(), args);
676677

678+
let mut proj_args: SmallVec<[_; 3]> =
679+
GenericArgs::identity_for_item(self.interner(), output_assoc_type.into())
680+
.into_iter()
681+
.collect();
682+
if proj_args.len() < 2 {
683+
never!();
684+
return None;
685+
}
686+
proj_args[0] = ty.into();
687+
proj_args[1] = arg_ty.into();
688+
677689
let projection = Ty::new_alias(
678690
self.interner(),
679691
rustc_type_ir::AliasTyKind::Projection,
680-
AliasTy::new(self.interner(), output_assoc_type.into(), args),
692+
AliasTy::new(self.interner(), output_assoc_type.into(), proj_args),
681693
);
682694

683695
let pred = Predicate::upcast_from(trait_ref, self.interner());

crates/hir-ty/src/tests/regression/new_solver.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,31 @@ fn g(it: *const (dyn Trait)) {
524524
"#,
525525
);
526526
}
527+
528+
#[test]
529+
fn regression_20951() {
530+
check_infer(
531+
r#"
532+
//- minicore: async_fn
533+
trait DoesSomething {
534+
fn do_something(&self) -> impl Future<Output = usize>;
535+
}
536+
537+
impl<F> DoesSomething for F
538+
where
539+
F: AsyncFn() -> usize,
540+
{
541+
fn do_something(&self) -> impl Future<Output = usize> {
542+
self()
543+
}
544+
}
545+
"#,
546+
expect![[r#"
547+
43..47 'self': &'? Self
548+
168..172 'self': &'? F
549+
205..227 '{ ... }': <F as AsyncFnMut<()>>::CallRefFuture<'a>
550+
215..219 'self': &'? F
551+
215..221 'self()': <F as AsyncFnMut<()>>::CallRefFuture<'a>
552+
"#]],
553+
);
554+
}

crates/hir-ty/src/tests/traits.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4880,20 +4880,20 @@ async fn baz<T: AsyncFnOnce(u32) -> i32>(c: T) {
48804880
37..38 'a': T
48814881
43..83 '{ ...ait; }': ()
48824882
43..83 '{ ...ait; }': impl Future<Output = ()>
4883-
53..57 'fut1': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'?>
4883+
53..57 'fut1': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'a>
48844884
60..61 'a': T
4885-
60..64 'a(0)': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'?>
4885+
60..64 'a(0)': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'a>
48864886
62..63 '0': u32
4887-
70..74 'fut1': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'?>
4887+
70..74 'fut1': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'a>
48884888
70..80 'fut1.await': i32
48894889
124..129 'mut b': T
48904890
134..174 '{ ...ait; }': ()
48914891
134..174 '{ ...ait; }': impl Future<Output = ()>
4892-
144..148 'fut2': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'?>
4892+
144..148 'fut2': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'a>
48934893
151..152 'b': T
4894-
151..155 'b(0)': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'?>
4894+
151..155 'b(0)': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'a>
48954895
153..154 '0': u32
4896-
161..165 'fut2': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'?>
4896+
161..165 'fut2': <T as AsyncFnMut<(u32,)>>::CallRefFuture<'a>
48974897
161..171 'fut2.await': i32
48984898
216..217 'c': T
48994899
222..262 '{ ...ait; }': ()

0 commit comments

Comments
 (0)