Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix visitor and transformer for params #1384

Merged
merged 1 commit into from
Feb 29, 2024
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
3 changes: 3 additions & 0 deletions quint/src/ir/IRTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ function transformExpression(transformer: IRTransformer, expr: ir.QuintEx): ir.Q
newExpr = transformer.enterLambda(newExpr)
}

newExpr.params = newExpr.params.map(p =>
p.typeAnnotation ? { ...p, typeAnnotation: transformType(transformer, p.typeAnnotation) } : p
)
newExpr.expr = transformExpression(transformer, newExpr.expr)

if (transformer.exitLambda) {
Expand Down
6 changes: 5 additions & 1 deletion quint/src/ir/IRVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,11 @@ export function walkExpression(visitor: IRVisitor, expr: ir.QuintEx): void {
if (visitor.enterLambda) {
visitor.enterLambda(expr)
}

expr.params.forEach(p => {
if (p.typeAnnotation) {
walkType(visitor, p.typeAnnotation)
}
})
walkExpression(visitor, expr.expr)

if (visitor.exitLambda) {
Expand Down
20 changes: 20 additions & 0 deletions quint/test/ir/IRTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ describe('enterExpr', () => {

assert.deepEqual(moduleToString(result), moduleToString(expectedModule))
})

it('transforms paramater type annotations', () => {
class TestTransformer implements IRTransformer {
exitType(_: QuintType): QuintType {
return { kind: 'var', name: 'trans' }
}
}

const transformer = new TestTransformer()

const m = buildModuleWithDecls(['def foo(x: int, b: int, c: str): int = 42'])

const transformedDecl = transformModule(transformer, m).declarations[0]
assert(transformedDecl.kind === 'def')
assert(transformedDecl.expr.kind === 'lambda')
transformedDecl.expr.params.forEach(p => {
assert(p.typeAnnotation)
assert.deepEqual(p.typeAnnotation, { kind: 'var', name: 'trans' })
})
})
})

describe('enterDecl', () => {
Expand Down
20 changes: 20 additions & 0 deletions quint/test/ir/IRVisitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,5 +902,25 @@ describe('walkModule', () => {
assert.deepEqual(visitor.entered.map(typeToString), expectedTypes)
assert.deepEqual(visitor.exited.map(typeToString), expectedTypes)
})

it('finds paramater type annotations', () => {
class TestVisitor implements IRVisitor {
typesVisited: QuintType[] = []
exitType(t: QuintType) {
this.typesVisited.push(t)
}
}

const visitor = new TestVisitor()

const m = buildModuleWithDecls(['def foo(x: int, b: str): bool = true'])
walkModule(visitor, m)
const actualTypes = visitor.typesVisited.map(typeToString)
// `int` and `str` should each show up TWICE:
// - once from of the lambda type annotations
// - once from of the parameter type annotation
const expectedTypes = ['int', 'str', 'bool', '(int, str) => bool', 'int', 'str']
assert.deepEqual(actualTypes, expectedTypes)
})
})
})
Loading