Skip to content

Commit 2891d1d

Browse files
authored
feat(acton): support void-type parameters from Tolk 1.4 (#285)
1 parent 9281fa9 commit 2891d1d

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

server/src/e2e/tolk/testcases/inspections/CallArgumentsCountMismatchInspection.test

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,43 @@ fun main(c: Color) {
493493
------------------------------------------------------------------------
494494
3 2:4 to 2:20 Enum member 'Blue' is never used (tolk)
495495
0 10:4 to 10:14 Too many arguments in call to 'isRed', expected 0, have 1 (tolk)
496+
497+
========================================================================
498+
Trailing generic void parameters can be omitted
499+
========================================================================
500+
fun println<T1, T2 = void, T3 = void>(
501+
value1: T1,
502+
value2: T2,
503+
value3: T3,
504+
): void {
505+
value1;
506+
value2;
507+
value3;
508+
}
509+
510+
fun main() {
511+
println(1);
512+
println(1, 2);
513+
println(1, 2, 3);
514+
}
515+
------------------------------------------------------------------------
516+
no issues
517+
518+
========================================================================
519+
Generic void parameters can be omitted only at the end
520+
========================================================================
521+
fun log<T1, T2 = void>(
522+
value1: T1,
523+
value2: T2,
524+
value3: T1,
525+
): void {
526+
value1;
527+
value2;
528+
value3;
529+
}
530+
531+
fun main() {
532+
log(1);
533+
}
534+
------------------------------------------------------------------------
535+
0 11:7 to 11:8 Too few arguments in call to 'log', expected 3, have 1 (tolk)

server/src/languages/tolk/inspections/CallArgumentsCountMismatchInspection.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import {RecursiveVisitor} from "@server/visitor/visitor"
99
import {
1010
Enum,
1111
FunctionBase,
12+
Parameter,
1213
Struct,
1314
TypeAlias,
1415
TypeParameter,
1516
} from "@server/languages/tolk/psi/Decls"
1617
import {CallLike, NamedNode} from "@server/languages/tolk/psi/TolkNode"
1718
import {Reference} from "@server/languages/tolk/psi/Reference"
19+
import {NeverTy, TypeParameterTy, VoidTy} from "@server/languages/tolk/types/ty"
20+
import {typeOf} from "@server/languages/tolk/type-inference"
1821

1922
import {Inspection, InspectionIds} from "./Inspection"
2023

@@ -49,10 +52,11 @@ export class CallArgumentsCountMismatchInspection implements Inspection {
4952
const deltaSelf = this.isInstanceMethodCall(call) ? 1 : 0
5053
const args = call.arguments()
5154
const argsCount = args.length + deltaSelf
52-
const maxParams = called.parameters().length
55+
const parameters = called.parameters()
56+
const maxParams = parameters.length
5357

5458
let minParams = maxParams
55-
while (minParams > 0 && called.parameters()[minParams - 1].defaultValue() !== null) {
59+
while (minParams > 0 && this.canBeOmitted(parameters[minParams - 1])) {
5660
minParams--
5761
}
5862

@@ -106,4 +110,20 @@ export class CallArgumentsCountMismatchInspection implements Inspection {
106110
// instance method call like foo.bar()
107111
return true
108112
}
113+
114+
private canBeOmitted(parameter: Parameter): boolean {
115+
if (parameter.defaultValue() !== null) {
116+
return true
117+
}
118+
119+
const type = typeOf(parameter.node, parameter.file)?.baseType()
120+
if (
121+
type instanceof TypeParameterTy &&
122+
(type.defaultType instanceof NeverTy || type.defaultType instanceof VoidTy)
123+
) {
124+
return true
125+
}
126+
127+
return type instanceof NeverTy || type instanceof VoidTy
128+
}
109129
}

0 commit comments

Comments
 (0)