Confusing error message

Can anyone explain why the following error messages differ:

func f(a: Int) { }
func g(a: Int, b: Int) { }

let a: UInt32 = 1

f(a: a)       // Cannot convert value of type 'UInt32' to expected argument type 'Int'
g(a: a, b: a) // Type of expression is ambiguous without more context

In addition, shouldn't the second complain that there's no matching function found, if it can't figure out that g(_:_) is a close match?

1 Like

I guess it's about the number of arguments that won't match. The Swift 5.3 compiler gives up after the first error.

func f(a: Int) { }
func g(a: Int, b: Int) { }

let i: UInt32 = 1

f(a: i)         // cannot convert value of type 'UInt32' to expected argument type 'Int'
g(a: i, b: 123) // cannot convert value of type 'UInt32' to expected argument type 'Int'
g(a: 123, b: i) // cannot convert value of type 'UInt32' to expected argument type 'Int'
g(a: i, b: i)   // type of expression is ambiguous without more context
Playground console
error: MyPlayground.playground:6:6: error: cannot convert value of type 'UInt32' to expected argument type 'Int'
f(a: i)
     ^
     Int( )

error: MyPlayground.playground:7:6: error: cannot convert value of type 'UInt32' to expected argument type 'Int'
g(a: i, b: 123)
     ^
     Int( )

error: MyPlayground.playground:8:14: error: cannot convert value of type 'UInt32' to expected argument type 'Int'
g(a: 123, b: i)
             ^
             Int( )

error: MyPlayground.playground:9:1: error: type of expression is ambiguous without more context
g(a: i, b: i)
^~~~~~~~~~~~~
1 Like

I think that is the same issue as https://bugs.swift.org/browse/SR-13240 which is fixed on master.
You can verify that using the latest master snapshot on https://swift.godbolt.org or downloading the toolchain from https://swift.org/download/#snapshots.

3 Likes

Swift nightly version on compiler explorer seems to be out of date. When you click on the "i" icon, it says c98d04b3f1aa51d which if from 18 August 2020, several snapshots ago

1 Like

Yeah, but still will be possible to verify this since the fix landed on master in July as far as I remember :)

1 Like
Terms of Service

Privacy Policy

Cookie Policy