Add BindingId

This commit is contained in:
hkalbasi 2023-02-19 00:02:55 +03:30
parent a360fab9a3
commit 61ad6a96ad
27 changed files with 514 additions and 297 deletions

View file

@ -22,7 +22,7 @@ use hir_def::{
body::Body,
builtin_type::{BuiltinInt, BuiltinType, BuiltinUint},
data::{ConstData, StaticData},
expr::{BindingAnnotation, ExprId, ExprOrPatId, PatId},
expr::{BindingAnnotation, BindingId, ExprId, ExprOrPatId, PatId},
lang_item::{LangItem, LangItemTarget},
layout::Integer,
path::Path,
@ -352,6 +352,7 @@ pub struct InferenceResult {
/// **Note**: When a pattern type is resolved it may still contain
/// unresolved or missing subpatterns or subpatterns of mismatched types.
pub type_of_pat: ArenaMap<PatId, Ty>,
pub type_of_binding: ArenaMap<BindingId, Ty>,
pub type_of_rpit: ArenaMap<RpitId, Ty>,
type_mismatches: FxHashMap<ExprOrPatId, TypeMismatch>,
/// Interned common types to return references to.
@ -414,6 +415,14 @@ impl Index<PatId> for InferenceResult {
}
}
impl Index<BindingId> for InferenceResult {
type Output = Ty;
fn index(&self, b: BindingId) -> &Ty {
self.type_of_binding.get(b).unwrap_or(&self.standard_types.unknown)
}
}
/// The inference context contains all information needed during type inference.
#[derive(Clone, Debug)]
pub(crate) struct InferenceContext<'a> {
@ -534,7 +543,10 @@ impl<'a> InferenceContext<'a> {
for ty in result.type_of_pat.values_mut() {
*ty = table.resolve_completely(ty.clone());
}
for ty in result.type_of_rpit.iter_mut().map(|x| x.1) {
for ty in result.type_of_binding.values_mut() {
*ty = table.resolve_completely(ty.clone());
}
for ty in result.type_of_rpit.values_mut() {
*ty = table.resolve_completely(ty.clone());
}
for mismatch in result.type_mismatches.values_mut() {
@ -704,6 +716,10 @@ impl<'a> InferenceContext<'a> {
self.result.type_of_pat.insert(pat, ty);
}
fn write_binding_ty(&mut self, id: BindingId, ty: Ty) {
self.result.type_of_binding.insert(id, ty);
}
fn push_diagnostic(&mut self, diagnostic: InferenceDiagnostic) {
self.result.diagnostics.push(diagnostic);
}