Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions server/src/e2e/tolk/testcases/types/array.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
========================================================================
Array type — Basic
========================================================================
struct array<T> {}

fun main() {
val a: array<int> = [1, 2, 3];
//! ^ array<int>
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Inference from elements
========================================================================
struct array<T> {}

fun main() {
val a = [1, 2, 3];
//! ^ array<int>
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Explicit literal
========================================================================
struct array<T> {}

fun main() {
val a = array<int>[1, 2];
//! ^ array<int>
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Mixed elements
========================================================================
struct array<T> {}

fun main() {
val a = [1, null];
//! ^ array<int?>
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Empty array
========================================================================
struct array<T> {}

fun main() {
val a = [];
//! ^ array<unknown>
}
------------------------------------------------------------------------
ok

========================================================================
Tuple type — Tuple hint
========================================================================
fun main() {
val a: [int, bool] = [1, true];
//! ^ [int, bool]
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Numeric index access
========================================================================
struct array<T> {}

fun main() {
val a: array<int> = [1, 2, 3];
val x = a.0;
//! ^ int
val y = a.100;
//! ^ int
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Generic deduction
========================================================================
struct array<T> {}

fun first<T>(arr: array<T>): T {
return arr.0;
}

fun main() {
val a = [1, 2, 3];
val x = first(a);
//! ^ int
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Struct hint in Tuple
========================================================================
struct Foo {
foo: int
}

fun main() {
val a: [Foo] = [{foo: 10}];
//! ^ int
val item = a.0;
val field = item.foo;
//! ^ int
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Struct hint in Array
========================================================================
struct array<T> {}

struct Foo {
foo: int
}

fun main() {
val a: array<Foo> = [{foo: 10}];
//! ^ int
val field = a.0.foo;
//! ^ int
}
------------------------------------------------------------------------
ok

========================================================================
Array type — Struct hint in Explicit Array
========================================================================
struct array<T> {}

struct Foo {
foo: int
}

fun main() {
val a = array<Foo>[{foo: 10}];
//! ^ int
val field = a.0.foo;
//! ^ int
}
------------------------------------------------------------------------
ok

========================================================================
Array type — lisp_list hint
========================================================================
struct Foo { foo: int }

struct lisp_list<T> {}

fun main() {
val a: lisp_list<Foo> = [{foo: 10}];
//! ^ int
}
------------------------------------------------------------------------
ok
4 changes: 4 additions & 0 deletions server/src/languages/tolk/psi/Reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
UnionTy,
NullTy,
EnumTy,
ArrayTy,
} from "@server/languages/tolk/types/ty"
import {parentOfType} from "@server/psi/utils"
import {inferenceOf, typeOf} from "@server/languages/tolk/type-inference"
Expand Down Expand Up @@ -445,6 +446,9 @@ export class Reference {
) {
return receiverType.innerTy.name() === expected.innerTy.name()
}
if (receiverType instanceof ArrayTy && expected instanceof ArrayTy) {
return true
}
}

if (receiver?.type === "nullable_type") {
Expand Down
9 changes: 8 additions & 1 deletion server/src/languages/tolk/tree-sitter-tolk/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,14 @@ const TOLK_GRAMMAR = {
parenthesized_expression: $ => seq("(", field("inner", $._expression), optional(","), ")"),
tensor_expression: $ =>
choice(seq("(", ")"), seq("(", commaSep2($._expression), optional(","), ")")),
typed_tuple: $ => seq("[", commaSep($._expression), optional(","), "]"),
typed_tuple: $ =>
seq(
optional(field("type", $._type_hint)),
"[",
field("elements", commaSep($._expression)),
optional(","),
"]",
),

lambda_expression: $ =>
prec.right(
Expand Down
136 changes: 87 additions & 49 deletions server/src/languages/tolk/tree-sitter-tolk/src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading