Skip to content

Commit

Permalink
Semantic: resolve arguments' generics before type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 31, 2024
1 parent 8ea6f45 commit 9bb6013
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/semantic/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export const checkMethodCall = (expr: UnaryExpr, mCall: MethodCallOp, ctx: Conte

let genericMaps = makeMethodGenericMaps(operand, ref.def, mCall, ctx)
const args = ref.def.fn.static ? mCall.call.args.map(a => a.expr) : [operand, ...mCall.call.args.map(a => a.expr)]
args.forEach(a => {
a.type = resolveType(a.type!, genericMaps, ctx)
})
const paramTypes = fnType.paramTypes.map(pt => resolveType(pt, genericMaps, ctx))
checkCallArgs(mCall, args, paramTypes, ctx)
// recalculate generic maps since malleable args might've been updated
Expand Down Expand Up @@ -203,7 +206,8 @@ export const makeMethodGenericMaps = (
implForGenericMap.delete(selfType.name)
maps.push(implForGenericMap)

const fnGenericMap = makeFnGenericMap(fnType, [lOperand.type!, ...call.call.args.map(a => a.expr.type!)])
const args = [lOperand.type!, ...call.call.args.map(a => a.expr.type!)]
const fnGenericMap = makeFnGenericMap(fnType, args)
maps.push(fnGenericMap)

return maps
Expand Down
6 changes: 3 additions & 3 deletions src/typecheck/generic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, InstanceScope } from '../scope'
import { getInstanceForType } from '../scope/trait'
import { merge } from '../util/map'
import { assert, unreachable } from '../util/todo'
import { assert } from '../util/todo'
import { VirtualFnType, VirtualType } from './index'
import { holeType, selfType } from './type'

Expand Down Expand Up @@ -54,7 +54,7 @@ export const makeGenericMapOverStructure = (arg: VirtualType, param: VirtualType
const paramType = param.paramTypes[i]
const argType = arg.paramTypes.at(i)
if (!argType) break
makeGenericMapOverStructure(argType, paramType).forEach((v, k) => map.set(k, v))
makeGenericMapOverStructure(paramType, argType).forEach((v, k) => map.set(k, v))
}
makeGenericMapOverStructure(arg.returnType, param.returnType).forEach((v, k) => map.set(k, v))
} else {
Expand Down Expand Up @@ -222,7 +222,7 @@ const resolveGenericMap = (
}
case 'hole-type':
case 'unknown-type':
case 'malleable-type':
return undefined
}
return unreachable()
}

0 comments on commit 9bb6013

Please sign in to comment.