From dab458a29c57f75d01b84e5ad4fa9767f2b9b676 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Mon, 20 Jan 2025 15:25:40 +0100 Subject: [PATCH] Add missing getters for local bindings and callables --- crates/hir-analysis/src/ty/ty_check/env.rs | 5 +++-- crates/hir-analysis/src/ty/ty_check/mod.rs | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/hir-analysis/src/ty/ty_check/env.rs b/crates/hir-analysis/src/ty/ty_check/env.rs index 0827ff2bd..2cc4b7526 100644 --- a/crates/hir-analysis/src/ty/ty_check/env.rs +++ b/crates/hir-analysis/src/ty/ty_check/env.rs @@ -334,7 +334,7 @@ impl<'db> BlockEnv<'db> { pub struct ExprProp<'db> { pub ty: TyId<'db>, pub is_mut: bool, - pub(crate) binding: Option>, + pub binding: Option>, } impl<'db> ExprProp<'db> { @@ -371,8 +371,9 @@ impl<'db> ExprProp<'db> { } } +/// Represents a definition site of a local variable. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(crate) enum LocalBinding<'db> { +pub enum LocalBinding<'db> { Local { pat: PatId, is_mut: bool, diff --git a/crates/hir-analysis/src/ty/ty_check/mod.rs b/crates/hir-analysis/src/ty/ty_check/mod.rs index b8b52588a..849e4f095 100644 --- a/crates/hir-analysis/src/ty/ty_check/mod.rs +++ b/crates/hir-analysis/src/ty/ty_check/mod.rs @@ -8,7 +8,7 @@ mod stmt; pub use callable::Callable; pub use env::ExprProp; -use env::TyCheckEnv; +use env::{LocalBinding, TyCheckEnv}; pub(super) use expr::TraitOps; use hir::{ hir_def::{Body, Expr, ExprId, Func, LitKind, Pat, PatId, PathId, TypeId as HirTyId}, @@ -16,7 +16,6 @@ use hir::{ visitor::{walk_expr, walk_pat, Visitor, VisitorCtxt}, }; pub(super) use path::RecordLike; - use rustc_hash::{FxHashMap, FxHashSet}; use super::{ @@ -234,6 +233,15 @@ impl<'db> TypedBody<'db> { .unwrap_or_else(|| ExprProp::invalid(db)) } + /// Returns a local variable binding information. + /// Returns `None` if + /// * Expression is not a local variable, or + /// * Binding is missing in a user code. + pub fn binding_for(&self, expr: ExprId) -> Option> { + let expr_prop = self.expr_ty.get(&expr)?; + expr_prop.binding + } + pub fn pat_ty(&self, db: &'db dyn HirAnalysisDb, pat: PatId) -> TyId<'db> { self.pat_ty .get(&pat) @@ -241,6 +249,10 @@ impl<'db> TypedBody<'db> { .unwrap_or_else(|| TyId::invalid(db, InvalidCause::Other)) } + /// Returns the callee information. + /// Returns `None` if + /// * the given `expr` is not a call or method call, or + /// * the given `expr` isn't typed properly. pub fn callable_expr(&self, expr: ExprId) -> Option<&Callable<'db>> { self.callables.get(&expr) }