Skip to content

[ConstraintSystem] Use witnesses for makeIterator and next refs in for-in context #61091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 15, 2022

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Sep 14, 2022

References to makeIterator and next in for-in loop context have to be
treated as references to witnesses of Sequence#makeIterator and
IteratorProtocol#next requirements. Letting overload resolution decide as-if
loop was written direct use of makeIterator() and while let ... = next() statement
could lead to problems with retroactive conformances and difference in runtime
behavior.

For example:

extension Array where Element == Double {
  func makeIterator() -> Array<Int>.Iterator {
    return [1, 2, 3].makeIterator()
  }
}

for i in [4.0, 5.0, 6.0] {
  print(i)
}

makeIterator declared in the Array extension is not a witness to Sequence#makeIterator
and shouldn't be used because that would change runtime behavior.

Resolves: #60514
Resolves: #60958

@xedin xedin requested a review from hborla September 14, 2022 07:47
@xedin
Copy link
Contributor Author

xedin commented Sep 14, 2022

This is an alternative approach to #61075.

@xedin
Copy link
Contributor Author

xedin commented Sep 14, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Sep 14, 2022

@swift-ci please test source compatibility

…in for-in

Reference to `makeIterator` in for-in loop context needs to be
treated as a reference to a witness of `Sequence#makeIterator`
requirement, otherwise it could lead to problems with retroactive
conformances.
Reference to `$geneator.next` in for-in loop context needs to be
treated as a reference to a witness of `IteratorProtocol#next`
requirement, otherwise it could lead to problems with retroactive
conformances.
@xedin
Copy link
Contributor Author

xedin commented Sep 14, 2022

@swift-ci please test

1 similar comment
@xedin
Copy link
Contributor Author

xedin commented Sep 15, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Sep 15, 2022

@swift-ci please test source compatibility

@xedin xedin merged commit c4b7d6d into swiftlang:main Sep 15, 2022
@xwu
Copy link
Collaborator

xwu commented Sep 15, 2022

🎉🎉🎉 Can we nominate for 5.7 branch?

@xedin
Copy link
Contributor Author

xedin commented Sep 15, 2022

@xwu It's in - #61109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Regression] Ambiguous makeIterator() in 5.7 Sequence/Iterator conformance broken in 5.7
3 participants