Skip to content

Split out ExpressionStore from Body #19036

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 5 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion crates/hir-def/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl GenericParams {
GenericDefId::TraitAliasId(id) => id_to_generics(db, id, enabled_params),
GenericDefId::TypeAliasId(id) => id_to_generics(db, id, enabled_params),
GenericDefId::ImplId(id) => id_to_generics(db, id, enabled_params),
GenericDefId::ConstId(_) => (
GenericDefId::ConstId(_) | GenericDefId::StaticId(_) => (
Arc::new(GenericParams {
type_or_consts: Default::default(),
lifetimes: Default::default(),
Expand Down
25 changes: 17 additions & 8 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ impl TypeOwnerId {
Some(match self {
TypeOwnerId::FunctionId(it) => GenericDefId::FunctionId(it),
TypeOwnerId::ConstId(it) => GenericDefId::ConstId(it),
TypeOwnerId::StaticId(it) => GenericDefId::StaticId(it),
TypeOwnerId::AdtId(it) => GenericDefId::AdtId(it),
TypeOwnerId::TraitId(it) => GenericDefId::TraitId(it),
TypeOwnerId::TraitAliasId(it) => GenericDefId::TraitAliasId(it),
Expand All @@ -701,7 +702,7 @@ impl TypeOwnerId {
TypeOwnerId::EnumVariantId(it) => {
GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent))
}
TypeOwnerId::InTypeConstId(_) | TypeOwnerId::StaticId(_) => return None,
TypeOwnerId::InTypeConstId(_) => return None,
})
}
}
Expand Down Expand Up @@ -743,6 +744,7 @@ impl From<GenericDefId> for TypeOwnerId {
GenericDefId::TypeAliasId(it) => it.into(),
GenericDefId::ImplId(it) => it.into(),
GenericDefId::ConstId(it) => it.into(),
GenericDefId::StaticId(it) => it.into(),
}
}
}
Expand Down Expand Up @@ -851,7 +853,7 @@ impl GeneralConstId {
pub fn generic_def(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
match self {
GeneralConstId::ConstId(it) => Some(it.into()),
GeneralConstId::StaticId(_) => None,
GeneralConstId::StaticId(it) => Some(it.into()),
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(db),
GeneralConstId::InTypeConstId(it) => it.lookup(db).owner.as_generic_def_id(db),
}
Expand Down Expand Up @@ -897,7 +899,7 @@ impl DefWithBodyId {
pub fn as_generic_def_id(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
match self {
DefWithBodyId::FunctionId(f) => Some(f.into()),
DefWithBodyId::StaticId(_) => None,
DefWithBodyId::StaticId(s) => Some(s.into()),
DefWithBodyId::ConstId(c) => Some(c.into()),
DefWithBodyId::VariantId(c) => Some(c.lookup(db).parent.into()),
// FIXME: stable rust doesn't allow generics in constants, but we should
Expand Down Expand Up @@ -927,18 +929,23 @@ pub enum GenericDefId {
ConstId(ConstId),
FunctionId(FunctionId),
ImplId(ImplId),
// can't actually have generics currently, but they might in the future
// More importantly, this completes the set of items that contain type references
// which is to be used by the signature expression store in the future.
StaticId(StaticId),
Comment on lines +932 to +935
Copy link
Member Author

Choose a reason for hiding this comment

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

Everything in this PR is basically just moving and renaming parts except for this. If we don't put static in here we'd need to create an entirely new id enum that is effectively GenericDefId but with Static. I don't feel like we gain anything from such a split and it should simplify the coming changes a bunch.

TraitAliasId(TraitAliasId),
TraitId(TraitId),
TypeAliasId(TypeAliasId),
}
impl_from!(
FunctionId,
AdtId(StructId, EnumId, UnionId),
TraitId,
TraitAliasId,
TypeAliasId,
ConstId,
FunctionId,
ImplId,
ConstId
StaticId,
TraitAliasId,
TraitId,
TypeAliasId
for GenericDefId
);

Expand Down Expand Up @@ -969,6 +976,7 @@ impl GenericDefId {
GenericDefId::TraitAliasId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::ImplId(it) => file_id_and_params_of_item_loc(db, it),
GenericDefId::ConstId(it) => (it.lookup(db).id.file_id(), None),
GenericDefId::StaticId(it) => (it.lookup(db).id.file_id(), None),
}
}

Expand Down Expand Up @@ -1350,6 +1358,7 @@ impl HasModule for GenericDefId {
GenericDefId::TypeAliasId(it) => it.module(db),
GenericDefId::ImplId(it) => it.module(db),
GenericDefId::ConstId(it) => it.module(db),
GenericDefId::StaticId(it) => it.module(db),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/hir-def/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,7 @@ impl HasResolver for GenericDefId {
GenericDefId::TypeAliasId(inner) => inner.resolver(db),
GenericDefId::ImplId(inner) => inner.resolver(db),
GenericDefId::ConstId(inner) => inner.resolver(db),
GenericDefId::StaticId(inner) => inner.resolver(db),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/hir-ty/src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ fn parent_generic_def(db: &dyn DefDatabase, def: GenericDefId) -> Option<Generic
GenericDefId::FunctionId(it) => it.lookup(db).container,
GenericDefId::TypeAliasId(it) => it.lookup(db).container,
GenericDefId::ConstId(it) => it.lookup(db).container,
GenericDefId::AdtId(_)
GenericDefId::StaticId(_)
| GenericDefId::AdtId(_)
| GenericDefId::TraitId(_)
| GenericDefId::ImplId(_)
| GenericDefId::TraitAliasId(_) => return None,
Expand Down
9 changes: 4 additions & 5 deletions crates/hir-ty/src/infer/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ impl InferenceContext<'_> {
}
};

let generic_def_id = value_def.to_generic_def_id(self.db);
let Some(generic_def) = generic_def_id else {
// `value_def` is the kind of item that can never be generic (i.e. statics, at least
// currently). We can just skip the binders to get its type.
let generic_def = value_def.to_generic_def_id(self.db);
if let GenericDefId::StaticId(_) = generic_def {
// `Static` is the kind of item that can never be generic currently. We can just skip the binders to get its type.
let (ty, binders) = self.db.value_ty(value_def)?.into_value_and_skipped_binders();
stdx::always!(binders.is_empty(Interner), "non-empty binders for non-generic def",);
return Some(ValuePathResolution::NonGeneric(ty));
Expand Down Expand Up @@ -122,7 +121,7 @@ impl InferenceContext<'_> {
}

let parent_substs = self_subst.or_else(|| {
let generics = generics(self.db.upcast(), generic_def_id?);
let generics = generics(self.db.upcast(), generic_def);
let parent_params_len = generics.parent_generics()?.len();
let parent_args = &substs[substs.len() - parent_params_len..];
Some(Substitution::from_iter(Interner, parent_args))
Expand Down
14 changes: 7 additions & 7 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2471,14 +2471,14 @@ pub enum ValueTyDefId {
impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for ValueTyDefId);

impl ValueTyDefId {
pub(crate) fn to_generic_def_id(self, db: &dyn HirDatabase) -> Option<GenericDefId> {
pub(crate) fn to_generic_def_id(self, db: &dyn HirDatabase) -> GenericDefId {
match self {
Self::FunctionId(id) => Some(id.into()),
Self::StructId(id) => Some(id.into()),
Self::UnionId(id) => Some(id.into()),
Self::EnumVariantId(var) => Some(var.lookup(db.upcast()).parent.into()),
Self::ConstId(id) => Some(id.into()),
Self::StaticId(_) => None,
Self::FunctionId(id) => id.into(),
Self::StructId(id) => id.into(),
Self::UnionId(id) => id.into(),
Self::EnumVariantId(var) => var.lookup(db.upcast()).parent.into(),
Self::ConstId(id) => id.into(),
Self::StaticId(id) => id.into(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ struct FixedPoint<T, U, V>(&'static FixedPoint<(), T, U>, V);
}
GenericDefId::ImplId(_) => return None,
GenericDefId::ConstId(_) => return None,
GenericDefId::StaticId(_) => return None,
},
))
})
Expand Down
2 changes: 2 additions & 0 deletions crates/hir/src/from_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ impl From<GenericDef> for GenericDefId {
GenericDef::TypeAlias(it) => GenericDefId::TypeAliasId(it.id),
GenericDef::Impl(it) => GenericDefId::ImplId(it.id),
GenericDef::Const(it) => GenericDefId::ConstId(it.id),
GenericDef::Static(it) => GenericDefId::StaticId(it.id),
}
}
}
Expand All @@ -197,6 +198,7 @@ impl From<GenericDefId> for GenericDef {
GenericDefId::TypeAliasId(it) => GenericDef::TypeAlias(it.into()),
GenericDefId::ImplId(it) => GenericDef::Impl(it.into()),
GenericDefId::ConstId(it) => GenericDef::Const(it.into()),
GenericDefId::StaticId(it) => GenericDef::Static(it.into()),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,7 @@ pub enum GenericDef {
Impl(Impl),
// consts can have type parameters from their parents (i.e. associated consts of traits)
Const(Const),
Static(Static),
}
impl_from!(
Function,
Expand All @@ -3463,7 +3464,8 @@ impl_from!(
TraitAlias,
TypeAlias,
Impl,
Const
Const,
Static
for GenericDef
);

Expand Down Expand Up @@ -3513,6 +3515,7 @@ impl GenericDef {
GenericDef::TypeAlias(it) => it.id.into(),
GenericDef::Impl(it) => it.id.into(),
GenericDef::Const(it) => it.id.into(),
GenericDef::Static(it) => it.id.into(),
}
}

Expand Down Expand Up @@ -3570,6 +3573,7 @@ impl GenericDef {
item_tree_source_maps.impl_(id.value).generics()
}
GenericDefId::ConstId(_) => return,
GenericDefId::StaticId(_) => return,
},
};

Expand Down
1 change: 1 addition & 0 deletions crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ impl From<GenericDef> for Definition {
GenericDef::TypeAlias(it) => it.into(),
GenericDef::Impl(it) => it.into(),
GenericDef::Const(it) => it.into(),
GenericDef::Static(it) => it.into(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ impl Definition {
hir::GenericDef::TypeAlias(it) => it.source(db).map(|src| src.syntax().cloned()),
hir::GenericDef::Impl(it) => it.source(db).map(|src| src.syntax().cloned()),
hir::GenericDef::Const(it) => it.source(db).map(|src| src.syntax().cloned()),
hir::GenericDef::Static(it) => it.source(db).map(|src| src.syntax().cloned()),
};
return match def {
Some(def) => SearchScope::file_range(
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/hover/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ fn definition_owner_name(db: &RootDatabase, def: Definition, edition: Edition) -
None => it.name(db),
}
}
hir::GenericDef::Static(it) => Some(it.name(db)),
},
Definition::DeriveHelper(derive_helper) => Some(derive_helper.derive().name(db)),
d => {
Expand Down
4 changes: 3 additions & 1 deletion crates/ide/src/signature_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ fn signature_help_for_generics(
format_to!(res.signature, "type {}", it.name(db).display(db, edition));
}
// These don't have generic args that can be specified
hir::GenericDef::Impl(_) | hir::GenericDef::Const(_) => return None,
hir::GenericDef::Impl(_) | hir::GenericDef::Const(_) | hir::GenericDef::Static(_) => {
return None
}
}

let params = generics_def.params(sema.db);
Expand Down
Loading