Skip to content

Commit

Permalink
Semantic: disallow named arguments in fn & method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 15, 2024
1 parent e567cfc commit cf5cecf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/semantic/expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ export const checkCall = (unaryExpr: UnaryExpr, ctx: Context): void => {
if (variantRef) {
unaryExpr.type = checkVariantCall(unaryExpr, variantRef, ctx)
} else {
call.args
.filter(arg => arg.name !== undefined)
.forEach(arg =>
addError(ctx, semanticError(ctx, arg.name!, `unexpected named argument \`${arg.name!.value}\``))
)
unaryExpr.type = checkCall_(call, operand, args, ctx)
}
}
Expand Down Expand Up @@ -525,14 +530,14 @@ export const checkCall_ = (call: CallOp, operand: Operand, args: Expr[], ctx: Co
}

const fnType = <VirtualFnType>operand.type

const genericMaps = makeFnGenericMaps(
operand.kind === 'identifier' ? operand.typeArgs.map(tp => typeToVirtual(tp, ctx)) : [],
fnType,
args.map(a => a.type!),
ctx
)
const paramTypes = fnType.paramTypes.map(pt => resolveType(pt, genericMaps, ctx))
// TODO: if it is a vid type call without args (e.g. Option::Some()), use checkNamedCall() reporting
checkCallArgs(call, args, paramTypes, ctx)

call.generics = fnType.generics.map(g => {
Expand Down
7 changes: 6 additions & 1 deletion src/semantic/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ const checkMethodCallExpr = (
if (binaryExpr.type) return binaryExpr.type

checkOperand(lOperand, ctx)
call.args.forEach(a => checkExpr(a.expr, ctx))
call.args.forEach(arg => {
checkExpr(arg.expr, ctx)
if (arg.name) {
addError(ctx, semanticError(ctx, arg.name, `unexpected named argument \`${arg.name!.value}\``))
}
})

const namedArgs = call.args.filter(a => a.name)
namedArgs.forEach(arg => {
Expand Down
10 changes: 10 additions & 0 deletions src/semantic/semantic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@ fn main() {
])
})

it('fn named call', () => {
const code = `\
fn main() {
let foo = println(value: 5)
return unit
}`
const ctx = check(code)
expect(ctx.errors.map(e => e.message)).toEqual(['unexpected named argument `value`'])
})

it('wrong arg count', () => {
const code = (arg: string) => `\
type Foo(x: Int)
Expand Down

0 comments on commit cf5cecf

Please sign in to comment.