Commit Graph

8 Commits

Author SHA1 Message Date
Doug Gregor
acc07db1a6 Update test case for "real" names of inferred associated type witnesses.
We might want to introduce some kind of type-printing adjustment to
avoid this, but for now let's be truthful.
2024-03-11 13:48:53 -07:00
Slava Pestov
af8c8f1671 Sema: Remove non-experimental associated type inference 2024-03-07 17:30:17 -05:00
Doug Gregor
6a3787796c Teach the old associated type inference about AsyncSequence.Failure
The new one is smart enough to figure out a type witness for
AsyncSequence.Failure from AsyncSequence.AsyncIterator.Failure, but the
old one doesn't have general logic to handle cases like these. Introduce
a bespoke rule to keep the old/new associated type inference logic in
sync for the newly-introduced Failure type.
2024-02-09 10:18:37 -08:00
Doug Gregor
e050294583 [Associated type inference] Limit Failure inference to rethrows next()
When inferring a type witness for `AsyncIteratorProtocol` or
`AsyncSequence`'s `Failure` associated type, don't infer from a
generic parameter named `Failure`. Instead, use `next()` as a cue: if
it `rethrows`, use `Failure` from one of the conformances; if it
`throws`, use `any Error`. This is a more conservative inference rule,
and addresses a failure to infer a `Failure` type witness for some
fairly-obvious cases.

Fixes rdar://122514816.
2024-02-08 18:16:05 -08:00
Doug Gregor
dc85ae3fc6 Choose between AsyncIteratorProtocol's next() and nextElement() based on availability
This allows us to not break backward deployment
2024-01-25 16:04:47 -08:00
Doug Gregor
0baa5b978b Typed throws is enabled by default, we don't need to opt in 2024-01-25 16:04:45 -08:00
Doug Gregor
bb7a563e6c Switch async for-each loop over to _nextElement and drop @rethrows.
This couples together several changes to move entirely from
`@rethrows` over to typed throws:

* Use the `Failure` type to determine whether an async for-each loop
will throw, rather than depending on rethrows checking

* Introduce a special carve-out for `rethrows` functions that have a
generic requirement on an `AsyncSequence` or `AsyncIteratorProtocol`,
which uses that requirement's `Failure` type as potentially being part
of the thrown error type. This allows existing generic functions like
the following to continue to work:

    func f<S: AsyncSequence>(_: S) rethrows

* Switch SIL generation for the async for-each loop from the prior
`next()` over to the typed-throws version `_nextElement`.

* Remove `@rethrows` from `AsyncSequence` and `AsyncIteratorProtocol`
entirely. We are now fully dependent on typed throws.
2024-01-25 16:04:43 -08:00
Doug Gregor
a5bdb12b48 Adopt typed throws in AsyncIteratorProtocol and AsyncSequence
Introduce a new associated type `Failure` into the two protocols involved
in async sequences, which represents the type thrown when the sequence
fails. Introduce a defaulted `_nextElement()` operations that throws
`Failure` or produces the next element of the sequence. Provide a
default implementation of `_nextElement()` in terms of `next()` that
force-cases the thrown error to the `Failure` type.

Introduce special associated type inference logic for the `Failure`
type of an `AsyncIteratorProtocol` conformance when there is no
specific _nextElement()` witness. This inference logic looks at the
witness for `next()`:
* If `next()` throws nothing, `Failure` is inferred to `Never`.
* If `next()` throws, `Failure` is inferred to `any Error`.
* If `next()` rethrows, `Failure` is inferred to `T.Failure`, where
`T` is the first type parameter with a conformance to either
`AsyncSequence` or `AsyncIteratorProtocol`.

The default implementation and the inference rule, together, allow
existing async sequences to continue working as before, and set us up
for changing the contract of the `async for` loop to use
`_nextIterator()` rather than `next()`.

Give `AsyncSequence` and `AsyncIteratorProtocol` primary associated
types for the element and failure types, which will allow them to be
used more generally with existential and opaque types.
2024-01-25 16:04:42 -08:00