Skip to content
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

Improve type check #1045

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions crates/hir-analysis/src/ty/ty_check/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl<'db> BlockEnv<'db> {
pub struct ExprProp<'db> {
pub ty: TyId<'db>,
pub is_mut: bool,
pub(crate) binding: Option<LocalBinding<'db>>,
pub binding: Option<LocalBinding<'db>>,
}

impl<'db> ExprProp<'db> {
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions crates/hir-analysis/src/ty/ty_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ 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},
span::{expr::LazyExprSpan, pat::LazyPatSpan, DynLazySpan},
visitor::{walk_expr, walk_pat, Visitor, VisitorCtxt},
};
pub(super) use path::RecordLike;

use rustc_hash::{FxHashMap, FxHashSet};

use super::{
Expand Down Expand Up @@ -234,13 +233,26 @@ 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<LocalBinding<'db>> {
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)
.copied()
.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)
}
Expand Down
Loading