Commit Graph

1149 Commits

Author SHA1 Message Date
Pavel Yaskevich
01691d6497 [ConstraintSystem] ExpressionTimer: Simplify locators only if allotted time has been exceeded 2022-03-17 00:51:25 -07:00
Pavel Yaskevich
eb6b267848 [ConstraintSystem] Implement simplification of Witness, WrappedValue, OptionalPayload, and IUO Choice 2022-03-17 00:51:25 -07:00
Pavel Yaskevich
a9e92111fc [ConstraintSystem] Augment condition/ternary simplication to support statements 2022-03-17 00:51:20 -07:00
Pavel Yaskevich
9bd05b7a00 [ConstraintSystem] Implement simplication of ImplicitConversion element 2022-03-16 14:29:16 -07:00
Pavel Yaskevich
8fe78d485e [ConstraintSystem] Implement simplication of {Pack, PatternBinding}Element elements 2022-03-16 14:27:17 -07:00
Pavel Yaskevich
029b11234b [ConstraintSystem] Implement simplication of generic requirement elements 2022-03-16 14:25:39 -07:00
Pavel Yaskevich
46ecae37e9 [ConstraintSystem] Implement simplication of Opened{Generic, OpaqueArchetype} elements 2022-03-16 14:25:05 -07:00
Pavel Yaskevich
3d8333bd71 [ConstraintSystem] Implement simplication of KeyPath{Root, Value} elements 2022-03-16 14:24:31 -07:00
Pavel Yaskevich
9dbda8573c [ConstraintSystem] Implement simplication of GenericParameter element 2022-03-16 14:23:38 -07:00
Pavel Yaskevich
6f3203dba1 [ConstraintSystem] Implement simplication of {DynamicLookup, KeyPathComponent}Result elements 2022-03-16 14:22:58 -07:00
Pavel Yaskevich
3f47ed2365 [ConstraintSystem] Implement simplication of {Function, Generic, Synthesized}Argument elements 2022-03-16 14:22:09 -07:00
Pavel Yaskevich
3b4c637b0a [ConstraintSystem] Implement simplication of assorted Type elements 2022-03-16 14:20:50 -07:00
Pavel Yaskevich
fed7c69112 [ConstraintSystem] Implement simplication of ClosureBodyElement element 2022-03-16 11:34:30 -07:00
Pavel Yaskevich
696c8f1d9d [ConstraintSystem] Implement simplication of PatternMatch element 2022-03-16 11:34:20 -07:00
Slava Pestov
5bb40ae66e Sema: Fix doesMemberHaveUnfulfillableConstraintsWithExistentialBase() for generic context
We don't want to pass in the outer generic signature here.

The base type's constraint type is written in terms of archetypes,
and we run generic signature queries against it with types appearing
in the protocol member. Since the protocol member has Self at
depth 0, index 0, prepending the outer generic signature to the
opened existential signature would produce incorrect results.
2022-03-09 17:27:06 -05:00
Slava Pestov
3f8e4e7442 AST: Fix mistake with generic signature in findExistentialSelfReferences()
We used to concatenate the DeclContext's generic signature with the
protocol requirement's signature, then look for occurrences of the
first generic parameter from the DeclContext's signature in the
requirement's type.

This almost worked, except when the first generic parameter from the
DeclContext's signature didn't conform to a protocol referenced by
an associated type. In that case, we would falsely report that there
are no 'Self' references.

Note that the CHECK lines in test/SILGen/witnesses_class.swift change
to what they were before 01d9d61cc8.
2022-03-09 02:38:09 -05:00
Robert Widmann
ab44a07045 Convert DeclContext Parameters to their Associated Generic Signatures Instead 2022-03-07 22:54:23 -08:00
Robert Widmann
01d9d61cc8 Add a Facility to Compute an Opened Archetype's Self Parameter 2022-03-07 22:54:23 -08:00
Robert Widmann
d6186c9cfb Add a DeclContext Parameter to Opened Archetype Construction
This ensures that opened archetypes always inherit any outer generic parameters from the context in which they reside. This matters because class bounds may bind generic parameters from these outer contexts, and without the outer context you can wind up with ill-formed generic environments like

<τ_0_0, where τ_0_0 : C<T>, τ_0_0 : P>

Where T is otherwise unbound because there is no entry for it among the generic parameters of the environment's associated generic signature.
2022-03-07 22:54:22 -08:00
Pavel Yaskevich
5ec5ffcfda Merge pull request #41570 from xedin/add-expr-pattern-handling-to-solver
[ConstraintSystem] Add support for expression patterns
2022-02-28 09:39:26 -08:00
Pavel Yaskevich
50d0508a4b [ConstraintSystem] Add a new contextual type purpose - expression pattern
This makes it possible to associate expression with its parent pattern.
2022-02-25 12:30:22 -08:00
Pavel Yaskevich
8cfdb9999c Merge pull request #41436 from xedin/allow-specialization-from-default-expr
[TypeChecker] Allow inference from default expressions in certain scenarios (under a flag)
2022-02-24 08:57:42 -08:00
LucianoAlmeida
fdbf98af30 [NFC] Clarify comments for SR-15843 2022-02-23 18:08:06 -03:00
LucianoAlmeida
034c4bd77e [Sema] Attempt diagnose generic arg ambiguity if all solutions produce generic arg mismatch 2022-02-22 22:38:51 -03:00
Pavel Yaskevich
a6f86c453d [ConstraintSystem] NFC: Extract opening of individual generic parameters into a method 2022-02-21 09:59:53 -08:00
Pavel Yaskevich
23297c94e6 [ConstraintSystem] NFC: Extract opening of individual requirements into a method
It's a convenient way to use existing logic for default argument inference
because suhc inference cannot open whole signature but only conformance
and layout constraints associated generic parameters used in a particular
parameter position.
2022-02-21 09:59:53 -08:00
Ben Barham
01b539e27b Merge pull request #41461 from bnbarham/overload-anchor-fallback
[CS] Add a fallback for overload locators that failed to simplify
2022-02-18 19:00:55 -08:00
Ben Barham
c05abfebec [CS] Add a fallback for overload locators that failed to simplify
If an overload locator couldn't be simplified to its anchor, fallback to
the anchor of the locator instead. Also add an assert so that we can
track down whether this is actually valid or not.

Resolves rdar://89097800.
2022-02-18 14:25:57 -08:00
Doug Gregor
fa6c3c15d8 Opaque types are only invariant when they involve same-type constraints.
An opaque type is only invariant with respect to the existential Self
when the constraints on the opaque type involve Self. Such constraints
are not expressible in the type-erased value, so treat them as
invariant. This loosens the restriction on using members of protocol
type that return an opaque type, such that (e.g.) the following is
still well-formed:

    protocol P { }
    protocol Q { }

    extension P {
      func getQ() -> some Q { ... }
    }

    func test(p: any P) {
      let q = p.getQ() // formerly an error, now returns an "any Q"
    }

However, this does not permit uses of members such as:

    extension P {
      func getCollection() -> some Collection<Self> { ... } // error
    }

because the type system cannot express the corresponding existential
type `any Collection<Self>`.
2022-02-18 11:22:56 -08:00
Doug Gregor
4ec6dc1e4a Erase opaque types involving opened existential types to their upper bound. 2022-02-18 11:22:56 -08:00
Doug Gregor
1e1b3427c3 Experimental support for implicitly opening existential arguments.
When calling a generic function with an argument of existential type,
implicitly "open" the existential type into a concrete archetype, which
can then be bound to the generic type. This extends the implicit
opening that is performed when accessing a member of an existential
type from the "self" parameter to all parameters. For example:

    func unsafeFirst<C: Collection>(_ c: C) -> C.Element { c.first! }

    func g(c: any Collection) {
      unsafeFirst(c)   // currently an error
                       // with this change, succeeds and produces an 'Any'
    }

This avoids many common sources of errors of the form

    protocol 'P' as a type cannot conform to the protocol itself

which come from calling generic functions with an existential, and
allows another way "out" if one has an existention and needs to treat
it generically.

This feature is behind a frontend flag
`-enable-experimental-opened-existential-types`.
2022-02-18 11:22:56 -08:00
Pavel Yaskevich
0cc8bc7928 [CSClosure] SE-0326: Type-checker statement conditions individually
Instead of referencing whole statement condition, break it down to
individual elements and solve them separately.

Resolves: rdar://88720312
2022-02-12 10:01:58 -08:00
Pavel Yaskevich
57ce1d2360 Merge pull request #41282 from xedin/too-complex-with-multistmt-cljs
[ConstraintSystem] Adjust expression complexity computation to account for multi-statement closures
2022-02-10 08:41:40 -08:00
Doug Gregor
4325cf429d Ensure that async/synchronous overloading works in implicit-async closures.
When we are within a closure that is not required to be asynchronous
(i.e., it has no `await` in it), make sure that we prefer synchronous
functions to asynchronous ones, even if this closure will later be
converted to `async` and the constraint system knows that.

Fixes rdar://88692889.
2022-02-09 12:47:04 -08:00
Pavel Yaskevich
f1e602f98d Merge pull request #41189 from xedin/trailing-closures-with-callAsFunction
[ConstraintSystem] Match trailing closures to implicit `.callAsFunction` when necessary
2022-02-08 17:44:55 -08:00
Pavel Yaskevich
d0031403cc [ConstraintSystem] ExpressionTimer: Accept locators as timer anchors 2022-02-08 14:33:46 -08:00
Pavel Yaskevich
e60afb3446 [ConstraintSystem] Augment ExpressionTimer to carry the threshold
Instead of asking callers of `isExpired` to provide the threshold,
let's ask for that upfront. This change also allows us to check how
much time remains in the timer and build timers with different
thresholds without having to safe that information somewhere else.
2022-02-08 14:33:46 -08:00
Pavel Yaskevich
49b181136b [AST] Augment ArgumentList::createImplicit to accept first trailing closure index 2022-02-03 19:41:00 -08:00
Pavel Yaskevich
b48a064f62 [ConstraintSystem] Add storage for implicitly generated roots of .callAsFunction
Some implicit calls to `.callAsFunction` require that a new root
expression to be created for them in order to record argument list
and resolved overload choice.
2022-02-03 15:35:50 -08:00
Anthony Latsis
c7fd60f2dd Merge pull request #39492 from AnthonyLatsis/se-309-2
SE-309: Covariant erasure for dependent member types
2022-02-02 07:07:17 +03:00
Anthony Latsis
24c19d4ad4 Sema: Enable covariant erasure for dependent member types 2022-02-02 02:10:05 +03:00
Anthony Latsis
b3ee4b0718 AST, Sema: Use the opened archetype's generic signature to determine existential member availability 2022-02-02 02:09:59 +03:00
Anthony Latsis
b2fe028885 [NFC] Turn SelfReferenceKind into a standalone utility type 2022-02-01 20:55:47 +03:00
Anthony Latsis
427190d348 Sema: Handle generic-signature-based existential availability 2022-02-01 20:55:46 +03:00
Anthony Latsis
3206f5ab34 [NFC] Preemptively relocate ProtocolDecl::isAvailableInExistential() and co. 2022-02-01 20:55:46 +03:00
Evan Wilde
4b182f6a61 Move findAsyncNode to BraceStmt
I need to determine when top-level code contains an `await` to determine
whether to make the source file an async context. This logic is
perfectly encapsulated in the `FindInnerAsync` AST walker.
Unfortunately, that is pushed down in Sema/ConstraintSystem and isn't
available at the AST level. I've pulled it up into the brace statement
so that I can use that as part of determining whether the source file is
async in `DeclContext::isAsyncContext`.

Unfortunately, statements don't have an AST context or evaluator or I
would make this a request.
2022-01-28 07:55:14 -08:00
Holly Borla
76e6156857 [Sema] Construct OpenedArchetypeType with a canonical existential type.
Opened archetypes can be created in the constraint system, and the
existential type it wraps can contain type variables. This can happen
when the existential type is inferred through a typealias inside a
generic type, and a member reference whose base is the opened existential
gets bound before binding the generic arguments of the parent type.

However, simplifying opened archetypes to replace type variables is
not yet supported, which leads to type variables escaping the constraint
system. We can support cases where the underlying existential type doesn't
depend on the type variables by canonicalizing it when opening the
existential. Cases where the underlying type requires resolved generic
arguments are still unsupported for now.
2022-01-19 22:53:52 -08:00
Holly Borla
626bea2bc6 [ConstraintSystem] Return an existential type in getTypeOfMemberReference
when the member is a typealias to a protocol or composition.
2022-01-13 22:31:55 -08:00
Holly Borla
5dced8e5f9 [ConstraintSystem] Fix a few constraint system corner cases with explicit
existential types.
2022-01-13 19:30:44 -08:00
Holly Borla
d971d489d6 [Type System] With explicit existential types, make sure existential
metatypes always wrap the constraint type directly, and the instance
type of an existential metatype is an existential type.
2022-01-13 19:30:44 -08:00