From 0acdb730769cfb040ffc5e2c87f83b19fd3ce291 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 1 Jan 2021 10:06:42 +0100 Subject: Add ConstParams to the HIR --- crates/hir/src/code_model.rs | 38 ++++++++++++++++++++++++++----- crates/hir/src/from_id.rs | 1 + crates/hir/src/has_source.rs | 12 ++++++++-- crates/hir/src/lib.rs | 6 ++--- crates/hir/src/semantics.rs | 12 ++++++---- crates/hir/src/semantics/source_to_def.rs | 18 ++++++++++++--- crates/hir/src/source_analyzer.rs | 1 + 7 files changed, 70 insertions(+), 18 deletions(-) (limited to 'crates/hir') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index b7ded3478..97b7a8b5f 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -18,10 +18,10 @@ use hir_def::{ resolver::{HasResolver, Resolver}, src::HasSource as _, type_ref::{Mutability, TypeRef}, - AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, DefWithBodyId, EnumId, - FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, LocalEnumVariantId, LocalFieldId, - LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, - UnionId, + AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, + DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId, + LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, TraitId, + TypeAliasId, TypeParamId, UnionId, }; use hir_def::{find_path::PrefixKind, item_scope::ItemInNs, visibility::Visibility}; use hir_expand::{ @@ -1125,7 +1125,12 @@ impl GenericDef { id: LifetimeParamId { parent: self.into(), local_id }, }) .map(GenericParam::LifetimeParam); - ty_params.chain(lt_params).collect() + let const_params = generics + .consts + .iter() + .map(|(local_id, _)| ConstParam { id: ConstParamId { parent: self.into(), local_id } }) + .map(GenericParam::ConstParam); + ty_params.chain(lt_params).chain(const_params).collect() } pub fn type_params(self, db: &dyn HirDatabase) -> Vec { @@ -1237,8 +1242,9 @@ impl Label { pub enum GenericParam { TypeParam(TypeParam), LifetimeParam(LifetimeParam), + ConstParam(ConstParam), } -impl_from!(TypeParam, LifetimeParam for GenericParam); +impl_from!(TypeParam, LifetimeParam, ConstParam for GenericParam); #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct TypeParam { @@ -1300,6 +1306,26 @@ impl LifetimeParam { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct ConstParam { + pub(crate) id: ConstParamId, +} + +impl ConstParam { + pub fn name(self, db: &dyn HirDatabase) -> Name { + let params = db.generic_params(self.id.parent); + params.consts[self.id.local_id].name.clone() + } + + pub fn module(self, db: &dyn HirDatabase) -> Module { + self.id.parent.module(db.upcast()).into() + } + + pub fn parent(self, _db: &dyn HirDatabase) -> GenericDef { + self.id.parent.into() + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Impl { pub(crate) id: ImplId, diff --git a/crates/hir/src/from_id.rs b/crates/hir/src/from_id.rs index a0792b9a6..2422887e3 100644 --- a/crates/hir/src/from_id.rs +++ b/crates/hir/src/from_id.rs @@ -44,6 +44,7 @@ from_id![ (hir_def::ImplId, crate::Impl), (hir_def::TypeParamId, crate::TypeParam), (hir_def::LifetimeParamId, crate::LifetimeParam), + (hir_def::ConstParamId, crate::ConstParam), (hir_expand::MacroDefId, crate::MacroDef) ]; diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 0dc07c33e..dd7c0c570 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -10,8 +10,8 @@ use hir_expand::InFile; use syntax::ast; use crate::{ - db::HirDatabase, Const, Enum, Field, FieldSource, Function, Impl, LifetimeParam, MacroDef, - Module, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant, + db::HirDatabase, Const, ConstParam, Enum, Field, FieldSource, Function, Impl, LifetimeParam, + MacroDef, Module, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant, }; pub trait HasSource { @@ -140,3 +140,11 @@ impl HasSource for LifetimeParam { child_source.map(|it| it[self.id.local_id].clone()) } } + +impl HasSource for ConstParam { + type Ast = ast::ConstParam; + fn source(self, db: &dyn HirDatabase) -> InFile { + let child_source = self.id.parent.child_source(db.upcast()); + child_source.map(|it| it[self.id.local_id].clone()) + } +} diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 7ac9fd507..8ac27e2dd 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -34,9 +34,9 @@ pub use crate::{ attrs::{HasAttrs, Namespace}, code_model::{ Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const, - Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function, GenericDef, - HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, ScopeDef, - Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant, VariantDef, + ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function, + GenericDef, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef, Module, ModuleDef, + ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant, VariantDef, }, has_source::HasSource, semantics::{PathResolution, Semantics, SemanticsScope}, diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 67cd16e31..cd689c869 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -25,9 +25,9 @@ use crate::{ diagnostics::Diagnostic, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, source_analyzer::{resolve_hir_path, SourceAnalyzer}, - AssocItem, Callable, Crate, Field, Function, HirFileId, Impl, InFile, Label, LifetimeParam, - Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, - VariantDef, + AssocItem, Callable, ConstParam, Crate, Field, Function, HirFileId, Impl, InFile, Label, + LifetimeParam, Local, MacroDef, Module, ModuleDef, Name, Path, ScopeDef, Trait, Type, + TypeAlias, TypeParam, VariantDef, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -38,6 +38,7 @@ pub enum PathResolution { Local(Local), /// A generic parameter TypeParam(TypeParam), + ConstParam(ConstParam), SelfType(Impl), Macro(MacroDef), AssocItem(AssocItem), @@ -59,7 +60,9 @@ impl PathResolution { PathResolution::Def(ModuleDef::TypeAlias(alias)) => { Some(TypeNs::TypeAliasId((*alias).into())) } - PathResolution::Local(_) | PathResolution::Macro(_) => None, + PathResolution::Local(_) | PathResolution::Macro(_) | PathResolution::ConstParam(_) => { + None + } PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())), PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())), PathResolution::AssocItem(AssocItem::Const(_)) @@ -744,6 +747,7 @@ to_def_impls![ (crate::Variant, ast::Variant, enum_variant_to_def), (crate::TypeParam, ast::TypeParam, type_param_to_def), (crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def), + (crate::ConstParam, ast::ConstParam, const_param_to_def), (crate::MacroDef, ast::MacroRules, macro_rules_to_def), (crate::Local, ast::IdentPat, bind_pat_to_def), (crate::Label, ast::Label, label_to_def), diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 424e6e8a9..4b9ebff72 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -6,9 +6,9 @@ use hir_def::{ dyn_map::DynMap, expr::{LabelId, PatId}, keys::{self, Key}, - ConstId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, ImplId, - LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, - VariantId, + ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, + ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, + UnionId, VariantId, }; use hir_expand::{name::AsName, AstId, MacroDefKind}; use rustc_hash::FxHashMap; @@ -157,6 +157,18 @@ impl SourceToDefCtx<'_, '_> { dyn_map[keys::LIFETIME_PARAM].get(&src).copied() } + pub(super) fn const_param_to_def( + &mut self, + src: InFile, + ) -> Option { + let container: ChildContainer = + self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into(); + let db = self.db; + let dyn_map = + &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); + dyn_map[keys::CONST_PARAM].get(&src).copied() + } + // FIXME: use DynMap as well? pub(super) fn macro_rules_to_def( &mut self, diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index bddc49c05..30a8e513d 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -479,6 +479,7 @@ pub(crate) fn resolve_hir_path( ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()), ValueNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()), ValueNs::ImplSelf(impl_id) => PathResolution::SelfType(impl_id.into()), + ValueNs::GenericParam(it) => PathResolution::ConstParam(it.into()), }; Some(res) }); -- cgit v1.2.3