mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-03 01:42:14 +00:00
fix: simplify all substructure (#248)
This commit is contained in:
parent
73a5742c09
commit
4fae99d767
5 changed files with 68 additions and 28 deletions
|
@ -385,14 +385,14 @@ impl<'a, 'w> TypeChecker<'a, 'w> {
|
||||||
|
|
||||||
let op = unary.op();
|
let op = unary.op();
|
||||||
|
|
||||||
let expr = Box::new(self.check_expr_in(unary.expr().span(), root));
|
let lhs = Box::new(self.check_expr_in(unary.expr().span(), root));
|
||||||
let ty = match op {
|
let op = match op {
|
||||||
ast::UnOp::Pos => FlowUnaryType::Pos(expr),
|
ast::UnOp::Pos => UnaryOp::Pos,
|
||||||
ast::UnOp::Neg => FlowUnaryType::Neg(expr),
|
ast::UnOp::Neg => UnaryOp::Neg,
|
||||||
ast::UnOp::Not => FlowUnaryType::Not(expr),
|
ast::UnOp::Not => UnaryOp::Not,
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(FlowType::Unary(ty))
|
Some(FlowType::Unary(FlowUnaryType { op, lhs }))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_binary(&mut self, root: LinkedNode<'_>) -> Option<FlowType> {
|
fn check_binary(&mut self, root: LinkedNode<'_>) -> Option<FlowType> {
|
||||||
|
@ -635,7 +635,10 @@ impl<'a, 'w> TypeChecker<'a, 'w> {
|
||||||
|
|
||||||
let body = self.check_expr_in(contextual.body().span(), root);
|
let body = self.check_expr_in(contextual.body().span(), root);
|
||||||
|
|
||||||
Some(FlowType::Unary(FlowUnaryType::Context(Box::new(body))))
|
Some(FlowType::Unary(FlowUnaryType {
|
||||||
|
op: UnaryOp::Context,
|
||||||
|
lhs: Box::new(body),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_conditional(&mut self, root: LinkedNode<'_>) -> Option<FlowType> {
|
fn check_conditional(&mut self, root: LinkedNode<'_>) -> Option<FlowType> {
|
||||||
|
@ -1640,19 +1643,30 @@ impl<'a, 'b> TypeSimplifier<'a, 'b> {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
FlowType::Unary(u) => {
|
FlowType::Unary(u) => {
|
||||||
let u2 = u.clone();
|
let lhs = self.transform(u.lhs(), pol);
|
||||||
|
FlowType::Unary(FlowUnaryType {
|
||||||
FlowType::Unary(u2)
|
op: u.op,
|
||||||
|
lhs: Box::new(lhs),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
FlowType::Binary(b) => {
|
FlowType::Binary(b) => {
|
||||||
let b2 = b.clone();
|
let (lhs, rhs) = b.repr();
|
||||||
|
let lhs = self.transform(lhs, pol);
|
||||||
|
let rhs = self.transform(rhs, pol);
|
||||||
|
|
||||||
FlowType::Binary(b2)
|
FlowType::Binary(FlowBinaryType {
|
||||||
|
op: b.op,
|
||||||
|
operands: Box::new((lhs, rhs)),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
FlowType::If(i) => {
|
FlowType::If(i) => {
|
||||||
let i2 = i.clone();
|
let i2 = *i.clone();
|
||||||
|
|
||||||
FlowType::If(i2)
|
FlowType::If(Box::new(FlowIfType {
|
||||||
|
cond: self.transform(&i2.cond, pol),
|
||||||
|
then: self.transform(&i2.then, pol),
|
||||||
|
else_: self.transform(&i2.else_, pol),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
FlowType::Union(v) => {
|
FlowType::Union(v) => {
|
||||||
let v2 = v.iter().map(|ty| self.transform(ty, pol)).collect();
|
let v2 = v.iter().map(|ty| self.transform(ty, pol)).collect();
|
||||||
|
@ -1665,9 +1679,10 @@ impl<'a, 'b> TypeSimplifier<'a, 'b> {
|
||||||
FlowType::Field(Box::new((x, self.transform(&y, pol), z)))
|
FlowType::Field(Box::new((x, self.transform(&y, pol), z)))
|
||||||
}
|
}
|
||||||
FlowType::At(a) => {
|
FlowType::At(a) => {
|
||||||
let a2 = a.clone();
|
let FlowAt(at) = a.clone();
|
||||||
|
let atee = self.transform(&at.0, pol);
|
||||||
|
|
||||||
FlowType::At(a2)
|
FlowType::At(FlowAt(Box::new((atee, at.1))))
|
||||||
}
|
}
|
||||||
|
|
||||||
FlowType::Value(v) => FlowType::Value(v.clone()),
|
FlowType::Value(v) => FlowType::Value(v.clone()),
|
||||||
|
|
|
@ -214,22 +214,23 @@ impl<'a> Iterator for UnionIter<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Hash)]
|
||||||
|
pub(crate) enum UnaryOp {
|
||||||
|
Pos,
|
||||||
|
Neg,
|
||||||
|
Not,
|
||||||
|
Context,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Hash)]
|
#[derive(Debug, Clone, Hash)]
|
||||||
pub(crate) enum FlowUnaryType {
|
pub(crate) struct FlowUnaryType {
|
||||||
Pos(Box<FlowType>),
|
pub op: UnaryOp,
|
||||||
Neg(Box<FlowType>),
|
pub lhs: Box<FlowType>,
|
||||||
Not(Box<FlowType>),
|
|
||||||
Context(Box<FlowType>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FlowUnaryType {
|
impl FlowUnaryType {
|
||||||
pub fn lhs(&self) -> &FlowType {
|
pub fn lhs(&self) -> &FlowType {
|
||||||
match self {
|
&self.lhs
|
||||||
FlowUnaryType::Pos(e) => e,
|
|
||||||
FlowUnaryType::Neg(e) => e,
|
|
||||||
FlowUnaryType::Not(e) => e,
|
|
||||||
FlowUnaryType::Context(e) => e,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
crates/tinymist-query/src/fixtures/type_check/external.typ
Normal file
10
crates/tinymist-query/src/fixtures/type_check/external.typ
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// path: base.typ
|
||||||
|
#let bad-instantiate(x) = {
|
||||||
|
let y; let z; let w;
|
||||||
|
x + y + z + w
|
||||||
|
};
|
||||||
|
-----
|
||||||
|
#import "base.typ": *
|
||||||
|
#let prefix(title: none) = {
|
||||||
|
bad-instantiate(title)
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ input_file: crates/tinymist-query/src/fixtures/type_check/control_flow.typ
|
||||||
---
|
---
|
||||||
"x0" = FlowIfType { cond: true, then: 1, else_: None }
|
"x0" = FlowIfType { cond: true, then: 1, else_: None }
|
||||||
"x1" = FlowIfType { cond: false, then: 2, else_: None }
|
"x1" = FlowIfType { cond: false, then: 2, else_: None }
|
||||||
"x2" = Context(FlowIfType { cond: FlowBinaryType { op: Gt, operands: (Any, 0) }, then: 1, else_: 2 })
|
"x2" = FlowUnaryType { op: Context, lhs: FlowIfType { cond: FlowBinaryType { op: Gt, operands: (Any, 0) }, then: 1, else_: 2 } }
|
||||||
---
|
---
|
||||||
5..7 -> @x0
|
5..7 -> @x0
|
||||||
31..33 -> @x1
|
31..33 -> @x1
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
source: crates/tinymist-query/src/analysis.rs
|
||||||
|
expression: result
|
||||||
|
input_file: crates/tinymist-query/src/fixtures/type_check/external.typ
|
||||||
|
---
|
||||||
|
"bad-instantiate" = Any
|
||||||
|
"prefix" = (, title: ( ⪯ Any)) -> FlowBinaryType { op: Add, operands: (FlowBinaryType { op: Add, operands: (FlowBinaryType { op: Add, operands: (Any, Infer) }, Infer) }, Infer) }
|
||||||
|
"title" = None
|
||||||
|
---
|
||||||
|
27..33 -> @prefix
|
||||||
|
34..39 -> @title
|
||||||
|
53..68 -> (@bad-instantiate | (Any) -> FlowBinaryType { op: Add, operands: (FlowBinaryType { op: Add, operands: (FlowBinaryType { op: Add, operands: (Any, Infer) }, Infer) }, Infer) })
|
||||||
|
53..75 -> FlowBinaryType { op: Add, operands: (FlowBinaryType { op: Add, operands: (FlowBinaryType { op: Add, operands: (Any, Infer) }, Infer) }, Infer) }
|
||||||
|
69..74 -> @title
|
Loading…
Add table
Add a link
Reference in a new issue