Merge pull request #19122 from Austaras/master

pass struct fields to chalk
This commit is contained in:
Lukas Wirth 2025-02-17 09:37:55 +00:00 committed by GitHub
commit d7fb8f15c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 20 deletions

View File

@ -768,23 +768,21 @@ pub(crate) fn adt_datum_query(
phantom_data, 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 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![] vec![]
} else { } else {
let field_types = db.field_types(id); // HACK: provide full struct type info slows down rust-analyzer by quite a bit unfortunately,
variant_data // so we trick chalk into thinking that our struct impl Unsize
.fields() if let Some(ty) = bound_vars_subst.at(Interner, 0).ty(Interner) {
.iter() vec![ty.clone()]
.map(|(idx, _)| field_types[idx].clone().substitute(Interner, &bound_vars_subst)) } else {
.filter(|it| !it.contains_unknown()) vec![]
.collect() }
}; };
rust_ir::AdtVariantDatum { fields } rust_ir::AdtVariantDatum { fields }
}; };
let variant_id_to_fields = |_: VariantId| rust_ir::AdtVariantDatum { fields: vec![] };
let (kind, variants) = match adt_id { let (kind, variants) = match adt_id {
hir_def::AdtId::StructId(id) => { hir_def::AdtId::StructId(id) => {

View File

@ -535,7 +535,7 @@ fn test() {
#[test] #[test]
fn coerce_unsize_generic() { fn coerce_unsize_generic() {
check( check_no_mismatches(
r#" r#"
//- minicore: coerce_unsized //- minicore: coerce_unsized
struct Foo<T> { t: T }; struct Foo<T> { t: T };
@ -543,9 +543,7 @@ struct Bar<T>(Foo<T>);
fn test() { fn test() {
let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] }; let _: &Foo<[usize]> = &Foo { t: [1, 2, 3] };
//^^^^^^^^^^^^^^^^^^^^^ expected &'? Foo<[usize]>, got &'? Foo<[i32; 3]>
let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] }); let _: &Bar<[usize]> = &Bar(Foo { t: [1, 2, 3] });
//^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &'? Bar<[usize]>, got &'? Bar<[i32; 3]>
} }
"#, "#,
); );
@ -957,3 +955,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));
}
"#,
)
}

View File

@ -4694,21 +4694,21 @@ fn f<T: Send, U>() {
Struct::<T>::IS_SEND; Struct::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^^Yes
Struct::<U>::IS_SEND; Struct::<U>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^^{unknown}
Struct::<*const T>::IS_SEND; Struct::<*const T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
Enum::<T>::IS_SEND; Enum::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^Yes
Enum::<U>::IS_SEND; Enum::<U>::IS_SEND;
//^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^{unknown}
Enum::<*const T>::IS_SEND; Enum::<*const T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
Union::<T>::IS_SEND; Union::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^Yes
Union::<U>::IS_SEND; Union::<U>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^{unknown}
Union::<*const T>::IS_SEND; Union::<*const T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^^^^^^^^{unknown}
PhantomData::<T>::IS_SEND; PhantomData::<T>::IS_SEND;
//^^^^^^^^^^^^^^^^^^^^^^^^^Yes //^^^^^^^^^^^^^^^^^^^^^^^^^Yes
PhantomData::<U>::IS_SEND; PhantomData::<U>::IS_SEND;