Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Aug 22, 2024
1 parent d34f878 commit c208212
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/hir-analysis/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod diagnostics;
pub mod fold;
pub mod func_def;
pub mod method_table;
pub mod pattern_analysis;
pub mod trait_def;
pub mod trait_lower;
pub mod trait_resolution;
Expand Down
48 changes: 48 additions & 0 deletions crates/hir-analysis/src/ty/pattern_analysis/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use hir::hir_def::{IntegerId, PatId, StringId};

use super::{ty_check::TypedBody, ty_def::TyId};
use crate::HirAnalysisDb;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimplifiedPat<'db> {
ctor: PatCtor<'db>,
args: Vec<Self>,
hir_pat: PatId,
ty: TyId<'db>,
}

impl<'db> SimplifiedPat<'db> {
pub fn new(
ctor: PatCtor<'db>,
args: Vec<SimplifiedPat<'db>>,
hir_pat: PatId,
ty: TyId<'db>,
) -> Self {
Self {
ctor,
args,
hir_pat,
ty,
}
}

pub fn simplify(db: &'db dyn HirAnalysisDb, pat: PatId, body: TypedBody) -> Self {
let ty = body.pat_ty(db, pat);
assert!(!ty.has_invalid(db));

todo!()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatCtor<'db> {
Or,
WildCard,
Tuple,
Struct,
Variant(u32),
// FIXME: Extend this to `IntRange` when we add range pattern.
Int(IntegerId<'db>),
String(StringId<'db>),
Bool(bool),
}
26 changes: 23 additions & 3 deletions crates/hir-analysis/src/ty/ty_check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'db> TyChecker<'db> {
(base, args) if base.is_tuple(self.db) => Some(args.len()),
_ => None,
};
let (actual, rest_range) = self.unpack_rest_pat(pat_tup, expected_len);
let (actual, rest_range) = self.unpack_tuple_pat(pat_tup, expected_len);
let actual = TyId::tuple_with_elems(self.db, &actual);

let unified = self.unify_ty(pat, actual, expected);
Expand Down Expand Up @@ -243,7 +243,7 @@ impl<'db> TyChecker<'db> {
let pat_ty = variant.ty(self.db);
let expected_len = expected_elems.len(self.db.as_hir_db());

let (actual_elems, rest_range) = self.unpack_rest_pat(elems, Some(expected_len));
let (actual_elems, rest_range) = self.unpack_tuple_pat(elems, Some(expected_len));
if actual_elems.len() != expected_len {
let diag = BodyDiag::MismatchedFieldCount {
primary: pat.lazy_span(self.body()).into(),
Expand Down Expand Up @@ -403,7 +403,27 @@ impl<'db> TyChecker<'db> {
}
}

fn unpack_rest_pat(
/// Unpacks a tuple pattern, identifying the location of any rest
/// patterns and generating the appropriate types for each element in
/// the tuple.
///
/// # Parameters
/// - `pat_tup`: A slice of pattern representing the elements of the tuple
/// pattern.
/// - `expected_len`: An optional expected length for the tuple.
///
/// # Returns
/// A tuple containing:
/// - A vector of type for each element in the tuple.
/// - A range indicating the position of the rest pattern within the tuple,
/// if present.
///
/// # Notes
/// - If there are multiple rest patterns, a diagnostic message is
/// generated.
/// - If the minimum length of the tuple pattern exceeds the expected
/// length, a default range is returned.
fn unpack_tuple_pat(
&mut self,
pat_tup: &[PatId],
expected_len: Option<usize>,
Expand Down

0 comments on commit c208212

Please sign in to comment.