Skip to content

Commit

Permalink
feat(rust-sema): propagate Array and Index into THIR
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Redondo committed Sep 17, 2023
1 parent 0bf540e commit ddaed59
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
13 changes: 10 additions & 3 deletions rust/src/sema/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ impl<'a> MirConverter<'a> {
| ThirExprInner::Read(_)
| ThirExprInner::Unreachable
| ThirExprInner::Ctor(_)
| ThirExprInner::BinaryExpr(_, _, _) => todo!(),
| ThirExprInner::BinaryExpr(_, _, _)
| ThirExprInner::Array(_)
| ThirExprInner::Index(_, _) => todo!(),
}
}

Expand Down Expand Up @@ -665,7 +667,8 @@ impl<'a> MirConverter<'a> {
| ThirExprInner::Cast(_, _)
| ThirExprInner::Tuple(_)
| ThirExprInner::Ctor(_)
| ThirExprInner::BinaryExpr(_, _, _) => unreachable!("cannot access"),
| ThirExprInner::BinaryExpr(_, _, _)
| ThirExprInner::Array(_) | ThirExprInner::Index(_, _) => unreachable!("cannot access"),
}
}

Expand Down Expand Up @@ -805,6 +808,8 @@ impl<'a> MirConverter<'a> {
Box::new(self.lower_expr(*rhs)?),
),
}),
super::tyck::ThirExprInner::Array(_) => todo!("array"),
super::tyck::ThirExprInner::Index(_, _) => todo!("index"),
super::tyck::ThirExprInner::MemberAccess(_, _) => {
panic!("only top level rvalues get lowered by `lower_expr`")
}
Expand Down Expand Up @@ -957,7 +962,9 @@ impl<'a> MirConverter<'a> {
| ThirExprInner::Cast(_, _)
| ThirExprInner::Tuple(_)
| ThirExprInner::Ctor(_)
| ThirExprInner::BinaryExpr(_, _, _) => unreachable!(),
| ThirExprInner::BinaryExpr(_, _, _)
| ThirExprInner::Array(_)
| ThirExprInner::Index(_, _) => unreachable!(),
}

Ok(())
Expand Down
55 changes: 53 additions & 2 deletions rust/src/sema/tyck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub enum ThirExprInner {
Box<Spanned<ThirExpr>>,
Box<Spanned<ThirExpr>>,
),
Array(Vec<Spanned<ThirExpr>>),
Index(Box<Spanned<ThirExpr>>, Box<Spanned<ThirExpr>>),
}

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -174,6 +176,18 @@ impl core::fmt::Display for ThirExprInner {
ThirExprInner::BinaryExpr(op, lhs, rhs) => {
write!(f, "({} {} {})", lhs.body, op.body, rhs.body)
}
ThirExprInner::Array(elements) => {
write!(f, "[")?;
let mut sep = "";
for element in elements {
write!(f, "{}{}", sep, element.body)?;
sep = ", ";
}
write!(f, "]")
}
ThirExprInner::Index(base, index) => {
write!(f, "{}[{}]", base.body, index.body)
}
}
}
}
Expand Down Expand Up @@ -670,8 +684,39 @@ impl<'a> ThirConverter<'a> {
inner: ThirExprInner::BinaryExpr(*op, Box::new(lhs), Box::new(rhs)),
})
}
hir::HirExpr::Array(_) => todo!("array"),
hir::HirExpr::Index(_, _) => todo!("index"),
hir::HirExpr::Array(elements) => {
let elements = elements
.iter()
.map(|x| self.convert_rvalue(x))
.collect::<Result<Vec<_>>>()?;
if elements.len() == 0 {
Ok(ThirExpr {
ty: Type::Inferable(InferId(self.next_infer.fetch_increment())),
cat: ValueCategory::Rvalue,
inner: ThirExprInner::Array(elements),
})
} else {
Ok(ThirExpr {
ty: elements[0].ty.clone(),
cat: ValueCategory::Rvalue,
inner: ThirExprInner::Array(elements),
})
}
}
hir::HirExpr::Index(base, index) => {
let base = self.convert_rvalue(base)?;
Ok(ThirExpr {
ty: match &base.ty {
Type::Array(ty, _) => ty.body.clone(),
_ => Type::Inferable(InferId(self.next_infer.fetch_increment())),
},
cat: ValueCategory::Rvalue,
inner: ThirExprInner::Index(
Box::new(base),
Box::new(self.convert_rvalue(index)?),
),
})
}
})
}

Expand Down Expand Up @@ -1042,6 +1087,12 @@ impl<'a> Inferer<'a> {
status &= self.unify_single_expr(lhs)?;
status &= self.unify_single_expr(rhs)?;
}
ThirExprInner::Array(_elements) => {
todo!("array")
}
ThirExprInner::Index(base, index) => {
todo!("index")
}
}

Ok(status)
Expand Down

0 comments on commit ddaed59

Please sign in to comment.