Skip to content

Commit 21fc062

Browse files
committed
pass struct fields to chalk
1 parent 35181e1 commit 21fc062

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

crates/hir-ty/src/chalk_db.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,9 @@ pub(crate) fn adt_datum_query(
768768
phantom_data,
769769
};
770770

771-
// this slows down rust-analyzer by quite a bit unfortunately, so enabling this is currently not worth it
772-
let _variant_id_to_fields = |id: VariantId| {
771+
// this slows down rust-analyzer by quite a bit unfortunately
772+
// but it's needed to trick chalk into impl Unsize
773+
let variant_id_to_fields = |id: VariantId| {
773774
let variant_data = &id.variant_data(db.upcast());
774775
let fields = if variant_data.fields().is_empty() {
775776
vec![]
@@ -778,13 +779,13 @@ pub(crate) fn adt_datum_query(
778779
variant_data
779780
.fields()
780781
.iter()
782+
.rev()
783+
.take(1)
781784
.map(|(idx, _)| field_types[idx].clone().substitute(Interner, &bound_vars_subst))
782-
.filter(|it| !it.contains_unknown())
783785
.collect()
784786
};
785787
rust_ir::AdtVariantDatum { fields }
786788
};
787-
let variant_id_to_fields = |_: VariantId| rust_ir::AdtVariantDatum { fields: vec![] };
788789

789790
let (kind, variants) = match adt_id {
790791
hir_def::AdtId::StructId(id) => {

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,17 +536,15 @@ fn test() {
536536

537537
#[test]
538538
fn coerce_unsize_generic() {
539-
check(
539+
check_no_mismatches(
540540
r#"
541541
//- minicore: coerce_unsized
542542
struct Foo<T> { t: T };
543543
struct Bar<T>(Foo<T>);
544544
545545
fn test() {
546546
let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
547-
//^^^^^^^^^^^^^^^^^^^^^ expected &'? Foo<[usize]>, got &'? Foo<[i32; 3]>
548547
let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
549-
//^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &'? Bar<[usize]>, got &'? Bar<[i32; 3]>
550548
}
551549
"#,
552550
);
@@ -958,3 +956,24 @@ fn f() {
958956
"#,
959957
);
960958
}
959+
960+
#[test]
961+
fn coerce_nested_unsized_struct() {
962+
check_types(
963+
r#"
964+
//- minicore: fn, coerce_unsized, dispatch_from_dyn, sized
965+
use core::marker::Unsize;
966+
967+
struct Foo<T: ?Sized>(T);
968+
969+
fn need(_: &Foo<dyn Fn(i32) -> i32>) {
970+
}
971+
972+
fn test() {
973+
let callback = |x| x;
974+
//^ i32
975+
need(&Foo(callback));
976+
}
977+
"#,
978+
)
979+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,21 +4694,21 @@ fn f<T: Send, U>() {
46944694
Struct::<T>::IS_SEND;
46954695
//^^^^^^^^^^^^^^^^^^^^Yes
46964696
Struct::<U>::IS_SEND;
4697-
//^^^^^^^^^^^^^^^^^^^^Yes
4697+
//^^^^^^^^^^^^^^^^^^^^{unknown}
46984698
Struct::<*const T>::IS_SEND;
4699-
//^^^^^^^^^^^^^^^^^^^^^^^^^^^Yes
4699+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
47004700
Enum::<T>::IS_SEND;
47014701
//^^^^^^^^^^^^^^^^^^Yes
47024702
Enum::<U>::IS_SEND;
4703-
//^^^^^^^^^^^^^^^^^^Yes
4703+
//^^^^^^^^^^^^^^^^^^{unknown}
47044704
Enum::<*const T>::IS_SEND;
4705-
//^^^^^^^^^^^^^^^^^^^^^^^^^Yes
4705+
//^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
47064706
Union::<T>::IS_SEND;
47074707
//^^^^^^^^^^^^^^^^^^^Yes
47084708
Union::<U>::IS_SEND;
4709-
//^^^^^^^^^^^^^^^^^^^Yes
4709+
//^^^^^^^^^^^^^^^^^^^{unknown}
47104710
Union::<*const T>::IS_SEND;
4711-
//^^^^^^^^^^^^^^^^^^^^^^^^^^Yes
4711+
//^^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
47124712
PhantomData::<T>::IS_SEND;
47134713
//^^^^^^^^^^^^^^^^^^^^^^^^^Yes
47144714
PhantomData::<U>::IS_SEND;

0 commit comments

Comments
 (0)