Skip to content

Generic overload resolution fails in for-in statement #59522

Closed
@natecook1000

Description

@natecook1000

Describe the bug
When using a Collection method with multiple overloads in for-in loop inside a generic context, the compiler reports ambiguity where there is none.

To Reproduce
Attempt to compile the following code (also attached):

func f<C: Collection>(_ c: C) {
  for _ in c.prefix(1) {}
}

Compilation fails with this error message:

test.swift:3:12: error: ambiguous use of 'prefix'
  for _ in c.prefix(1) {}
           ^
Swift.Collection:6:40: note: found this candidate
    @inlinable public __consuming func prefix(_ maxLength: Int) -> Self.SubSequence
                                       ^
Swift.Sequence:7:40: note: found this candidate
    @inlinable public __consuming func prefix(_ maxLength: Int) -> PrefixSequence<Self>
                                       ^

Expected behavior
Successful compilation.

Environment
The earliest snapshot that fails to compile is from 2022-06-07:

Apple Swift version 5.8-dev (LLVM fdfb8ac496e4538, Swift c9f10040f7f3838)

The code above compiles correctly using the 2022-06-02 snapshot:

Apple Swift version 5.8-dev (LLVM 278d67f38c6a910, Swift ee312bc1e20eb01)

Additional context
Assigning the result of c.prefix(1) before the loop or using it in a for-in loop in a non-generic context both succeed.

// OK
func g<C: Collection>(_ c: C) {
  let pre = c.prefix(1)
  for _ in pre {}
}

// OK
func h(_ c: Array<Int>) {
  for _ in c.prefix(1) {}
}

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions