Skip to content

Commit cc1ffab

Browse files
author
Shon Feder
committed
Fix visitor and transformer for params
Followup for #1371 I forgot to add visitor logic for the parameter type annotations
1 parent f8f90cf commit cc1ffab

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

quint/src/ir/IRTransformer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ function transformExpression(transformer: IRTransformer, expr: ir.QuintEx): ir.Q
483483
newExpr = transformer.enterLambda(newExpr)
484484
}
485485

486+
newExpr.params = newExpr.params.map(p =>
487+
p.typeAnnotation ? { ...p, typeAnnotation: transformType(transformer, p.typeAnnotation) } : p
488+
)
486489
newExpr.expr = transformExpression(transformer, newExpr.expr)
487490

488491
if (transformer.exitLambda) {

quint/src/ir/IRVisitor.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,11 @@ export function walkExpression(visitor: IRVisitor, expr: ir.QuintEx): void {
456456
if (visitor.enterLambda) {
457457
visitor.enterLambda(expr)
458458
}
459-
459+
expr.params.forEach(p => {
460+
if (p.typeAnnotation) {
461+
walkType(visitor, p.typeAnnotation)
462+
}
463+
})
460464
walkExpression(visitor, expr.expr)
461465

462466
if (visitor.exitLambda) {

quint/test/ir/IRTransformer.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ describe('enterExpr', () => {
4141

4242
assert.deepEqual(moduleToString(result), moduleToString(expectedModule))
4343
})
44+
45+
it('transforms paramater type annotations', () => {
46+
class TestTransformer implements IRTransformer {
47+
exitType(_: QuintType): QuintType {
48+
return { kind: 'var', name: 'trans' }
49+
}
50+
}
51+
52+
const transformer = new TestTransformer()
53+
54+
const m = buildModuleWithDecls(['def foo(x: int, b: int, c: str): int = 42'])
55+
56+
const transformedDecl = transformModule(transformer, m).declarations[0]
57+
assert(transformedDecl.kind === 'def')
58+
assert(transformedDecl.expr.kind === 'lambda')
59+
transformedDecl.expr.params.forEach(p => {
60+
assert(p.typeAnnotation)
61+
assert.deepEqual(p.typeAnnotation, { kind: 'var', name: 'trans' })
62+
})
63+
})
4464
})
4565

4666
describe('enterDecl', () => {

quint/test/ir/IRVisitor.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -902,5 +902,25 @@ describe('walkModule', () => {
902902
assert.deepEqual(visitor.entered.map(typeToString), expectedTypes)
903903
assert.deepEqual(visitor.exited.map(typeToString), expectedTypes)
904904
})
905+
906+
it('finds paramater type annotations', () => {
907+
class TestVisitor implements IRVisitor {
908+
typesVisited: QuintType[] = []
909+
exitType(t: QuintType) {
910+
this.typesVisited.push(t)
911+
}
912+
}
913+
914+
const visitor = new TestVisitor()
915+
916+
const m = buildModuleWithDecls(['def foo(x: int, b: str): bool = true'])
917+
walkModule(visitor, m)
918+
const actualTypes = visitor.typesVisited.map(typeToString)
919+
// `int` and `str` should each show up TWICE:
920+
// - once from of the lambda type annotations
921+
// - once from of the parameter type annotation
922+
const expectedTypes = ['int', 'str', 'bool', '(int, str) => bool', 'int', 'str']
923+
assert.deepEqual(actualTypes, expectedTypes)
924+
})
905925
})
906926
})

0 commit comments

Comments
 (0)