Commit Graph

1988 Commits

Author SHA1 Message Date
Xi Ge
6206d7cc9d sema: accept an array literal of enum case references as compile-time constant 2022-03-24 21:44:09 -07:00
Xi Ge
d26760e49a Merge pull request #42010 from nkcsgexi/90210674 2022-03-24 16:13:22 -07:00
Doug Gregor
25e71a3a3b Don't open an existential argument that's completely self-conforming.
Such existential arguments will already meet the requirements of a
generic function they are passed to, but would change runtime
semantics. Therefore, don't open the existential in such cases.
2022-03-24 13:08:00 -07:00
Xi Ge
121f39c790 sema: set locator properly when diagnosing constness mismatches
rdar://90210674
2022-03-24 12:47:28 -07: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
9d413b3668 Merge pull request #41939 from xedin/rdar-87407899
[Diagnostics] Fix out-of-bounds index while fixing argument mismatch
2022-03-22 22:46:54 -07:00
Alex Hoppen
1ef2c5972c Merge pull request #41917 from ahoppen/pr/filter-overloads-if-no-cc-token
[CodeCompletion] Filter overloads if their function application doesn't contain the code completion token
2022-03-22 12:11:29 +01:00
Alex Hoppen
045302b4cb [CodeCompletion] Filter overloads if their function application doesn't contain the code completion token
When solving for code completion, we weren't disabling overloads because the call might be malfored in the presence of a code completion token (because the user is only now writing the function call). But this logic doesn't apply to function calls that don't even involve the code completion token, which happens if completing in result builders.

I am hoping that this significantly improves code completion performance inside result builders.
2022-03-22 08:07:46 +01:00
Pavel Yaskevich
b6c2b3edff [Diagnostics] Fix out-of-bounds index while fixing argument mismatch
Although the overload choice has two parameters, it doesn't
mean that there are exactly two arguments passed to it.

Resolves: rdar://87407899
2022-03-21 14:57:50 -07:00
Alex Hoppen
458ce70245 Merge pull request #41633 from ahoppen/pr/solver-based-global-completions
[CodeCompletion] Migrate expression completions to solver-based
2022-03-21 20:02:47 +01:00
Alex Hoppen
e2a62f1a60 [CodeCompletion] Migrate expression completions to solver-based 2022-03-21 13:00:33 +01:00
Alex Hoppen
82fc059018 [CodeCompletion] Store ignored arguments as Expr * instead of ConstraintLocators
This avoids the construction of `ConstraintLocator`s.
2022-03-18 15:14:31 +01: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
Robert Widmann
43189db520 Merge pull request #41845 from CodaFi/flag-sitter 2022-03-16 22:15:12 -07:00
Luciano Almeida
1505d7cc14 [Diagnostic][Sema] Record locator with OptionalPayload dropped for contextual mismatch fixes for existentials 2022-03-16 18:31:50 -03:00
Robert Widmann
363954a416 Put Variadic Generics Behind a Flag 2022-03-16 14:22:03 -07:00
Robert Widmann
4efcb825f5 Unlock Opaque Types in Parameterized Protocols
Represent this in much the same way that collections do by creating an opaque value representing the source argument, and a conversion expression representing the destination argument.
2022-03-08 23:45:37 -08:00
Robert Widmann
635d253eb6 Unify ParameterizedProtocolTypes
Add deep equality typing rules for

P =~= Q, T == X, U == Y, V == Z, ...
--------------------------------
P<T, U, V, ...> == Q<X, Y, Z, ...>

Also add existential matching rules to discharge the constraints on parameterized protocols against concrete types. e.g.

class C: P { typealias T = Int }
func foo<T>(_ x: P<T>) {}
foo(C())

Should cause T to unify against C.T = Int
2022-03-08 22:01:21 -08:00
Doug Gregor
69c998f70a [Constraint system] Have simplifyOpenedExistentialOfConstraint use openExistentialType
... rather than re-implementing it.
2022-03-08 10:32:29 -08:00
Doug Gregor
02a13f3507 Allow explicit "as <existential-type>" to disable implicit opening of existentials 2022-03-08 10:31:35 -08:00
Robert Widmann
ab44a07045 Convert DeclContext Parameters to their Associated Generic Signatures Instead 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
26d85520a2 Merge pull request #41589 from xedin/handle-expr-patterns-in-the-solver
[ConstraintSystem] Support solving expression patterns via injecting call to `~=` operator
2022-03-02 16:33:52 -08:00
swift-ci
ad903094c0 Merge pull request #41386 from beccadax/your-library-is-overdue
Weaken some type checks for @preconcurrency decls
2022-03-01 16:13:59 -08:00
Pavel Yaskevich
59154d6d71 [ConstraintSystem] Support solving expression patterns via injecting call to ~= operator
Augment the constraint solver to fallback to implicit `~=` application
when member couldn't be found for `EnumElement` patterns because
`case` statement should be able to match enum member directly, as well
as through an implicit `~=` operator application.
2022-02-28 17:02:28 -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
Pavel Yaskevich
bf820c5dab [CSDiagnostics] Look through l-value conversions when mismatch is in subscript setter
`repairFailures` needs a special case when l-value conversion is
associated with a result type of subscript setter because otherwise
it falls through and treats result type as if it's an argument type,
which leads to crashes.

Resolves: rdar://84580119
2022-02-21 16:32:25 -08:00
Pavel Yaskevich
82b955b93c [CSDiagnostics] Detect and diagnose contextual mismatches with default value 2022-02-21 09:59:54 -08:00
Pavel Yaskevich
aa286b7bf4 [CSSimplify] Check whether parameter refers to generic parameters before attempting inference from default 2022-02-21 09:59:54 -08:00
Pavel Yaskevich
aea76ca358 [TypeChecker] Extend type inference from default expressions to handle nested generic parameters
Adds support for parameter types like `[T?]` or `[(T, U?)]`,
and relaxes restriction on same-type generic parameters.

A same-type requirement is acceptable if it only includes
in-scope generic parameters and concrete types i.e. `T.X == Int`
if accepted if `T` is referenced only by a parameter default
expression is being applied to.
2022-02-21 09:59:54 -08:00
Pavel Yaskevich
a6e84b3d05 [ConstraintSystem] Add call site support for type inference from default expressions 2022-02-21 09:59:53 -08:00
Doug Gregor
4141e20ad6 Revert "Allow existential opening for parameters of optional type."
This reverts commit 6e7fff7e65283ae9b25116fa6b75ba92fd2f2a58. The
asymmetry between opening for optional parameters and optional
arguments is surprising enough that we're currently opting not to
allow them.
2022-02-18 11:22:57 -08:00
Doug Gregor
e1f68b26c5 Allow existential opening for parameters of optional type. 2022-02-18 11:22:57 -08:00
Doug Gregor
e30c8a5c09 Allow opening existentials for inout parameters. 2022-02-18 11:22:57 -08:00
Doug Gregor
76d1ba75f8 Only open existentials for generic arguments on the called declaration 2022-02-18 11:22:57 -08:00
Doug Gregor
9a7b9c6216 Don't open existentials when calling C++ function templates
C++ function templates require specialization, which does not work with
opened existentials. Disable opening for them.
2022-02-18 11:22:57 -08:00
Doug Gregor
33be0beb12 Ensure that we only open existential arguments to functions/subscripts. 2022-02-18 11:22:57 -08:00
Doug Gregor
deab0b9bd7 Don't open existential arguments to type(of:). 2022-02-18 11:22:57 -08:00
Doug Gregor
1273fa4185 Support opening of arguments of existential metatype type. 2022-02-18 11:22:57 -08:00
Doug Gregor
51abd3d56f Prevent opening existentials when the corresponding parameter is variant
Ensure that we only open existentials in an argument when the corresponding
parameter's type is a generic parameter that is only used in covariant
positions, because either invariant or contravariant uses mean that we
won't be able to type-erase other uses of that parameter. This mirrors
the requirement placed when opening existentials as a member of protocol
type.
2022-02-18 11:22:57 -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
Becca Royal-Gordon
e04ec5a7d9 Weaken some type checks for @preconcurrency decls
In an async context, trying to pass a non-`@Sendable` function to an `@Sendable` parameter or trying to assign a `@MainActor` method to a non-`@MainActor`-typed variable were hard errors. We now think that this a mistake for `@preconcurrency` APIs in Swift 5 mode, as it hinders retroactive adoption of `@Sendable` and `@MainActor` by libraries.

This PR weakens these errors to warnings *only* when the decl which contains the attribute in its type signature is `@preconcurrency` and *only* when in Swift 5 mode (with or without -warn-concurrency). For non-`@preconcurrency` decls, it is still an error.

Fixes <rdar://88703266>.
2022-02-15 13:35:55 -08:00
Hamish Knight
04b37ae1d0 [CS] Don't apply compatibility logic in Swift 6 mode
This was only intended to preserve compatibility
for cases where the constraint system may have
previously accepted invalid code. Drop it in Swift 6
mode such that invalid collection coercions become
errors.
2022-02-10 19:06:49 +00:00
Hamish Knight
63c094c28b [CS] Don't apply compatibility logic to collection literals
We currently permit (but warn on) coercions of
collections with unrelated element types in
certain cases. This is done in an effort to preserve
compatibility with pre-5.3 compilers that may have
allowed such code to compile due to dropping
constraints while solving.

This is limited to collection coercions as we
emit somewhat reasonable code for them that
performs a force cast of the elements at runtime.
However, it turns out that in the case of collection
literals, we peephole the element conversions in
CSApply, leading to codegen crashes. As such,
narrow down the condition of this compatibility
logic to not apply to collection literals, as this
never would have compiled properly.

rdar://88334481
2022-02-10 19:06:48 +00: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
dee174acca [CSSimplify] Simplify new argument/trailing closure splitting code for .callAsFunction injection 2022-02-04 11:20:32 -08:00
Pavel Yaskevich
49b181136b [AST] Augment ArgumentList::createImplicit to accept first trailing closure index 2022-02-03 19:41:00 -08:00