dev: implement Ord for Decl (#773)

* dev: change order to compare

* dev: implement `Ord` for `Decl`
This commit is contained in:
Myriad-Dreamin 2024-11-08 15:46:28 +08:00 committed by GitHub
parent a402d6d4ae
commit d02fa18617
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 64 additions and 57 deletions

View file

@ -111,7 +111,7 @@ mod expr_tests {
let result = ctx.shared_().expr_stage(&source);
let mut docstrings = result.docstrings.iter().collect::<Vec<_>>();
docstrings.sort_by(|x, y| x.0.weak_cmp(y.0));
docstrings.sort_by(|x, y| x.0.cmp(y.0));
let mut docstrings = docstrings
.into_iter()
.map(|(ident, expr)| {
@ -136,7 +136,7 @@ mod expr_tests {
let result = ctx.shared_().expr_stage(&source);
let mut resolves = result.resolves.iter().collect::<Vec<_>>();
resolves.sort_by(|x, y| x.1.decl.weak_cmp(&y.1.decl));
resolves.sort_by(|x, y| x.1.decl.cmp(&y.1.decl));
let mut resolves = resolves
.into_iter()

View file

@ -5,11 +5,11 @@ input_file: crates/tinymist-query/src/fixtures/expr_of/destructing.typ
---
= resolves
Var(a)@6..7 in /s0.typ -> Var(a)@6..7 in /s0.typ, root Var(a)@6..7 in /s0.typ, val: None
Var(a)@28..29 in /s0.typ -> Var(a)@28..29 in /s0.typ, root Var(a)@28..29 in /s0.typ, val: None
IdentRef(a)@40..41 in /s0.typ -> Var(a)@6..7 in /s0.typ, root Var(a)@6..7 in /s0.typ, val: None
Var(b)@9..10 in /s0.typ -> Var(b)@9..10 in /s0.typ, root Var(b)@9..10 in /s0.typ, val: None
Var(a)@28..29 in /s0.typ -> Var(a)@28..29 in /s0.typ, root Var(a)@28..29 in /s0.typ, val: None
Var(b)@31..32 in /s0.typ -> Var(b)@31..32 in /s0.typ, root Var(b)@31..32 in /s0.typ, val: None
IdentRef(b)@37..38 in /s0.typ -> Var(b)@9..10 in /s0.typ, root Var(b)@9..10 in /s0.typ, val: None
IdentRef(a)@40..41 in /s0.typ -> Var(a)@6..7 in /s0.typ, root Var(a)@6..7 in /s0.typ, val: None
= exports
a -> Var(a)@28..29 in /s0.typ
b -> Var(b)@31..32 in /s0.typ

View file

@ -4,8 +4,8 @@ expression: "snap.join(\"\\n\")"
input_file: crates/tinymist-query/src/fixtures/expr_of/dict.typ
---
= resolves
Var(x)@17..18 in /s0.typ -> Var(x)@17..18 in /s0.typ, root Var(x)@17..18 in /s0.typ, val: None
Var(z)@5..6 in /s0.typ -> Var(z)@5..6 in /s0.typ, root Var(z)@5..6 in /s0.typ, val: None
Var(x)@17..18 in /s0.typ -> Var(x)@17..18 in /s0.typ, root Var(x)@17..18 in /s0.typ, val: None
IdentRef(z)@28..29 in /s0.typ -> Var(z)@5..6 in /s0.typ, root Var(z)@5..6 in /s0.typ, val: None
IdentRef(z)@40..41 in /s0.typ -> Var(z)@5..6 in /s0.typ, root Var(z)@5..6 in /s0.typ, val: None
= exports

View file

@ -4,10 +4,10 @@ expression: "snap.join(\"\\n\")"
input_file: crates/tinymist-query/src/fixtures/expr_of/func.typ
---
= resolves
Var(x)@5..6 in /s0.typ -> Var(x)@5..6 in /s0.typ, root Var(x)@5..6 in /s0.typ, val: None
Func(f)@17..18 in /s0.typ -> Func(f)@17..18 in /s0.typ, root Func(f)@17..18 in /s0.typ, val: None
Var(a)@19..20 in /s0.typ -> Var(a)@19..20 in /s0.typ, root Var(a)@19..20 in /s0.typ, val: None
IdentRef(a)@24..25 in /s0.typ -> Var(a)@19..20 in /s0.typ, root Var(a)@19..20 in /s0.typ, val: None
Func(f)@17..18 in /s0.typ -> Func(f)@17..18 in /s0.typ, root Func(f)@17..18 in /s0.typ, val: None
Var(x)@5..6 in /s0.typ -> Var(x)@5..6 in /s0.typ, root Var(x)@5..6 in /s0.typ, val: None
= exports
f -> Func(f)@17..18 in /s0.typ
x -> Var(x)@5..6 in /s0.typ

View file

@ -7,8 +7,8 @@ input_file: crates/tinymist-query/src/fixtures/expr_of/import_alias.typ
PathStem(base)@8..18 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@20..24 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ImportPath(base)@33..43 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@52..56 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ModuleAlias(foo)@47..50 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@52..56 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(foo)@59..62 in /s0.typ -> ModuleAlias(foo)@47..50 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
= exports
base -> ref(PathStem(base), step = Module(base), root = Module(base))

View file

@ -7,11 +7,11 @@ input_file: crates/tinymist-query/src/fixtures/expr_of/import_alias_both.typ
PathStem(base)@8..18 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@20..24 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ImportPath(base)@33..43 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@62..66 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ModuleAlias(foo)@47..50 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
ImportAlias(foo)@57..60 in /s0.typ -> Import(x)@52..53 in /s0.typ, root (ModuleAlias(foo)).IdentRef(x), val: None
IdentRef(foo)@69..72 in /s0.typ -> ImportAlias(foo)@57..60 in /s0.typ, root (ModuleAlias(foo)).IdentRef(x), val: None
Import(x)@52..53 in /s0.typ -> (ModuleAlias(foo)).IdentRef(x), root (ModuleAlias(foo)).IdentRef(x), val: None
ImportAlias(foo)@57..60 in /s0.typ -> Import(x)@52..53 in /s0.typ, root (ModuleAlias(foo)).IdentRef(x), val: None
IdentRef(base)@62..66 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(foo)@69..72 in /s0.typ -> ImportAlias(foo)@57..60 in /s0.typ, root (ModuleAlias(foo)).IdentRef(x), val: None
= exports
base -> ref(PathStem(base), step = Module(base), root = Module(base))
foo -> ref(ImportAlias(foo), step = Import(x), root = (ModuleAlias(foo)).IdentRef(x))

View file

@ -5,8 +5,8 @@ input_file: crates/tinymist-query/src/fixtures/expr_of/import_by_ident.typ
---
= resolves
ModuleImport(..)@1..23 in /s2.typ -> Module(derive)@0..0 in /derive.typ, root Module(derive)@0..0 in /derive.typ, val: None
ModuleImport(..)@25..39 in /s2.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
ImportPath(derive)@8..20 in /s2.typ -> Module(derive)@0..0 in /derive.typ, root Module(derive)@0..0 in /derive.typ, val: None
ModuleImport(..)@25..39 in /s2.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(f)@41..42 in /s2.typ -> Func(f)@0..0 in /base.typ, root Func(f)@0..0 in /base.typ, val: None
= exports
base -> ref(PathStem(base), step = Module(base), root = Module(base))

View file

@ -4,12 +4,12 @@ expression: "snap.join(\"\\n\")"
input_file: crates/tinymist-query/src/fixtures/expr_of/import_ident.typ
---
= resolves
ModuleImport(..)@26..46 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
PathStem(base)@8..18 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@20..24 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ModuleImport(..)@26..46 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
ImportPath(base)@33..43 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@48..52 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
Import(x)@45..46 in /s0.typ -> (ModuleImport(..)).IdentRef(x), root (ModuleImport(..)).IdentRef(x), val: None
IdentRef(base)@48..52 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(x)@55..56 in /s0.typ -> Import(x)@45..46 in /s0.typ, root (ModuleImport(..)).IdentRef(x), val: None
= exports
base -> ref(PathStem(base), step = Module(base), root = Module(base))

View file

@ -4,14 +4,14 @@ expression: "snap.join(\"\\n\")"
input_file: crates/tinymist-query/src/fixtures/expr_of/import_ident_alias.typ
---
= resolves
ModuleImport(..)@26..53 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
PathStem(base)@8..18 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@20..24 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ModuleImport(..)@26..53 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
ImportPath(base)@33..43 in /s0.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@55..59 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
ImportAlias(foo)@50..53 in /s0.typ -> Import(x)@45..46 in /s0.typ, root (ModuleImport(..)).IdentRef(x), val: None
IdentRef(foo)@62..65 in /s0.typ -> ImportAlias(foo)@50..53 in /s0.typ, root (ModuleImport(..)).IdentRef(x), val: None
Import(x)@45..46 in /s0.typ -> (ModuleImport(..)).IdentRef(x), root (ModuleImport(..)).IdentRef(x), val: None
ImportAlias(foo)@50..53 in /s0.typ -> Import(x)@45..46 in /s0.typ, root (ModuleImport(..)).IdentRef(x), val: None
IdentRef(base)@55..59 in /s0.typ -> PathStem(base)@8..18 in /s0.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(foo)@62..65 in /s0.typ -> ImportAlias(foo)@50..53 in /s0.typ, root (ModuleImport(..)).IdentRef(x), val: None
= exports
base -> ref(PathStem(base), step = Module(base), root = Module(base))
foo -> ref(ImportAlias(foo), step = Import(x), root = (ModuleImport(..)).IdentRef(x))

View file

@ -4,9 +4,9 @@ expression: "snap.join(\"\\n\")"
input_file: crates/tinymist-query/src/fixtures/expr_of/import_star.typ
---
= resolves
ModuleImport(..)@26..46 in /s1.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
PathStem(base)@8..18 in /s1.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@20..24 in /s1.typ -> PathStem(base)@8..18 in /s1.typ, root Module(base)@0..0 in /base.typ, val: None
ModuleImport(..)@26..46 in /s1.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
ImportPath(base)@33..43 in /s1.typ -> Module(base)@0..0 in /base.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(base)@48..52 in /s1.typ -> PathStem(base)@8..18 in /s1.typ, root Module(base)@0..0 in /base.typ, val: None
IdentRef(x)@55..56 in /s1.typ -> Var(x)@0..0 in /base.typ, root Var(x)@0..0 in /base.typ, val: None

View file

@ -3,11 +3,11 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/annotation_fn.typ
---
"args" = Args
"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
"touying-fn-wrapper" = ((( ⪯ Type(function)), "max-repetitions": ( ⪯ Type(int)), "repetitions": ( ⪯ Type(int)), ...: ( ⪯ Any)) => None).with(..("max-repetitions": None, "repetitions": None) => any)
"args" = Args
---
162..180 -> @touying-fn-wrapper
181..183 -> @fn

View file

@ -4,9 +4,9 @@ expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/annotation_fn2.typ
---
"args" = Any
"args" = Args
"fn" = Any
"fn-wrapper" = (( ⪯ (Type(function) | (...: Any) => Any)), ...: ( ⪯ Any)) => None
"fn" = Any
"args" = Args
---
107..117 -> @fn-wrapper
118..120 -> @fn

View file

@ -3,8 +3,8 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/annotation_sum.typ
---
"args" = Args
"sum" = (...: ( ⪯ Array<Type(int)>)) => None
"args" = Args
---
65..68 -> @sum
71..75 -> @args

View file

@ -5,10 +5,10 @@ input_file: crates/tinymist-query/src/fixtures/type_check/annotation_var.typ
---
"x" = Any
"y" = Any
"" = (Any, Any) => Any
"f" = Any
"mapper" = (Type(function) | (Any, Any) => Any)
"" = (Any, Any) => Any
"x" = Any
"f" = Any
---
56..62 -> @mapper
65..83 -> @

View file

@ -4,8 +4,8 @@ expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/bug_cite_func_infer.typ
---
"cite_prose" = (( ⪯ (RefLabel))) => Element(ref)
"cite_prose_different_name" = (( ⪯ (RefLabel))) => Element(ref)
"labl" = Any
"cite_prose_different_name" = (( ⪯ (RefLabel))) => Element(ref)
"labl" = Any
---
5..15 -> @cite_prose

View file

@ -3,12 +3,12 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/confusing-name.typ
---
"date" = Any
"info" = Any
"master-cover" = ((Any, "x": Any) => TypeBinary { operands: (Any, TypeBinary { operands: ({"submit-date": 0}, Any), op: Add }), op: Assign }).with(..("x": Any) => any)
"x" = (Any) => Any
"date" = Any
"x" = Any
"x" = Any
"master-cover" = ((Any, "x": Any) => TypeBinary { operands: (Any, TypeBinary { operands: ({"submit-date": 0}, Any), op: Add }), op: Assign }).with(..("x": Any) => any)
"info" = Any
"x" = Any
---
5..6 -> @x

View file

@ -3,8 +3,8 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named.typ
---
"d" = ( ⪰ 3 | Type(int))
"foo" = (("d": Any) => Any).with(..("d": 3) => any)
"d" = ( ⪰ 3 | Type(int))
"x" = 3
---
5..8 -> @foo

View file

@ -3,9 +3,9 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named2.typ
---
"a" = Any
"foo" = (("a": Any) => Any).with(..("a": 1) => any)
"val" = 1
"foo" = (("a": Any) => Any).with(..("a": 1) => any)
"a" = Any
"x" = Any
---
5..8 -> @val

View file

@ -3,9 +3,9 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named3.typ
---
"b" = ( ⪰ TypeUnary { lhs: 2, op: Return } | TypeUnary { lhs: 2, op: Return })
"foo" = (("b": Any) => Any).with(..("b": TypeUnary { lhs: 2, op: Return }) => any)
"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 })
"x" = TypeUnary { lhs: 2, op: Return }
---
5..8 -> @fun

View file

@ -3,9 +3,9 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/fn_named4.typ
---
"c" = ( ⪰ (3 | {"x": 3}.x) | (3 | {"x": 3}.x))
"dict" = {"x": 3}
"foo" = (("c": Any) => Any).with(..("c": (3 | {"x": 3}.x)) => any)
"c" = ( ⪰ (3 | {"x": 3}.x) | (3 | {"x": 3}.x))
"x" = (3 | {"x": 3}.x)
---
5..9 -> @dict

View file

@ -3,8 +3,8 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/sig_template.typ
---
"content" = Any
"tmpl" = (Any) => Any
"content" = Any
---
5..9 -> @tmpl
10..17 -> @content

View file

@ -3,11 +3,11 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/sig_template_set.typ
---
"authors" = ()
"class" = ( ⪰ "article" | "article" | "letter" | "article" | "letter")
"content" = Any
"font" = None
"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")
---
5..9 -> @tmpl
10..17 -> @content

View file

@ -3,11 +3,11 @@ source: crates/tinymist-query/src/analysis.rs
expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/tuple_map.typ
---
"" = (( ⪯ (Type(bytes) | Type(decimal) | Type(float) | Type(int) | Type(label) | Type(str) | Type(type) | Type(version)))) => Type(str)
"a" = (1, )
"b" = (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), )
---
5..6 -> @a
20..21 -> @f

View file

@ -4,8 +4,8 @@ expression: result
input_file: crates/tinymist-query/src/fixtures/type_check/with.typ
---
"f" = (Any) => Any
"g" = ((Any) => Any).with(..(1) => any)
"x" = ( ⪰ Type(int) | Type(int))
"g" = ((Any) => Any).with(..(1) => any)
"x" = 1
---
5..6 -> @f

View file

@ -442,18 +442,6 @@ impl Decl {
src.range(self.span())
}
pub fn weak_cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name()
.cmp(other.name())
.then_with(|| match (self, other) {
(Self::Generated(l), Self::Generated(r)) => l.0 .0.cmp(&r.0 .0),
(Self::Docs(l), Self::Docs(r)) => {
l.var.cmp(&r.var).then_with(|| l.base.weak_cmp(&r.base))
}
_ => self.span().number().cmp(&other.span().number()),
})
}
pub fn as_def(this: &Interned<Self>, val: Option<Ty>) -> Interned<RefExpr> {
let def: Expr = this.clone().into();
Interned::new(RefExpr {
@ -465,6 +453,25 @@ impl Decl {
}
}
impl Ord for Decl {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let base = match (self, other) {
(Self::Generated(l), Self::Generated(r)) => l.0 .0.cmp(&r.0 .0),
(Self::Module(l), Self::Module(r)) => l.fid.cmp(&r.fid),
(Self::Docs(l), Self::Docs(r)) => l.var.cmp(&r.var).then_with(|| l.base.cmp(&r.base)),
_ => self.span().number().cmp(&other.span().number()),
};
base.then_with(|| self.name().cmp(other.name()))
}
}
impl PartialOrd for Decl {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<Decl> for Expr {
fn from(decl: Decl) -> Self {
Expr::Decl(decl.into())

View file

@ -450,7 +450,7 @@ pub struct TypeVar {
impl Ord for TypeVar {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// todo: buggy
self.def.weak_cmp(&other.def)
self.def.cmp(&other.def)
}
}