Skip to content

Commit bfb5c7a

Browse files
committed
Filter out {unknown} types in adt_datum_quqery
1 parent 0eca3ef commit bfb5c7a

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

crates/hir-def/src/data/adt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct StructData {
4040
}
4141

4242
bitflags! {
43-
#[derive(Debug, Clone, PartialEq, Eq)]
43+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4444
pub struct StructFlags: u8 {
4545
const NO_FLAGS = 0;
4646
/// Indicates whether the struct is `PhantomData`.

crates/hir-ty/src/chalk_db.rs

+41-28
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use chalk_solve::rust_ir::{self, OpaqueTyDatumBound, WellKnownTrait};
1010

1111
use base_db::CrateId;
1212
use hir_def::{
13+
data::adt::StructFlags,
1314
hir::Movability,
1415
lang_item::{LangItem, LangItemTarget},
15-
AssocItemId, BlockId, GenericDefId, HasModule, ItemContainerId, Lookup, TypeAliasId,
16+
AssocItemId, BlockId, GenericDefId, HasModule, ItemContainerId, Lookup, TypeAliasId, VariantId,
1617
};
1718
use hir_expand::name::name;
1819

@@ -159,6 +160,7 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
159160
debug!("impls_for_trait returned {} impls", result.len());
160161
result
161162
}
163+
162164
fn impl_provided_for(&self, auto_trait_id: TraitId, kind: &chalk_ir::TyKind<Interner>) -> bool {
163165
debug!("impl_provided_for {:?}, {:?}", auto_trait_id, kind);
164166

@@ -183,22 +185,22 @@ impl chalk_solve::RustIrDatabase<Interner> for ChalkContext<'_> {
183185
(TyKind::Adt(id_a, _), TyKind::Adt(id_b, _)) => id_a == id_b,
184186
(TyKind::AssociatedType(id_a, _), TyKind::AssociatedType(id_b, _)) => id_a == id_b,
185187
(TyKind::Scalar(scalar_a), TyKind::Scalar(scalar_b)) => scalar_a == scalar_b,
186-
(TyKind::Str, TyKind::Str) => true,
188+
(TyKind::Error, TyKind::Error)
189+
| (TyKind::Str, TyKind::Str)
190+
| (TyKind::Slice(_), TyKind::Slice(_))
191+
| (TyKind::Never, TyKind::Never)
192+
| (TyKind::Array(_, _), TyKind::Array(_, _)) => true,
187193
(TyKind::Tuple(arity_a, _), TyKind::Tuple(arity_b, _)) => arity_a == arity_b,
188194
(TyKind::OpaqueType(id_a, _), TyKind::OpaqueType(id_b, _)) => id_a == id_b,
189-
(TyKind::Slice(_), TyKind::Slice(_)) => true,
190195
(TyKind::FnDef(id_a, _), TyKind::FnDef(id_b, _)) => id_a == id_b,
191-
(TyKind::Ref(id_a, _, _), TyKind::Ref(id_b, _, _)) => id_a == id_b,
192-
(TyKind::Raw(id_a, _), TyKind::Raw(id_b, _)) => id_a == id_b,
193-
(TyKind::Never, TyKind::Never) => true,
194-
(TyKind::Array(_, _), TyKind::Array(_, _)) => true,
196+
(TyKind::Ref(id_a, _, _), TyKind::Ref(id_b, _, _))
197+
| (TyKind::Raw(id_a, _), TyKind::Raw(id_b, _)) => id_a == id_b,
195198
(TyKind::Closure(id_a, _), TyKind::Closure(id_b, _)) => id_a == id_b,
196-
(TyKind::Coroutine(id_a, _), TyKind::Coroutine(id_b, _)) => id_a == id_b,
197-
(TyKind::CoroutineWitness(id_a, _), TyKind::CoroutineWitness(id_b, _)) => {
199+
(TyKind::Coroutine(id_a, _), TyKind::Coroutine(id_b, _))
200+
| (TyKind::CoroutineWitness(id_a, _), TyKind::CoroutineWitness(id_b, _)) => {
198201
id_a == id_b
199202
}
200203
(TyKind::Foreign(id_a), TyKind::Foreign(id_b)) => id_a == id_b,
201-
(TyKind::Error, TyKind::Error) => true,
202204
(_, _) => false,
203205
}
204206
};
@@ -653,7 +655,7 @@ pub(crate) fn trait_datum_query(
653655
coinductive: false, // only relevant for Chalk testing
654656
// FIXME: set these flags correctly
655657
marker: false,
656-
fundamental: false,
658+
fundamental: trait_data.fundamental,
657659
};
658660
let where_clauses = convert_where_clauses(db, trait_.into(), &bound_vars);
659661
let associated_ty_ids = trait_data.associated_types().map(to_assoc_type_id).collect();
@@ -715,33 +717,44 @@ fn lang_item_from_well_known_trait(trait_: WellKnownTrait) -> LangItem {
715717
pub(crate) fn adt_datum_query(
716718
db: &dyn HirDatabase,
717719
krate: CrateId,
718-
adt_id: AdtId,
720+
chalk_ir::AdtId(adt_id): AdtId,
719721
) -> Arc<AdtDatum> {
720722
debug!("adt_datum {:?}", adt_id);
721-
let chalk_ir::AdtId(adt_id) = adt_id;
722723
let generic_params = generics(db.upcast(), adt_id.into());
723724
let bound_vars_subst = generic_params.bound_vars_subst(db, DebruijnIndex::INNERMOST);
724725
let where_clauses = convert_where_clauses(db, adt_id.into(), &bound_vars_subst);
725726

726-
let phantom_data_id = db
727-
.lang_item(krate, LangItem::PhantomData)
728-
.and_then(|item| item.as_struct())
729-
.map(|item| item.into());
727+
let (fundamental, phantom_data) = match adt_id {
728+
hir_def::AdtId::StructId(s) => {
729+
let flags = db.struct_data(s).flags;
730+
(
731+
flags.contains(StructFlags::IS_FUNDAMENTAL),
732+
flags.contains(StructFlags::IS_PHANTOM_DATA),
733+
)
734+
}
735+
// FIXME set fundamental flags correctly
736+
hir_def::AdtId::UnionId(_) => (false, false),
737+
hir_def::AdtId::EnumId(_) => (false, false),
738+
};
730739
let flags = rust_ir::AdtFlags {
731740
upstream: adt_id.module(db.upcast()).krate() != krate,
732-
// FIXME set fundamental flags correctly
733-
fundamental: false,
734-
phantom_data: phantom_data_id == Some(adt_id),
741+
fundamental,
742+
phantom_data,
735743
};
736744

737-
let variant_id_to_fields = |id| {
738-
let field_types = db.field_types(id);
739-
let fields = id
740-
.variant_data(db.upcast())
741-
.fields()
742-
.iter()
743-
.map(|(idx, _)| field_types[idx].clone().substitute(Interner, &bound_vars_subst))
744-
.collect();
745+
let variant_id_to_fields = |id: VariantId| {
746+
let variant_data = &id.variant_data(db.upcast());
747+
let fields = if variant_data.fields().is_empty() {
748+
vec![]
749+
} else {
750+
let field_types = db.field_types(id);
751+
variant_data
752+
.fields()
753+
.iter()
754+
.map(|(idx, _)| field_types[idx].clone().substitute(Interner, &bound_vars_subst))
755+
.filter(|it| !it.is_unknown())
756+
.collect()
757+
};
745758
rust_ir::AdtVariantDatum { fields }
746759
};
747760

0 commit comments

Comments
 (0)