Skip to content

Commit

Permalink
Unify types: id <> id edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Nov 25, 2024
1 parent 361c540 commit e04c68d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
15 changes: 12 additions & 3 deletions src/phase/type-unify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ const unify_ = (a: InferredType, b: InferredType, ctx: Context): InferredType =>
return b
}
switch (b.kind) {
// biome-ignore lint:
case 'identifier': {
if (a.def && a.def === b.def) {
if (a.typeArgs.length === b.typeArgs.length) {
Expand All @@ -273,6 +272,15 @@ const unify_ = (a: InferredType, b: InferredType, ctx: Context): InferredType =>
return u
}
}
if (a.def?.kind === 'type-def' && b.def?.kind === 'type-def') {
const e = makeErrorType(
`failed unify [${[a, b].map(t => inferredTypeToString(t)).join(', ')}]`,
'no-unify'
)
assign(a, e)
assign(b, e)
return e
}
if (a.def?.kind === 'type-param') {
if (a.def.unified) {
const u = unify(a.def.unified, b, ctx)
Expand All @@ -285,17 +293,18 @@ const unify_ = (a: InferredType, b: InferredType, ctx: Context): InferredType =>
return b
}
}
break
}
case 'inferred-fn':
case 'fn-type':
case 'name':
const e = makeErrorType(
`failed unify [${[a, b].map(t => `${inferredTypeToString(t)} (${t.kind})`).join(', ')}]`,
`failed unify [${[a, b].map(t => inferredTypeToString(t)).join(', ')}]`,
'no-unify'
)
assign(a, e)
assign(b, e)
return a
return e
}
break
}
Expand Down
7 changes: 0 additions & 7 deletions src/semantic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,4 @@ export const semanticCheck = (ctx: Context): void => {
unifyTypeBounds
]
phases.forEach(f => eachModule(f, ctx))
// ;[collectTypeBounds, unifyTypeBounds].forEach(f =>
// ctx.packages.at(-1)!.modules.forEach(m => {
// ctx.moduleStack.push(m)
// f(m, ctx)
// ctx.moduleStack.pop()
// })
// )
}
4 changes: 2 additions & 2 deletions src/typecheck/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const inferredTypeToString = (t: InferredType, depth = 0): string => {
case 'inferred':
return `[${t.bounds.map(b => inferredTypeToString(b, depth + 1)).join(', ')}]`
case 'inferred-fn':
const tps = t.typeParams.length > 0 ? `<${t.typeParams.map(typeParamToString)}>` : ''
const tps = t.typeParams.length > 0 ? `<${t.typeParams.map(typeParamToString).join(', ')}>` : ''
const pts = t.params
.map(pt => `${pt.name ? `${pt.name}: ` : ''}${inferredTypeToString(pt.type)}`)
.join(', ')
Expand All @@ -162,7 +162,7 @@ export const typeToString = (t: Type): string => {
case 'identifier':
return idToString(t)
case 'fn-type':
const tps = t.typeParams.length > 0 ? `<${t.typeParams.map(typeParamToString)}>` : ''
const tps = t.typeParams.length > 0 ? `<${t.typeParams.map(typeParamToString).join(', ')}>` : ''
const pts = t.params
.map(pt => `${pt.name ? `${pt.name.value}: ` : ''}${typeToString(pt.paramType)}`)
.join(', ')
Expand Down

0 comments on commit e04c68d

Please sign in to comment.