Skip to content

[TypeChecker] Rework type-checking of for-in statements #59003

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 17 commits into from
Jun 2, 2022

Conversation

xedin
Copy link
Contributor

@xedin xedin commented May 20, 2022

Change the way how for-in statements are type-checked, where a lot of
information has to be recorded in a statement in order for SILGen to generate
appropriate calls to makeIterator and next witnesses.

Instead of just finding a witness, solver would now:

  • Synthesize:
    • Iterator variable with its initializer - $generator = <sequence>.makeIterator();
    • Call to $generator.next() which is called by each loop iteration;
  • Verify that type of pattern is convertible to type of next()

Doing so significantly simplifies SILGen, removes the need to carry type information
in ForEachStmt and enables iterating over existential types.

Resolves: rdar://92177656

@xedin xedin requested a review from hborla May 20, 2022 21:03
@hborla hborla requested a review from slavapestov May 23, 2022 16:10
@xedin
Copy link
Contributor Author

xedin commented May 24, 2022

@swift-ci please test source compatibility

@xedin
Copy link
Contributor Author

xedin commented May 27, 2022

@swift-ci please test

1 similar comment
@xedin
Copy link
Contributor Author

xedin commented May 27, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented May 29, 2022

@swift-ci please test macOS platform

@xedin
Copy link
Contributor Author

xedin commented May 29, 2022

@swift-ci please test Linux platform

xedin added 17 commits May 30, 2022 23:17
Previously for-in target was actually an expression target, which means
certain type-checking behavior. These changes make it a standalone target
with custom behavior which would allow solver to introduce implicit
`makeIterator` and `next` calls and move some logic from SILGen.
… targets

The dependency on target being an expression is artificial,
`typeCheckForCodeCompletion` is capable of type-checking any
type of target.
Use newly updated `forEachStmt` target to handle for-in statement
preamble when it appears in a multi-statement closure.
Using `typeCheckTarget` removes almost all the duplicate logic.
Use a conformance reference recorded in a `ForEachStmt` by
the type-checker, instead of trying to look it up again.
The actual issue in this cases is that the type of a sequence
doesn't conform to `Sequence` protocol.
Instead of asking SILGen to build calls to `makeIterator` and
`$generator.next()`, let's synthesize and type-check them
together with the rest of for-in preamble. This greatly simplifies
interaction between Sema and SILGen for for-in statements.
The actual issue in this cases is that the type of a sequence
doesn't conform to `Sequence` protocol, which means that it's
impossible to deduce a base for `next` call.
Implicitly generated code requires checking but it doesn't have
to produce a diagnostic into the void.
This allows to use `for-in` statement to iterate over i.e. `any Collection`
and other existentials that conform to `Sequence` protocol.
…ions

Just like `any Collection`, it should be possible to use `any Sequence`
to iterate over.
Don't so would disconnect $generator from `.next()` call which
have to be solved together because they sometimes depend on the
for-in pattern to infer the element type.
@xedin
Copy link
Contributor Author

xedin commented May 31, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Jun 1, 2022

@swift-ci please test source compatibility

1 similar comment
@xedin
Copy link
Contributor Author

xedin commented Jun 2, 2022

@swift-ci please test source compatibility

@xedin
Copy link
Contributor Author

xedin commented Jun 2, 2022

@swift-ci please test source compatibility

2 similar comments
@shahmishal
Copy link
Member

@swift-ci please test source compatibility

@xedin
Copy link
Contributor Author

xedin commented Jun 2, 2022

@swift-ci please test source compatibility

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.

3 participants