Skip to content

Commit b533f95

Browse files
authored
Merge pull request #21069 from Aditya-PS-05/feat/add-regression-test-19957
feat: add test for async-trait type mismatch
2 parents b342c20 + f39579c commit b533f95

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,58 @@ where
552552
"#]],
553553
);
554554
}
555+
556+
#[test]
557+
fn regression_19957() {
558+
// This test documents issue #19957: async-trait patterns incorrectly produce
559+
// type mismatches between Pin<Box<dyn Future>> and Pin<Box<impl Future>>.
560+
check_no_mismatches(
561+
r#"
562+
//- minicore: future, pin, result, error, send, coerce_unsized, dispatch_from_dyn
563+
use core::{future::Future, pin::Pin};
564+
565+
#[lang = "owned_box"]
566+
pub struct Box<T: ?Sized> {
567+
inner: *mut T,
568+
}
569+
570+
impl<T> Box<T> {
571+
fn pin(value: T) -> Pin<Box<T>> {
572+
// Implementation details don't matter here for type checking
573+
loop {}
574+
}
575+
}
576+
577+
impl<T: ?Sized + core::marker::Unsize<U>, U: ?Sized> core::ops::CoerceUnsized<Box<U>> for Box<T> {}
578+
579+
impl<T: ?Sized + core::ops::DispatchFromDyn<U>, U: ?Sized> core::ops::DispatchFromDyn<Box<U>> for Box<T> {}
580+
581+
pub struct ExampleData {
582+
pub id: i32,
583+
}
584+
585+
// Simulates what #[async_trait] expands to
586+
pub trait SimpleModel {
587+
fn save<'life0, 'async_trait>(
588+
&'life0 self,
589+
) -> Pin<Box<dyn Future<Output = i32> + Send + 'async_trait>>
590+
where
591+
'life0: 'async_trait,
592+
Self: 'async_trait;
593+
}
594+
595+
impl SimpleModel for ExampleData {
596+
fn save<'life0, 'async_trait>(
597+
&'life0 self,
598+
) -> Pin<Box<dyn Future<Output = i32> + Send + 'async_trait>>
599+
where
600+
'life0: 'async_trait,
601+
Self: 'async_trait,
602+
{
603+
// Body creates Pin<Box<impl Future>>, which should coerce to Pin<Box<dyn Future>>
604+
Box::pin(async move { self.id })
605+
}
606+
}
607+
"#,
608+
);
609+
}

crates/test-utils/src/minicore.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,12 @@ pub mod pin {
15181518
{
15191519
}
15201520
// endregion:dispatch_from_dyn
1521+
// region:coerce_unsized
1522+
impl<Ptr, U> crate::ops::CoerceUnsized<Pin<U>> for Pin<Ptr> where
1523+
Ptr: crate::ops::CoerceUnsized<U>
1524+
{
1525+
}
1526+
// endregion:coerce_unsized
15211527
}
15221528
// endregion:pin
15231529

0 commit comments

Comments
 (0)