Static member lookup with multiple generic contexts

In the nightly compiler, the following code which exercises swift-evolution/0299-extend-generic-static-member-lookup.md at main · apple/swift-evolution · GitHub doesn't work:

protocol Foo { }
struct Bar: Foo { }
extension Foo where Self == Bar {
  static func bar(_ foo: Foo) -> Self {  fatalError() }
  static var baz: Self { fatalError() }
}
func foo() {
  let _: Foo = .baz // Works
  let _: Foo = .bar(Bar.baz) // Works
  let _: Foo = .bar(.baz) // "Type of expression is ambiguous without more context"
}

Is this is a bug, a limitation of the current implementation, or something that explicitly won't be supported? @xedin

This is expected because argument to bar is existential type of Foo which can't reference a static member baz, you need to change declaration to func bar<T: Foo>(_ foo: T) -> Self to make it work.

2 Likes

Ahh, right you are! Thanks for the quick response!

No worries, happy to help!