Skip to content

Commit cf676d3

Browse files
authored
fix: assert throwing expected 2 arguments but got 1 (#2435)
1 parent e8ed2dd commit cf676d3

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

Diff for: src/resolver.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,12 @@ export class Resolver extends DiagnosticEmitter {
732732
// infer types with generic components while updating contextual types
733733
for (let i = 0; i < numParameters; ++i) {
734734
let argumentExpression = i < numArguments ? argumentNodes[i] : parameterNodes[i].initializer;
735-
if (!argumentExpression) { // missing initializer -> too few arguments
735+
if (!argumentExpression) {
736+
// optional but not have initializer should be handled in the other place
737+
if (parameterNodes[i].parameterKind == ParameterKind.OPTIONAL) {
738+
continue;
739+
}
740+
// missing initializer -> too few arguments
736741
if (reportMode == ReportMode.REPORT) {
737742
this.error(
738743
DiagnosticCode.Expected_0_arguments_but_got_1,

Diff for: tests/compiler/infer-generic.debug.wat

+37-2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
(export "test2" (func $export:infer-generic/test2))
5454
(export "test3" (func $export:infer-generic/test3))
5555
(export "test4" (func $export:infer-generic/test4))
56+
(export "inferAssert" (func $export:infer-generic/inferAssert))
5657
(start $~start)
5758
(func $infer-generic/inferCompatible<f64> (param $0 f64) (param $1 f64) (result i32)
5859
local.get $0
@@ -2233,6 +2234,24 @@
22332234
local.get $0
22342235
call $infer-generic/inferEncapsulatedFunctionMixed<f32,f64>
22352236
)
2237+
(func $infer-generic/inferAssert (param $0 i32)
2238+
(local $1 i32)
2239+
local.get $0
2240+
local.tee $1
2241+
i32.eqz
2242+
if (result i32)
2243+
i32.const 0
2244+
i32.const 32
2245+
i32.const 67
2246+
i32.const 3
2247+
call $~lib/builtins/abort
2248+
unreachable
2249+
else
2250+
local.get $1
2251+
end
2252+
i32.load
2253+
drop
2254+
)
22362255
(func $~lib/rt/__visit_globals (param $0 i32)
22372256
(local $1 i32)
22382257
global.get $infer-generic/arr
@@ -2421,7 +2440,7 @@
24212440
if
24222441
i32.const 0
24232442
i32.const 32
2424-
i32.const 60
2443+
i32.const 62
24252444
i32.const 1
24262445
call $~lib/builtins/abort
24272446
unreachable
@@ -2464,7 +2483,7 @@
24642483
if
24652484
i32.const 0
24662485
i32.const 32
2467-
i32.const 61
2486+
i32.const 63
24682487
i32.const 1
24692488
call $~lib/builtins/abort
24702489
unreachable
@@ -2562,4 +2581,20 @@
25622581
global.set $~lib/memory/__stack_pointer
25632582
local.get $1
25642583
)
2584+
(func $export:infer-generic/inferAssert (param $0 i32)
2585+
global.get $~lib/memory/__stack_pointer
2586+
i32.const 4
2587+
i32.sub
2588+
global.set $~lib/memory/__stack_pointer
2589+
call $~stack_check
2590+
global.get $~lib/memory/__stack_pointer
2591+
local.get $0
2592+
i32.store
2593+
local.get $0
2594+
call $infer-generic/inferAssert
2595+
global.get $~lib/memory/__stack_pointer
2596+
i32.const 4
2597+
i32.add
2598+
global.set $~lib/memory/__stack_pointer
2599+
)
25652600
)

Diff for: tests/compiler/infer-generic.release.wat

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
(module
22
(type $none_=>_none (func))
3+
(type $i32_=>_none (func (param i32)))
34
(type $i32_f32_i32_i32_=>_i32 (func (param i32 f32 i32 i32) (result i32)))
45
(type $i32_i32_=>_none (func (param i32 i32)))
56
(type $none_=>_i32 (func (result i32)))
67
(type $i32_=>_i32 (func (param i32) (result i32)))
7-
(type $i32_=>_none (func (param i32)))
88
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
99
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
1010
(type $f32_=>_f32 (func (param f32) (result f32)))
@@ -49,6 +49,7 @@
4949
(export "test2" (func $export:infer-generic/test2))
5050
(export "test3" (func $export:infer-generic/test2))
5151
(export "test4" (func $export:infer-generic/test2))
52+
(export "inferAssert" (func $export:infer-generic/inferAssert))
5253
(start $~start)
5354
(func $start:infer-generic~anonymous|0 (param $0 i32) (param $1 f32) (param $2 i32) (param $3 i32) (result i32)
5455
local.get $1
@@ -1458,6 +1459,43 @@
14581459
global.set $~lib/memory/__stack_pointer
14591460
local.get $0
14601461
)
1462+
(func $export:infer-generic/inferAssert (param $0 i32)
1463+
global.get $~lib/memory/__stack_pointer
1464+
i32.const 4
1465+
i32.sub
1466+
global.set $~lib/memory/__stack_pointer
1467+
global.get $~lib/memory/__stack_pointer
1468+
i32.const 1684
1469+
i32.lt_s
1470+
if
1471+
i32.const 18096
1472+
i32.const 18144
1473+
i32.const 1
1474+
i32.const 1
1475+
call $~lib/builtins/abort
1476+
unreachable
1477+
end
1478+
global.get $~lib/memory/__stack_pointer
1479+
local.get $0
1480+
i32.store
1481+
local.get $0
1482+
i32.eqz
1483+
if
1484+
i32.const 0
1485+
i32.const 1056
1486+
i32.const 67
1487+
i32.const 3
1488+
call $~lib/builtins/abort
1489+
unreachable
1490+
end
1491+
local.get $0
1492+
i32.load
1493+
drop
1494+
global.get $~lib/memory/__stack_pointer
1495+
i32.const 4
1496+
i32.add
1497+
global.set $~lib/memory/__stack_pointer
1498+
)
14611499
(func $byn-split-outlined-A$~lib/rt/itcms/__visit (param $0 i32)
14621500
(local $1 i32)
14631501
(local $2 i32)

Diff for: tests/compiler/infer-generic.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function test2(arr: f32[]): f32[] {
2020

2121
// (a: T) => R should infer T,R
2222

23-
function inferEncapsulatedFunction<T,R>(fn: (a: T) => R): (a: T) => R {
23+
function inferEncapsulatedFunction<T, R>(fn: (a: T) => R): (a: T) => R {
2424
return fn;
2525
}
2626

@@ -30,7 +30,7 @@ export function test3(fn: (a: f32) => f64): (a: f32) => f64 {
3030

3131
// (a: T, b: i32) => R should not bail out on non-inferred i32
3232

33-
function inferEncapsulatedFunctionMixed<T,R>(fn: (a: T, b: i32) => R): (a: T, b: i32) => R {
33+
function inferEncapsulatedFunctionMixed<T, R>(fn: (a: T, b: i32) => R): (a: T, b: i32) => R {
3434
return fn;
3535
}
3636

@@ -53,9 +53,16 @@ arr.reduce(((acc, cur) => acc && cur != 0), false);
5353

5454
// should fall back to default type
5555

56-
class Ref { x: i32; }
56+
class Ref {
57+
x: i32;
58+
}
5759
function inferDefault<T = Ref>(a: T): T {
5860
return a;
5961
}
6062
assert(inferDefault(1) == 1);
6163
assert(inferDefault({ x: 2 }) instanceof Ref);
64+
65+
// infer builtin assert generic
66+
export function inferAssert(v: Ref | null): void {
67+
assert(v).x;
68+
}

0 commit comments

Comments
 (0)