From 7df47e5d2bf578f21ad9d7b17b8d7b11194846c3 Mon Sep 17 00:00:00 2001 From: palumbon Date: Sun, 7 Jan 2024 20:31:19 +0100 Subject: [PATCH 1/3] Fix ignore wollok code in Type System --- src/model.ts | 5 +++++ src/typeSystem/typeVariables.ts | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/model.ts b/src/model.ts index 3226ecea..0699c5be 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,3 +1,4 @@ +import { WOLLOK_BASE_PACKAGE } from './constants' import { cached, getPotentiallyUninitializedLazy, lazy } from './decorators' import { ConstructorFor, InstanceOf, is, last, List, mapObject, Mixable, MixinDefinition, MIXINS, notEmpty, TypeDefinition } from './extensions' import { TypeRegistry, WollokType } from './typeSystem/wollokTypes' @@ -281,6 +282,10 @@ export function Entity>(supertype: S) { ? `${parent.fullyQualifiedName}.${label}` : label } + + get isBaseWollokCode(): boolean { + return this.fullyQualifiedName.includes(WOLLOK_BASE_PACKAGE) + } } return EntityType diff --git a/src/typeSystem/typeVariables.ts b/src/typeSystem/typeVariables.ts index cb1dfc14..e7fdd172 100644 --- a/src/typeSystem/typeVariables.ts +++ b/src/typeSystem/typeVariables.ts @@ -1,3 +1,4 @@ +import { WOLLOK_BASE_PACKAGE } from '../constants' import { is, last, List, match, when } from '../extensions' import { Assignment, Body, Class, Closure, Describe, Environment, Expression, Field, If, Import, Literal, Method, Module, NamedArgument, New, Node, Package, Parameter, Program, Reference, Return, Self, Send, Singleton, Super, Test, Throw, Try, Variable } from '../model' import { ANY, AtomicType, ELEMENT, RETURN, TypeSystemProblem, VOID, WollokAtomicType, WollokClosureType, WollokMethodType, WollokModuleType, WollokParameterType, WollokParametricType, WollokType, WollokUnionType } from './wollokTypes' @@ -92,7 +93,7 @@ const inferEnvironment = (env: Environment) => { } const inferPackage = (p: Package) => { - if (p.name.startsWith('wollok')) return //TODO: Fix wrong inferences + if (p.isBaseWollokCode) return // Wollok code should be typed by annotations p.children.forEach(createTypeVariables) } From 35e18cbe75d7f58cf61c805c8444c2f770a72b9a Mon Sep 17 00:00:00 2001 From: palumbon Date: Tue, 16 Jan 2024 22:59:05 +0100 Subject: [PATCH 2/3] includes -> startsWith --- src/model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.ts b/src/model.ts index 0699c5be..5ea437a5 100644 --- a/src/model.ts +++ b/src/model.ts @@ -284,7 +284,7 @@ export function Entity>(supertype: S) { } get isBaseWollokCode(): boolean { - return this.fullyQualifiedName.includes(WOLLOK_BASE_PACKAGE) + return this.fullyQualifiedName.startsWith(WOLLOK_BASE_PACKAGE) } } From 47d05eeba7318534279de4041133e8763316cb20 Mon Sep 17 00:00:00 2001 From: palumbon Date: Tue, 16 Jan 2024 22:59:29 +0100 Subject: [PATCH 3/3] Rename to inferTypeVariables --- package-lock.json | 4 +-- src/typeSystem/typeVariables.ts | 48 ++++++++++++++++----------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1d2bfe85..5ddcb9b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "wollok-ts", - "version": "4.0.7", + "version": "4.0.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "wollok-ts", - "version": "4.0.7", + "version": "4.0.8", "license": "MIT", "dependencies": { "@types/parsimmon": "^1.10.8", diff --git a/src/typeSystem/typeVariables.ts b/src/typeSystem/typeVariables.ts index e7fdd172..ecaaacf4 100644 --- a/src/typeSystem/typeVariables.ts +++ b/src/typeSystem/typeVariables.ts @@ -1,4 +1,3 @@ -import { WOLLOK_BASE_PACKAGE } from '../constants' import { is, last, List, match, when } from '../extensions' import { Assignment, Body, Class, Closure, Describe, Environment, Expression, Field, If, Import, Literal, Method, Module, NamedArgument, New, Node, Package, Parameter, Program, Reference, Return, Self, Send, Singleton, Super, Test, Throw, Try, Variable } from '../model' import { ANY, AtomicType, ELEMENT, RETURN, TypeSystemProblem, VOID, WollokAtomicType, WollokClosureType, WollokMethodType, WollokModuleType, WollokParameterType, WollokParametricType, WollokType, WollokUnionType } from './wollokTypes' @@ -9,7 +8,7 @@ const tVars = new Map() export function newTypeVariables(env: Environment): Map { tVars.clear() - createTypeVariables(env) + inferTypeVariables(env) return tVars } @@ -27,7 +26,7 @@ function newTVarFor(node: Node) { const newTVar = doNewTVarFor(node) let annotatedVar = newTVar // By default, annotations reference the same tVar if (node.is(Method)) { - const parameters = node.parameters.map(p => createTypeVariables(p)!) + const parameters = node.parameters.map(p => inferTypeVariables(p)!) annotatedVar = newSyntheticTVar(node) // But for methods, annotations reference to return tVar newTVar.setType(new WollokMethodType(annotatedVar, parameters, annotatedVariableMap(node)), false) } @@ -51,7 +50,7 @@ function doNewTVarFor(node: Node) { return newTVar } -function createTypeVariables(node: Node): TypeVariable | void { +function inferTypeVariables(node: Node): TypeVariable | void { return match(node)( when(Environment)(inferEnvironment), @@ -89,28 +88,29 @@ function createTypeVariables(node: Node): TypeVariable | void { } const inferEnvironment = (env: Environment) => { - env.children.forEach(createTypeVariables) + env.children.forEach(inferTypeVariables) } const inferPackage = (p: Package) => { - if (p.isBaseWollokCode) return // Wollok code should be typed by annotations - p.children.forEach(createTypeVariables) + // Wollok code should be typed by annotations, avoid inference. + if (p.isBaseWollokCode) return; + p.children.forEach(inferTypeVariables) } const inferProgram = (p: Program) => { - createTypeVariables(p.body) + inferTypeVariables(p.body) } const inferTest = (t: Test) => { - createTypeVariables(t.body) + inferTypeVariables(t.body) } const inferBody = (body: Body) => { - body.sentences.forEach(createTypeVariables) + body.sentences.forEach(inferTypeVariables) } const inferModule = (m: Module | Describe) => { - m.members.forEach(createTypeVariables) + m.members.forEach(inferTypeVariables) const tVar = typeVariableFor(m) if (!(m.is(Singleton) && m.isClosure())) // Avoid closures tVar.setType(typeForModule(m)) // Set module type @@ -120,12 +120,12 @@ const inferModule = (m: Module | Describe) => { const inferNew = (n: New) => { const clazz = n.instantiated.target! const tVar = typeVariableFor(n).setType(typeForModule(clazz)) - /*const args =*/ n.args.map(createTypeVariables) + /*const args =*/ n.args.map(inferTypeVariables) return tVar } const inferNamedArgument = (n: NamedArgument) => { - const valueTVar = createTypeVariables(n.value)! + const valueTVar = inferTypeVariables(n.value)! if (n.parent instanceof New) { // Named arguments value should be subtype of field definition const clazz = n.parent.instantiated.target! @@ -141,7 +141,7 @@ const inferNamedArgument = (n: NamedArgument) => { const inferMethod = (m: Method) => { const method = typeVariableFor(m) - m.sentences.forEach(createTypeVariables) + m.sentences.forEach(inferTypeVariables) if (m.sentences.length) { const lastSentence = last(m.sentences)! if (!lastSentence.is(Return) && !lastSentence.is(If)) { // Return inference already propagate type to method @@ -154,22 +154,22 @@ const inferMethod = (m: Method) => { } const inferSend = (send: Send) => { - const receiver = createTypeVariables(send.receiver)! - /*const args =*/ send.args.map(createTypeVariables) + const receiver = inferTypeVariables(send.receiver)! + /*const args =*/ send.args.map(inferTypeVariables) receiver.addSend(send) // TODO: Save args info for max type inference return typeVariableFor(send) } const inferAssignment = (a: Assignment) => { - const variable = createTypeVariables(a.variable)! - const value = createTypeVariables(a.value)! + const variable = inferTypeVariables(a.variable)! + const value = inferTypeVariables(a.value)! variable.beSupertypeOf(value) return typeVariableFor(a).setType(new WollokAtomicType(VOID)) } const inferVariable = (v: Variable | Field) => { - const valueTVar = createTypeVariables(v.value) + const valueTVar = inferTypeVariables(v.value) const varTVar = typeVariableFor(v) if (valueTVar && !varTVar.closed) varTVar.beSupertypeOf(valueTVar) return varTVar @@ -183,16 +183,16 @@ const inferReturn = (r: Return) => { const method = r.ancestors.find(is(Method)) if (!method) throw new Error('Method for Return not found') if (r.value) - typeVariableFor(method).atParam(RETURN).beSupertypeOf(createTypeVariables(r.value)!) + typeVariableFor(method).atParam(RETURN).beSupertypeOf(inferTypeVariables(r.value)!) else typeVariableFor(method).atParam(RETURN).setType(new WollokAtomicType(VOID)) return typeVariableFor(r).setType(new WollokAtomicType(VOID)) } const inferIf = (_if: If) => { - createTypeVariables(_if.condition)!.setType(new WollokModuleType(_if.environment.booleanClass)) - createTypeVariables(_if.thenBody) - createTypeVariables(_if.elseBody) + inferTypeVariables(_if.condition)!.setType(new WollokModuleType(_if.environment.booleanClass)) + inferTypeVariables(_if.thenBody) + inferTypeVariables(_if.elseBody) if (_if.elseBody.sentences.length) { typeVariableFor(_if) .beSupertypeOf(typeVariableFor(last(_if.elseBody.sentences)!)) @@ -234,7 +234,7 @@ const inferLiteral = (l: Literal) => { const arrayLiteralType = (value: readonly [Reference, List]) => { const arrayTVar = typeForModule(value[0].target!) const elementTVar = arrayTVar.atParam(ELEMENT) - value[1].map(createTypeVariables).forEach(inner => + value[1].map(inferTypeVariables).forEach(inner => elementTVar.beSupertypeOf(inner!) ) return arrayTVar