From b63234e20be04ae0e7eca20e0bf49dca8d7d3718 Mon Sep 17 00:00:00 2001 From: OleStrohm Date: Sat, 6 Aug 2022 19:12:57 +0200 Subject: [PATCH] Cleaned up code --- crates/hir-ty/src/infer.rs | 6 +----- crates/hir/src/lib.rs | 14 +++++++++----- crates/hir/src/symbols.rs | 5 ++--- crates/ide/src/hover/render.rs | 5 ++--- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs index 63d0f1b01cf..c821a3786b9 100644 --- a/crates/hir-ty/src/infer.rs +++ b/crates/hir-ty/src/infer.rs @@ -26,7 +26,7 @@ resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs}, type_ref::TypeRef, AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, HasModule, Lookup, - TraitId, TypeAliasId, VariantId, + TraitId, TypeAliasId, VariantId }; use hir_expand::name::{name, Name}; use itertools::Either; @@ -68,10 +68,6 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc ctx.collect_fn(f), DefWithBodyId::StaticId(s) => ctx.collect_static(&db.static_data(s)), DefWithBodyId::VariantId(v) => { - //let def = AttrDefId::EnumVariantId(v); - //let attrs = db.attrs(def); - //let repr = attrs.by_key("repr").attrs().next().unwrap(); - //let ident = repr.single_ident_value().unwrap().text; // TODO(ole): Get the real type ctx.return_ty = TyBuilder::def_ty(db, v.parent.into()).fill_with_unknown().build() } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 258224a7584..1b06dbd9085 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -967,11 +967,6 @@ pub struct Variant { } impl Variant { - pub fn value(self, db: &dyn HirDatabase) -> Option { - // TODO(ole): Handle missing exprs (+1 to the prev) - self.source(db)?.value.expr() - } - pub fn module(self, db: &dyn HirDatabase) -> Module { self.parent.module(db) } @@ -999,6 +994,15 @@ pub fn kind(self, db: &dyn HirDatabase) -> StructKind { pub(crate) fn variant_data(self, db: &dyn HirDatabase) -> Arc { db.enum_data(self.parent.id).variants[self.id].variant_data.clone() } + + pub fn value(self, db: &dyn HirDatabase) -> Option { + // TODO(ole): Handle missing exprs (+1 to the prev) + self.source(db)?.value.expr() + } + + pub fn eval(self, db: &dyn HirDatabase) -> Result { + db.const_eval_variant(self.into()) + } } /// Variants inherit visibility from the parent enum. diff --git a/crates/hir/src/symbols.rs b/crates/hir/src/symbols.rs index 8432f0e7e01..fd78decda4e 100644 --- a/crates/hir/src/symbols.rs +++ b/crates/hir/src/symbols.rs @@ -1,7 +1,6 @@ //! File symbol extraction. use base_db::FileRange; -use hir_def::db::DefDatabase; use hir_def::{ item_tree::ItemTreeNode, src::HasSource, AdtId, AssocItemId, AssocItemLoc, DefWithBodyId, HasModule, ImplId, ItemContainerId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId, @@ -246,8 +245,8 @@ fn def_with_body_id_name(&self, body_id: DefWithBodyId) -> Option { id.lookup(self.db.upcast()).source(self.db.upcast()).value.name()?.text().into(), ), DefWithBodyId::VariantId(id) => Some({ - let up_db: &dyn DefDatabase = self.db.upcast(); - up_db.lookup_intern_enum(id.parent).source(up_db).value.name()?.text().into() + let db = self.db.upcast(); + id.parent.lookup(db).source(db).value.name()?.text().into() }), } } diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index cd63131e7a7..8ac268f2438 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -349,11 +349,10 @@ pub(super) fn definition( Definition::Function(it) => label_and_docs(db, it), Definition::Adt(it) => label_and_docs(db, it), Definition::Variant(it) => label_value_and_docs(db, it, |&it| { - let hir_db: &dyn HirDatabase = db; - let body = hir_db.const_eval_variant(it.into()); + let body = it.eval(db); match body { Ok(x) => Some(format!("{}", x)), - Err(_) => it.value(db).map(|s| format!("{}", s)), + Err(_) => it.value(db).map(|x| format!("{}", x)), } }), Definition::Const(it) => label_value_and_docs(db, it, |it| {