Commit Graph

1263 Commits

Author SHA1 Message Date
Alex Hoppen
0ded798ceb Merge pull request #42006 from ahoppen/pr/prepare-for-migrate-postfixexprparen
[CodeCompletion][Sema] Multiple improvements to prepare for migration of PostfixExprParen to solver-based
2022-04-03 16:43:36 +02:00
Evan Wilde
2eb801145e Merge branch 'main' into ewilde/async-main-resolution 2022-04-02 16:15:10 -07:00
Evan Wilde
da0a3311a5 Add -async-main flag to favor asynchronous main
This flag biases the overload checker in favor of selecting an
asynchronous main function over a synchronous main. If no asynchronous
main function exists, a synchronous one will still be selected.
Likewise, if the flag is not passed and there are only asynchronous main
functions available, the most specific asynchronous main function will
still be selected.
2022-04-02 16:14:01 -07:00
Doug Gregor
b94d6eb874 Merge pull request #42121 from xedin/rdar-91110069
[ConstraintSystem] Fix a bug in existential Self erasure
2022-03-31 20:55:34 -07:00
Pavel Yaskevich
9e3432a833 [ConstraintSystem] Fix a bug in existential Self erasure
`typeEraseExistentialSelfReferences` shouldn't account for
contextual signature because that signature could have
generic parameters of it's own unrelated to the reference
which would be located before generic parameters of the
member, e.g. when the code is located in a protocol extension,
which invalidates the assumption that `Self` is located at
depth = 0, index = 0.

Resolves: rdar://91110069
2022-03-31 13:19:33 -07:00
Doug Gregor
89ab4aac30 Fix the "early" computation of a return type to respect @preconcurrency.
Fixes rdar://91087622.
2022-03-31 12:46:00 -07:00
Anthony Latsis
324913055d CS: Handle unbound references to @objc optional methods 2022-03-30 19:11:29 +03:00
Alex Hoppen
237376b980 [CodeCompletion] Call into simplifyTypeImpl when simplifying a type for code completion
This makes sure we e.g. resolve dependent member types after we perform type variable to generic parameter substitutions for code completion
2022-03-25 09:36:05 +01:00
Doug Gregor
38c9d2e8dd Merge pull request #41992 from DougGregor/implicit-existential-opening-erase-or-reject 2022-03-23 21:03:54 -07:00
Doug Gregor
50451d2583 Type-erase contravariant uses of opened existentials in subsequent parameters.
When we open an existential argument in a call to a generic function,
type-erase contravariant uses of that opened existential in subsequent
parameters. This primarily impacts closure parameters, where we want
the closure to be provided with an existential parameter type rather
than permit the parameter to have opened existential type. This
prevents the opened existential type from being directly exposed in
the type system.

Note that we do not need to perform this erasure when the argument is
a reference to a generic function, because there it is suitable to
infer that the generic arguments are the opened archetypes. This
subsumes the use case for `_openExistential`.
2022-03-23 15:22:06 -07:00
Pavel Yaskevich
3c5a6f93e9 Merge pull request #41864 from xedin/improvements-for-diagnoseAmbiguity
[ConstraintSystem] Augment `diagnoseAmbiguity` to handle non-expression anchors
2022-03-21 10:00:19 -07:00
Alex Hoppen
17ac201b29 Merge pull request #39373 from ahoppen/pr/solver-based-arg-completion
[CodeCompletion] Migrate argument position completion to the solver-based implementation
2022-03-17 22:41:59 +01:00
Pavel Yaskevich
ebdef12fe1 [ContraintSystem] Augment diagnoseAmbiguity to diagnose non-expression ambiguities
Since constraint solver can now handle statements, patterns, and declarations,
it's possible to that ambiguity could be detected in a non-expression context,
for example - pattern matching in switch statements. Augment `diagnoseAmbiguity`
to accept overloads with non-expression anchors and diagnose them in order of
their appearance in a solution.
2022-03-17 13:02:20 -07:00
Alex Hoppen
f538d33e5f [CodeCompletion][Sema] Migrate CallArgurment position completion to the solver-based implementation
This hooks up call argument position completion to the typeCheckForCodeCompletion API to generate completions from all the solutions the constraint solver produces (even those requiring fixes), rather than relying on a single solution being applied to the AST (if any).

Co-authored-by: Nathan Hawes <nathan.john.hawes@gmail.com>
2022-03-17 15:15:54 +01:00
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