Skip to content

pass struct fields to chalk #19122

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

Merged
merged 1 commit into from
Feb 17, 2025
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
20 changes: 9 additions & 11 deletions crates/hir-ty/src/chalk_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,23 +768,21 @@ pub(crate) fn adt_datum_query(
phantom_data,
};

// this slows down rust-analyzer by quite a bit unfortunately, so enabling this is currently not worth it
let _variant_id_to_fields = |id: VariantId| {
let variant_id_to_fields = |id: VariantId| {
let variant_data = &id.variant_data(db.upcast());
let fields = if variant_data.fields().is_empty() {
let fields = if variant_data.fields().is_empty() || bound_vars_subst.is_empty(Interner) {
vec![]
} else {
let field_types = db.field_types(id);
variant_data
.fields()
.iter()
.map(|(idx, _)| field_types[idx].clone().substitute(Interner, &bound_vars_subst))
.filter(|it| !it.contains_unknown())
.collect()
// HACK: provide full struct type info slows down rust-analyzer by quite a bit unfortunately,
// so we trick chalk into thinking that our struct impl Unsize
if let Some(ty) = bound_vars_subst.at(Interner, 0).ty(Interner) {
Copy link
Member

Choose a reason for hiding this comment

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

What is the first bound var in this context? (has been a while since I touched this here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the first generic of this struct

struct Foo<T, U, V>
         //^ this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Basically this is make chalk believe every struct with generic type is struct Foo<T>(T)

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this could cause other issues, also wouldn't this only work out where the first supplied parameter type is actually unsizable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if this could cause other issues

Maybe, but since other tests passed, I guess it's fine so far

also wouldn't this only work out where the first supplied parameter type is actually unsizable?

Yes but I believe r-a and chalk already handled that. r-a has recorded what type is sized and chalk would query r-a for that info.

vec![ty.clone()]
} else {
vec![]
}
};
rust_ir::AdtVariantDatum { fields }
};
let variant_id_to_fields = |_: VariantId| rust_ir::AdtVariantDatum { fields: vec![] };

let (kind, variants) = match adt_id {
hir_def::AdtId::StructId(id) => {
Expand Down
25 changes: 22 additions & 3 deletions crates/hir-ty/src/tests/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,17 +536,15 @@ fn test() {

#[test]
fn coerce_unsize_generic() {
check(
check_no_mismatches(
r#"
//- minicore: coerce_unsized
struct Foo<T> { t: T };
struct Bar<T>(Foo<T>);

fn test() {
let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
//^^^^^^^^^^^^^^^^^^^^^ expected &'? Foo<[usize]>, got &'? Foo<[i32; 3]>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test case is effectively the same with the added one, just different kind of coerce unsized

let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
//^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &'? Bar<[usize]>, got &'? Bar<[i32; 3]>
}
"#,
);
Expand Down Expand Up @@ -958,3 +956,24 @@ fn f() {
"#,
);
}

#[test]
fn coerce_nested_unsized_struct() {
check_types(
r#"
//- minicore: fn, coerce_unsized, dispatch_from_dyn, sized
use core::marker::Unsize;

struct Foo<T: ?Sized>(T);

fn need(_: &Foo<dyn Fn(i32) -> i32>) {
}

fn test() {
let callback = |x| x;
//^ i32
need(&Foo(callback));
}
"#,
)
}
12 changes: 6 additions & 6 deletions crates/hir-ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4694,21 +4694,21 @@ fn f<T: Send, U>() {
Struct::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^Yes
Struct::<U>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^Yes
//^^^^^^^^^^^^^^^^^^^^{unknown}
Struct::<*const T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^Yes
//^^^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
Enum::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^Yes
Enum::<U>::IS_SEND;
//^^^^^^^^^^^^^^^^^^Yes
//^^^^^^^^^^^^^^^^^^{unknown}
Enum::<*const T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^Yes
//^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
Union::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^Yes
Union::<U>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^Yes
//^^^^^^^^^^^^^^^^^^^{unknown}
Union::<*const T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^^Yes
//^^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
PhantomData::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^Yes
PhantomData::<U>::IS_SEND;
Expand Down