mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-08-03 17:58:17 +00:00
dev: simplify let type (#844)
* fix: simplify let type * test: update snapshot
This commit is contained in:
parent
a9800bc802
commit
8a15eb9d66
26 changed files with 44 additions and 51 deletions
|
@ -3,6 +3,5 @@ source: crates/tinymist-query/src/analysis.rs
|
|||
description: "Check on \"(\" (30)"
|
||||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/text_font2.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
( ⪯ TextFont)
|
||||
TextFont
|
||||
|
|
|
@ -3,6 +3,5 @@ source: crates/tinymist-query/src/analysis.rs
|
|||
description: "Check on \"(\" (31)"
|
||||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/text_font4.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
( ⪯ TextFont)
|
||||
TextFont
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \"\\\"Test\\\"\" (34)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/text_font_element.typ
|
||||
---
|
||||
( ⪰ (TextFont | Array<TextFont>) | TextFont)
|
||||
( ⪰ TextFont | (TextFont | Array<TextFont>))
|
||||
|
|
|
@ -3,6 +3,5 @@ source: crates/tinymist-query/src/analysis.rs
|
|||
description: "Check on \"\\\"Test\\\"\" (31)"
|
||||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/text_font_element4.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
( ⪯ TextFont)
|
||||
TextFont
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \")\" (69)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/text_stroke2.typ
|
||||
---
|
||||
"stroke": ( ⪯ Stroke)
|
||||
"stroke": Stroke
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \"(\" (48)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/text_stroke4.typ
|
||||
---
|
||||
"stroke": ( ⪯ Stroke)
|
||||
"stroke": Stroke
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \":\" (34)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/user_external_ever.typ
|
||||
---
|
||||
( ⪰ "article" | "article" | "letter" | "article" | "letter")
|
||||
( ⪰ "article" | "letter")
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \",\" (83)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/user_func_pos.typ
|
||||
---
|
||||
"stroke": ( ⪯ Stroke)
|
||||
"stroke": Stroke
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \")\" (98)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/user_named.typ
|
||||
---
|
||||
( ⪰ "content": None | "font": ( ⪰ None | None ⪯ (TextFont | Array<TextFont>)))
|
||||
( ⪰ "content": None | "font": ( ⪰ None ⪯ (TextFont | Array<TextFont>)))
|
||||
|
|
|
@ -3,6 +3,5 @@ source: crates/tinymist-query/src/analysis.rs
|
|||
description: "Check on \"(\" (18)"
|
||||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/with_element.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
( ⪰ "body": Type(content) | "alternates": Type(bool) | "baseline": Type(length) | "bottom-edge": (Type(length) | "baseline" | "bounds" | "descender") | "cjk-latin-spacing": (Type(auto) | Type(none)) | "costs": Type(dictionary) | "dir": Dir | "discretionary-ligatures": Type(bool) | "fallback": Type(bool) | "features": (Type(array) | Type(dictionary)) | "fill": Color | "font": (TextFont | Array<TextFont>) | "fractions": Type(bool) | "historical-ligatures": Type(bool) | "hyphenate": (Type(auto) | Type(bool)) | "kerning": Type(bool) | "lang": TextLang | "ligatures": Type(bool) | "number-type": (Type(auto) | "lining" | "old-style") | "number-width": (Type(auto) | "proportional" | "tabular") | "overhang": Type(bool) | "region": TextRegion | "script": (Type(auto) | Type(str)) | "size": TextSize | "slashed-zero": Type(bool) | "spacing": Type(relative) | "stretch": Type(ratio) | "stroke": Stroke | "style": ("italic" | "normal" | "oblique") | "stylistic-set": (Type(array) | Type(int) | Type(none)) | "top-edge": (Type(length) | "ascender" | "baseline" | "bounds" | "cap-height" | "x-height") | "tracking": Type(length) | "weight": (Type(int) | "black" | "bold" | "extrabold" | "extralight" | "light" | "medium" | "regular" | "semibold" | "thin"))
|
||||
( ⪰ "alternates": Type(bool) | "baseline": Type(length) | "body": Type(content) | "bottom-edge": (Type(length) | "baseline" | "bounds" | "descender") | "cjk-latin-spacing": (Type(auto) | Type(none)) | "costs": Type(dictionary) | "dir": Dir | "discretionary-ligatures": Type(bool) | "fallback": Type(bool) | "features": (Type(array) | Type(dictionary)) | "fill": Color | "font": (TextFont | Array<TextFont>) | "fractions": Type(bool) | "historical-ligatures": Type(bool) | "hyphenate": (Type(auto) | Type(bool)) | "kerning": Type(bool) | "lang": TextLang | "ligatures": Type(bool) | "number-type": (Type(auto) | "lining" | "old-style") | "number-width": (Type(auto) | "proportional" | "tabular") | "overhang": Type(bool) | "region": TextRegion | "script": (Type(auto) | Type(str)) | "size": TextSize | "slashed-zero": Type(bool) | "spacing": Type(relative) | "stretch": Type(ratio) | "stroke": Stroke | "style": ("italic" | "normal" | "oblique") | "stylistic-set": (Type(array) | Type(int) | Type(none)) | "top-edge": (Type(length) | "ascender" | "baseline" | "bounds" | "cap-height" | "x-height") | "tracking": Type(length) | "weight": (Type(int) | "black" | "bold" | "extrabold" | "extralight" | "light" | "medium" | "regular" | "semibold" | "thin"))
|
||||
|
|
|
@ -4,4 +4,4 @@ description: "Check on \"(\" (72)"
|
|||
expression: literal_type
|
||||
input_file: crates/tinymist-query/src/fixtures/post_type_check/with_user_func.typ
|
||||
---
|
||||
"font": ( ⪯ (TextFont | Array<TextFont>))
|
||||
"font": (TextFont | Array<TextFont>)
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/annotation_fn.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"touying-fn-wrapper" = ((( ⪯ Type(function)), "max-repetitions": ( ⪯ Type(int)), "repetitions": ( ⪯ Type(int)), ...: ( ⪯ Any)) => None).with(..("max-repetitions": None, "repetitions": None) => any)
|
||||
"touying-fn-wrapper" = ((Type(function), "max-repetitions": Type(int), "repetitions": Type(int), ...: Any) => None).with(..("max-repetitions": None, "repetitions": None) => any)
|
||||
"fn" = Any
|
||||
"max-repetitions" = None
|
||||
"repetitions" = None
|
||||
|
|
|
@ -2,10 +2,9 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/annotation_fn2.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"args" = Any
|
||||
"fn-wrapper" = (( ⪯ (Type(function) | (...: Any) => Any)), ...: ( ⪯ Any)) => None
|
||||
"fn-wrapper" = ((Type(function) | (...: Any) => Any), ...: Any) => None
|
||||
"fn" = Any
|
||||
"args" = Args
|
||||
---
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/annotation_sum.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"sum" = (...: ( ⪯ Array<Type(int)>)) => None
|
||||
"sum" = (...: Array<Type(int)>) => None
|
||||
"args" = Args
|
||||
---
|
||||
65..68 -> @sum
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/bug_cite_func_infer.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"cite_prose" = (( ⪯ RefLabel)) => Element(ref)
|
||||
"cite_prose" = (RefLabel) => Element(ref)
|
||||
"labl" = Any
|
||||
"cite_prose_different_name" = (( ⪯ RefLabel)) => Element(ref)
|
||||
"cite_prose_different_name" = (RefLabel) => Element(ref)
|
||||
"labl" = Any
|
||||
---
|
||||
5..15 -> @cite_prose
|
||||
|
|
|
@ -5,7 +5,7 @@ input_file: crates/tinymist-query/src/fixtures/type_check/external.typ
|
|||
---
|
||||
"base" = Any
|
||||
"bad-instantiate" = Any
|
||||
"prefix" = (("title": ( ⪯ Any)) => TypeBinary { operands: (TypeBinary { operands: (TypeBinary { operands: (Any, None), op: Add }, None), op: Add }, None), op: Add }).with(..("title": None) => any)
|
||||
"prefix" = (("title": Any) => TypeBinary { operands: (TypeBinary { operands: (TypeBinary { operands: (Any, None), op: Add }, None), op: Add }, None), op: Add }).with(..("title": None) => any)
|
||||
"title" = None
|
||||
---
|
||||
0..0 -> @bad-instantiate
|
||||
|
|
|
@ -2,10 +2,9 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"foo" = (("d": Any) => Any).with(..("d": 3) => any)
|
||||
"d" = ( ⪰ 3 | Type(int))
|
||||
"d" = ( ⪰ Type(int) | 3)
|
||||
"x" = 3
|
||||
---
|
||||
5..8 -> @foo
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named3.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"fun" = () => TypeUnary { lhs: 2, op: Return }
|
||||
"foo" = (("b": Any) => Any).with(..("b": TypeUnary { lhs: 2, op: Return }) => any)
|
||||
"b" = ( ⪰ TypeUnary { lhs: 2, op: Return } | TypeUnary { lhs: 2, op: Return })
|
||||
"b" = TypeUnary { lhs: 2, op: Return }
|
||||
"x" = TypeUnary { lhs: 2, op: Return }
|
||||
---
|
||||
5..8 -> @fun
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named4.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"dict" = {"x": 3}
|
||||
"foo" = (("c": Any) => Any).with(..("c": (3 | {"x": 3}.x)) => any)
|
||||
"c" = ( ⪰ (3 | {"x": 3}.x) | (3 | {"x": 3}.x))
|
||||
"c" = (3 | {"x": 3}.x)
|
||||
"x" = (3 | {"x": 3}.x)
|
||||
---
|
||||
5..9 -> @dict
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/op_contains.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"f" = (( ⪯ ("line" | "number"))) => Type(none)
|
||||
"f" = (("line" | "number")) => Type(none)
|
||||
"x" = Any
|
||||
---
|
||||
5..6 -> @f
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/op_contains_str.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"f" = (( ⪯ TypeUnary { lhs: "abc", op: ElementOf })) => Type(none)
|
||||
"f" = (TypeUnary { lhs: "abc", op: ElementOf }) => Type(none)
|
||||
"x" = Any
|
||||
---
|
||||
5..6 -> @f
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/op_type_of.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"f" = (( ⪯ Any)) => Type(none)
|
||||
"f" = (Any) => Type(none)
|
||||
"x" = Type(integer)
|
||||
---
|
||||
5..6 -> @f
|
||||
|
|
|
@ -2,13 +2,12 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/sig_template_set.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"tmpl" = ((Any, "authors": ( ⪯ (Type(array) | Type(str))), "class": Any, "font": ( ⪯ (TextFont | Array<TextFont>))) => Any).with(..("authors": (), "class": "article", "font": None) => any)
|
||||
"tmpl" = ((Any, "authors": (Type(array) | Type(str)), "class": Any, "font": (TextFont | Array<TextFont>)) => Any).with(..("authors": (), "class": "article", "font": None) => any)
|
||||
"content" = Any
|
||||
"authors" = ()
|
||||
"font" = None
|
||||
"class" = ( ⪰ "article" | "article" | "letter" | "article" | "letter")
|
||||
"class" = ( ⪰ "article" | "letter")
|
||||
---
|
||||
5..9 -> @tmpl
|
||||
10..17 -> @content
|
||||
|
|
|
@ -2,11 +2,10 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/tuple_map.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"a" = (1, )
|
||||
"f" = (( ⪯ (Type(bytes) | Type(decimal) | Type(float) | Type(int) | Type(label) | Type(str) | Type(type) | Type(version)))) => Type(str)
|
||||
"" = (( ⪯ (Type(bytes) | Type(decimal) | Type(float) | Type(int) | Type(label) | Type(str) | Type(type) | Type(version)))) => Type(str)
|
||||
"f" = ((Type(bytes) | Type(decimal) | Type(float) | Type(int) | Type(label) | Type(str) | Type(type) | Type(version))) => Type(str)
|
||||
"" = ((Type(bytes) | Type(decimal) | Type(float) | Type(int) | Type(label) | Type(str) | Type(type) | Type(version))) => Type(str)
|
||||
"x" = Any
|
||||
"b" = (Type(str), )
|
||||
---
|
||||
|
|
|
@ -2,10 +2,9 @@
|
|||
source: crates/tinymist-query/src/analysis.rs
|
||||
expression: result
|
||||
input_file: crates/tinymist-query/src/fixtures/type_check/with.typ
|
||||
snapshot_kind: text
|
||||
---
|
||||
"f" = (Any) => Any
|
||||
"x" = ( ⪰ Type(int) | Type(int))
|
||||
"x" = Type(int)
|
||||
"g" = ((Any) => Any).with(..(1) => any)
|
||||
"x" = 1
|
||||
---
|
||||
|
|
|
@ -250,33 +250,43 @@ impl<'a, 'b> TypeSimplifier<'a, 'b> {
|
|||
seq.collect::<Vec<_>>().into()
|
||||
}
|
||||
|
||||
// todo: reduce duplication
|
||||
#[allow(clippy::mutable_key_type)]
|
||||
fn transform_let(&mut self, w: &TypeBounds, def_id: Option<&DeclExpr>, pol: bool) -> Ty {
|
||||
let mut lbs = EcoVec::with_capacity(w.lbs.len());
|
||||
let mut ubs = EcoVec::with_capacity(w.ubs.len());
|
||||
let mut lbs = HashSet::with_capacity(w.lbs.len());
|
||||
let mut ubs = HashSet::with_capacity(w.ubs.len());
|
||||
|
||||
log::debug!("transform let [principal={}] with {w:?}", self.principal);
|
||||
|
||||
if !self.principal || ((pol) && !def_id.is_some_and(|i| self.negatives.contains(i))) {
|
||||
for lb in w.lbs.iter() {
|
||||
lbs.push(self.transform(lb, pol));
|
||||
lbs.insert(self.transform(lb, pol));
|
||||
}
|
||||
}
|
||||
if !self.principal || ((!pol) && !def_id.is_some_and(|i| self.positives.contains(i))) {
|
||||
for ub in w.ubs.iter() {
|
||||
ubs.push(self.transform(ub, !pol));
|
||||
ubs.insert(self.transform(ub, !pol));
|
||||
}
|
||||
}
|
||||
|
||||
if ubs.is_empty() {
|
||||
if lbs.len() == 1 {
|
||||
return lbs.pop().unwrap();
|
||||
return lbs.into_iter().next().unwrap();
|
||||
}
|
||||
if lbs.is_empty() {
|
||||
return Ty::Any;
|
||||
}
|
||||
} else if lbs.is_empty() && ubs.len() == 1 {
|
||||
return ubs.into_iter().next().unwrap();
|
||||
}
|
||||
|
||||
// todo: bad performance
|
||||
let mut lbs: Vec<_> = lbs.into_iter().collect();
|
||||
lbs.sort();
|
||||
let mut ubs: Vec<_> = ubs.into_iter().collect();
|
||||
ubs.sort();
|
||||
|
||||
let mut lbs = lbs.into_iter().collect();
|
||||
let mut ubs = ubs.into_iter().collect();
|
||||
Ty::Let(TypeBounds { lbs, ubs }.into())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue