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_ty/src/db.rs | 7 +++++-- crates/hir_ty/src/infer/path.rs | 1 + crates/hir_ty/src/lower.rs | 15 ++++++++++++--- crates/hir_ty/src/tests/simple.rs | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) (limited to 'crates/hir_ty/src') diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index 66bdb8e88..f3567c49e 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs @@ -5,8 +5,8 @@ use std::sync::Arc; use arena::map::ArenaMap; use base_db::{impl_intern_key, salsa, CrateId, Upcast}; use hir_def::{ - db::DefDatabase, expr::ExprId, DefWithBodyId, FunctionId, GenericDefId, ImplId, LocalFieldId, - TypeParamId, VariantId, + db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId, + LocalFieldId, TypeParamId, VariantId, }; use crate::{ @@ -37,6 +37,9 @@ pub trait HirDatabase: DefDatabase + Upcast { #[salsa::cycle(crate::lower::impl_self_ty_recover)] fn impl_self_ty(&self, def: ImplId) -> Binders; + #[salsa::invoke(crate::lower::const_param_ty_query)] + fn const_param_ty(&self, def: ConstParamId) -> Ty; + #[salsa::invoke(crate::lower::impl_trait_query)] fn impl_trait(&self, def: ImplId) -> Option>; diff --git a/crates/hir_ty/src/infer/path.rs b/crates/hir_ty/src/infer/path.rs index 80d7ed10e..5d541104e 100644 --- a/crates/hir_ty/src/infer/path.rs +++ b/crates/hir_ty/src/infer/path.rs @@ -89,6 +89,7 @@ impl<'a> InferenceContext<'a> { return None; } } + ValueNs::GenericParam(it) => return Some(self.db.const_param_ty(it)), }; let ty = self.db.value_ty(typable); diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 8da56cd11..222f61a11 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -16,9 +16,9 @@ use hir_def::{ path::{GenericArg, Path, PathSegment, PathSegments}, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, - AdtId, AssocContainerId, AssocItemId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, - HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, - UnionId, VariantId, + AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, + GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId, + TypeAliasId, TypeParamId, UnionId, VariantId, }; use hir_expand::name::Name; use smallvec::SmallVec; @@ -1221,6 +1221,15 @@ pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binde Binders::new(generics.len(), Ty::from_hir(&ctx, &impl_data.target_type)) } +pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty { + let parent_data = db.generic_params(def.parent); + let data = &parent_data.consts[def.local_id]; + let resolver = def.parent.resolver(db.upcast()); + let ctx = TyLoweringContext::new(db, &resolver); + + Ty::from_hir(&ctx, &data.ty) +} + pub(crate) fn impl_self_ty_recover( db: &dyn HirDatabase, _cycle: &[String], diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index a61282d5a..bd6beea99 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -2375,3 +2375,20 @@ fn infer_operator_overload() { "#]], ); } + + +#[test] +fn infer_const_params() { + check_infer( + r#" + fn foo() { + let bar = FOO; + } + "#, + expect![[r#" + 27..49 '{ ...FOO; }': () + 37..40 'bar': usize + 43..46 'FOO': usize + "#]], + ); +} -- cgit v1.2.3