Commit Graph

1809 Commits

Author SHA1 Message Date
Pavel Yaskevich
beb23718f8 [Diagnostics] Improve diagnostics related to pointer initialization
- Increase impact of a generic pointer-to-pointer mismatch fix to
  match that of a contextual type failure. This allows more specific
  fixes to take precedence.

- De-prioritize generic argument mismatch fix when both types are
  pointers. This allows pointer-to-pointer conversion produce a
  more specific fix.

- De-prioritize fixes that involve `Builtin.RawPointer` or `OpaquePointer`
  in parameter positions. This helps to produce better diagnostics
  for argument mismatches during pointer initialization e.g.
  `UnsafeRawPointer(<pointer>)`.
2021-08-20 15:44:56 -07:00
Pavel Yaskevich
d5b30a671b Merge pull request #38941 from xedin/improve-inout-variadic-inference-for-closures
[ConstraintSystem] Always propagate inout/variadic flags to type-less…
2021-08-20 09:51:53 -07:00
Frederick Kellison-Linn
5f07747cb3 [Sema] Diagnose user-specified placeholders which couldn’t be resolved 2021-08-19 14:53:33 -04:00
Pavel Yaskevich
2aa98ebbf1 [ConstraintSystem] Always propagate inout/variadic flags to type-less closure parameters
Instead of infer all of the flags only for anonymous parameters,
let's infer inout/variadic for all type-less parameters - anonymous
and named. This allows to correctly form internal parameter type and
reduce amount of inference in the body (e.g. `BindParam` could be
simplified inline).
2021-08-18 14:53:55 -07:00
Doug Gregor
725624fc1b [SE-0286] Disable backward scanning for trailing closures in Swift 6 mode. 2021-08-18 05:54:54 -07:00
LucianoAlmeida
da234d0d74 [Sema][SR-15038] Use LocatorPathElt::GenericArgument to propagate (array, set, dictionary) generic elt in CheckedCast constraint and not record extraneous cast fixes for it 2021-08-16 21:34:29 -03:00
Robert Widmann
d75b34be22 Merge pull request #38753 from CodaFi/modulo-modules
[NFC] Remove Unused Module Parameter to Conformance Lookup
2021-08-11 10:36:39 -07:00
LucianoAlmeida
434ff12062 [Sema] Check for extra optionals injected on checked cast subexprs when recording extraneous cast warnings 2021-08-07 17:27:31 -03:00
Robert Widmann
592e90af9b Add Sugar for Variadics
We used to represent the interface type of variadic parameters directly
with ArraySliceType. This was awfully convenient for the constraint
solver since it could just canonicalize and open [T] to Array<$T>
wherever it saw a variadic parameter. However, this both destroys the
sugaring of T... and locks the representation to Array<T>. In the
interest of generalizing this in the future, introduce
VariadicSequenceType. For now, it canonicalizes to Array<T> just like
the old representation. But, as you can guess, this is a new staging
point for teaching the solver how to munge variadic generic type bindings.

rdar://81628287
2021-08-06 12:51:39 -07:00
Pavel Yaskevich
1509accca2 [ConstraintSystem] Open only directly declared opaque types related to return statements (#38762)
* [ConstraintSystem] NFC: Rename `openOpaqueTypeRec` and make `openOpaqueType` private for individual types

* [ConstraintSystem] Require a contextual purpose for `openOpaqueType(Type, ...)`

Some of the contexts require special handling e.g. return type of a function
can only open directly declared opaque result type(s), this would be
implemented in a follow-up commit.

* [ConstraintSystem] Open only directly declared opaque types related to return statements

Re-establish a check which would only open opaque types directly
declared in the return type of a function/accessor declaration,
otherwise constraint system would end up with type variables it
has no context for if the type had generic parameters.

Resolves: rdar://81531010
2021-08-05 20:45:46 -04:00
Robert Widmann
808220510e [NFC] Remove Unused Module Parameter to Conformance Lookup
It's been quite a long time since this unused parameter was introduced.
The intent is to produce the module as a root for the search - that is,
computing the set of conformances visible from that module, not the set
of conformances inside of that module. Callers have since been providing
all manner of module-scoped contexts to it.

Let's just get rid of it. When we want to teach protocol conformance
lookup to do this, we can revert this commit as a starting point and try
again.
2021-08-04 14:43:31 -07:00
willtunnels
319b3e64aa Add support for opaque result types in structural positions (#38392)
* [TypeResolver][TypeChecker] Add support for structural opaque result types

* [TypeResolver][TypeChecker] Clean up changes that add structural opaque result types
2021-08-03 23:45:02 -04:00
Doug Gregor
f9c34756ef Parameterize conformance lookup on whether "missing" conformances are allowed.
Many clients of the conformance lookup operations would prefer to get
an invalid conformance (== there is no conformance) rather than a
missing conformance. Parameterize the conformance lookup operations so
that most callers won't see missing conformances, by filtering them
out at the end. Opt-in those callers that do want to see missing
conformances so they can be diagnosed.
2021-08-03 00:10:45 -07:00
Holly Borla
2d851ff619 Merge pull request #38700 from hborla/improve-transitive-conformance-heuristic
[ConstraintSystem] Improve the "transitive conformance" heuristic and remove early conformance checking from `DisjunctionStep::shouldSkip`
2021-07-31 13:51:49 -07:00
Pavel Yaskevich
d61d9238e7 Merge pull request #38702 from nathawes/account-for-synthesized-args
[Sema][CSSimplify] Account for synthesized args when diagnosing labelling issues in matchCallArgumentsImpl
2021-07-30 09:09:38 -07:00
Nathan Hawes
82544451e6 [Sema][CSSimplify] Account for synthesized args when diagnosing labelling issues in matchCallArgumentsImpl
Unfulfilled params get arguments synthesized for them which are added
to the end of the argument list. Later logic to detect out-of-order
arguments and produce relabelling fixes didn't account for them though.
This improves several diagnostics for mislabelled call arguments in the
existing tests.
2021-07-30 14:53:34 +10:00
Holly Borla
0a617a8dc0 [ConstraintSystem] Fail transitive conformance constraints without checking
implicit conversions if the type does not conform and the current score
is equal to the best score.

Implicit conversions will always increase the score, so if the current
score is equal to the best score and an implicit conversion is needed to
satisfy a conformance requirement, the solver would end up pruning that
path later on anyway. It's better to fail early.
2021-07-29 19:27:35 -07:00
Hamish Knight
df566e3147 [AST] Rename decomposeInput/composeInput
Explicitly call out the fact we're dealing with
tuples.
2021-07-28 23:14:45 +01:00
Robert Widmann
1491eb0ca3 [NFC] Drop Some Superfluous Null Signature Guards 2021-07-22 23:27:06 -07:00
Robert Widmann
d86551de67 Lift Requirement and Parameter Accessors up to GenericSignature
Start treating the null {Can}GenericSignature as a regular signature
with no requirements and no parameters. This not only makes for a much
safer abstraction, but allows us to simplify a lot of the clients of
GenericSignature that would previously have to check for null before
using the abstraction.
2021-07-22 23:27:05 -07:00
Pavel Yaskevich
3e73f88512 [ConstraintSystem] NFC: Use stored result type while generating constraints for closures
Instead of passing/storing result type to constraint generator,
let's retrieve it from the constraint system when needed.
2021-07-14 16:28:26 -07:00
Pavel Yaskevich
001884e268 Merge pull request #38235 from xedin/rdar-79757320
[Diagnostics] Ignore result type failures if one side is a hole
2021-07-06 13:52:07 -07:00
Pavel Yaskevich
556c74401d [Diagnostics] Ignore result type failures if one side is a hole
If one of the sides in a application result conversion is a hole
it could only mean that the fix has been recorded earlier (at the
point where hole was introduced) and failure at function result
position could be safely ignored.

Resolves: rdar://79757320
2021-07-06 10:29:09 -07:00
Luciano Almeida
0a107363c2 [Sema] Avoid eargly record an apply arg mismatch for fn param type mismatch involving l-values 2021-07-05 20:04:55 -03:00
Pavel Yaskevich
aea8d246b5 [ConstraintSystem] Resolve dependent member as a hole if base is one
Instead of preserving unresolved dependent member types, let's resolve
them as holes if their base has been determined to be one.
2021-07-02 12:57:21 -07:00
Pavel Yaskevich
6aa0ea552a Merge pull request #38199 from xedin/rdar-65667992
[ConstraintSystem] Bind missing member in pattern match to a hole early
2021-07-02 12:40:13 -07:00
Pavel Yaskevich
c1d0036b09 [ConstraintSystem] Bind missing member in pattern match to a hole early
If lookup failed to find a member for a pattern match, let's bind
type variable representing such member to a hole right away, otherwise
there is a risk of missing other potential hole locations in pattern
function type (arguments + result type) because pattern matching
doesn't use 'applicable function' constraint.

Resolves: rdar://65667992
2021-07-01 10:01:54 -07:00
Doug Gregor
80b6fbde88 Merge pull request #38186 from DougGregor/actors-and-global-actors
Semantic tweaks to (global) actors to bring them in line with the accepted proposals
2021-06-30 19:02:12 -07:00
Alex Hoppen
39e92db1b1 Merge pull request #38049 from ahoppen/pr/keypath-completion
[CodeCompletion] Migrate key path completion to be solver based
2021-06-30 22:03:44 +02:00
Doug Gregor
9ec20776c9 [SE-0306] Make actors semantically "final".
Treat actors as being semantically `final` throughout the type checker.
This allows, for example, a non-`required` initializer to satisfy a
protocol requirement.

We're leaving the ABI open for actor inheritance should we need it.

Addresses rdar://78269551.
2021-06-30 10:59:49 -07:00
Pavel Yaskevich
f3b6ff009c Merge pull request #38104 from xedin/drop-optionality-for-contextual-closure-type
[CSSimplify] Look through optionals in contextual type while resolving a closure
2021-06-28 10:04:08 -07:00
Alex Hoppen
d64b8ecea6 [CodeCompletion] Migrate key path completion to be solver based
This commit essentially consistes of the following steps:
- Add a new code completion key path component that represents the code completion token inside a key path. Previously, the key path would have an invalid component at the end if it contained a code completion token.
- When type checking the key path, model the code completion token’s result type by a new type variable that is unrelated to the previous components (because the code completion token might resolve to anything).
- Since the code completion token is now properly modelled in the constraint system, we can use the solver based code completion implementation and inspect any solution determined by the constraint solver. The base type for code completion is now the result type of the key path component that preceeds the code completion component.

This resolves bugs where code completion was not working correctly if the key path’s type had a generic base or result type. It’s also nice to have moved another completion type over to the solver-based implementation.

Resolves rdar://78779234 [SR-14685] and rdar://78779335 [SR-14703]
2021-06-25 23:19:35 +02:00
Pavel Yaskevich
0562c31cad [CSSimplify] Look through optionals in contextual type while resolving a closure
Let's look through all optionals associated with contextual
type to make it possible to infer parameter/result type of
the closure faster e.g.:

```swift
func test(_: ((Int) -> Void)?) {
   ...
}

test { $0 + ... }
```

In this case dropping optionality from contextual type
`((Int) -> Void)?` allows `resolveClosure` to infer type
of `$0` directly (via `getContextualParamAt`) instead of
having to use type variable inference mechanism.
2021-06-24 15:29:01 -07:00
Holly Borla
d083c66686 [ConstraintSystem] Before matching tuple types, add each complete tuple
type to the locator.

This will provide context for tuple element mismatch diagnostics, instead
of attempting to compute the full tuple type in diagnostics code with a pile
of special cases (see getStructuralTypeContext in CSFix.cpp).
2021-06-24 09:40:14 -07:00
Pavel Yaskevich
be6e2fad8c [Diagnostics] Adjust Self conformance check to find non-decl overload choices
Use `findSelectedOverloadFor` instead of `findResolvedMemberRef`
to cover cases where member is found via base type unwrap, otherwise
conformance constraint would be re-inserted but never re-attempted
which results in a compiler crash.

Resolves: rdar://79268378
2021-06-22 15:32:51 -07:00
Pavel Yaskevich
c1bac12bb6 [CSSimplify] Fix warning check to account that "from" might be less optional than "to" type
While checking whether compiler needs to produce a checked cast warning,
account for the fact that "from" could be less optional than "to" e.g.
`0 as Any?`, so the difference has to be stored as a signed integer
otherwise it's going to underflow and result in a crash or infinite
recursion in the diagnostics.

Resolves: rdar://79523605
2021-06-21 16:38:04 -07:00
Doug Gregor
4afa371094 Introduce subtyping rule for functions with "isolated" parameters.
With isolated parameters being part of a function's type, check to
ensure that isolated and non-isolated parameters aren't incorrectly
matched. Specifically, it is okay to add `isolated` to a parameter
when there is a subtyping relationship, but not remove it:

```swift
actor A { }
func f(_: isolated A) { }
func g(_: A) { }

func test() {
  let _: (isolated A) -> Void = g    // okay to add 'isolated'
  let _: (A) -> Void = f             // error when removing 'isolated'
}
```
2021-06-07 23:59:39 -07:00
Pavel Yaskevich
9e54006cc6 [ConstraintSystem] Don't record a mismatch for synthesized arguments
Without context synthesized argument would be inferred as a hole,
so let's not record any additional fixes for it if there is no way
to infer it properly from a parameter (which could also be a hole
if it is a generic parameter type).

Resolves: rdar://78781552
2021-06-03 16:14:20 -07:00
Pavel Yaskevich
1c3d685fd8 [ConstraintSystem] Simplify relational constraints with the same dependent member type on both sides
If relational constraint has the same dependent member type on both
sides e.g. `$T1.Element == $T1.Element` allow its simplification,
since inference of `$T1` results in dependent member being resolved
to the same concrete type. Otherwise constraint system would be left
with this constraint in inactive state if `$T1` couldn't be resolved
which results in a crash.

Resolves: rdar://78623338
2021-05-28 17:49:55 -07:00
Joe Groff
92f56e7ec8 Allow conversions from actor-bound sync function type to unbound async function type.
For `async` function types, an actor constraint can be enforced by the callee by hopping executors,
unlike with `sync` functions, so doesn't need to influence the outward type of the function.

rdar://76248452
2021-05-21 14:17:50 -07:00
Pavel Yaskevich
87113d76ac Merge pull request #37430 from xedin/rdar-77942193
[ConstraintSystem] Detect passing sync closure to async argument early
2021-05-18 09:21:35 -07:00
Slava Pestov
131d3f4bce Sema: Pass down a ModuleDecl instead of a DeclContext to conformsToProtocol()
... and a bunch of follow-up simplifications pushing ModuleDecls further
up, since I couldn't resist the yak shave.
2021-05-17 16:34:18 -04:00
Pavel Yaskevich
ace4d3ac5e [ConstraintSystem] Detect passing sync closure to async argument early
This helps in situations where there are multiple overloads which
differ only in async effect of their parameters. Passing sync argument
to a sync parameter is always preferred and when detected early
allows solver to avoid some of the duplicate work re-solving for
the rest of the path e.g.

```swift
func test<T>(_: () -> T) {}
func test<T>(_: () async -> T) {}

test {
  // sync work
}.op(...)
```

In this case since closure is synchronous first overload of `test`
is always preferred (when it's a match) and solver can skip re-checking
body of the closure and `op` call when it encounters `async` version.

Resolves: rdar://77942193
2021-05-14 13:49:39 -07:00
Holly Borla
e1591314cf Merge pull request #37380 from hborla/property-wrapper-inference-crash
[ConstraintSystem] Fix a constraint system crash with property wrapper inference using the $ syntax.
2021-05-12 08:55:51 -07:00
Holly Borla
ef58f6a827 [ConstraintSystem] If the contextual parameter type doesn't exist when
resolving a closure, create a new type variable for inferred property
wrapper types.
2021-05-11 18:26:22 -07:00
Pavel Yaskevich
7ce8a44f6d [CSSimplify] Increase fix impact when passing closure to a non-function type parameter
In overloaded context it's possible that there is an overload that
expects a closure but it can have other issues e.g. different number
of parameters, so in order to pick a better solution let's always
increase a score for overloads where closure is matched against a
non-function type parameter.
2021-05-11 15:07:17 -07:00
Pavel Yaskevich
29cb7ddbcb [Diagnostics] Handle ambiguities related to use of nil literal
When `nil` is passed as an argument to call with multiple overloads
it's possible that this would result in ambiguity where matched
expected argument type doesn't conform to `ExpressibleByNilLiteral`.

To handle situations like this locator for contextual mismatch
has to be adjusted to point to the call where `nil` is used, so
`diagnoseAmbiguityWithFixes` can identify multiple overloads and
produce a correct ambiguity diagnostic.

Resolves: rdar://75514153
2021-05-05 12:56:25 -07:00
Luciano Almeida
c069e86f83 [NFC][Sema] Changing API to facilitate recording pontential holes recursively 2021-05-01 23:06:25 -03:00
Pavel Yaskevich
f36ecf2fa1 [CSSimplify] Allow overload choices with missing labels to be considered for diagnostics
Let's make use of a newly added "disable for performance" flag to
allow solver to consider overload choices where the only issue is
missing one or more labels - this makes it for a much better
diagnostic experience without any performance impact for valid code.
2021-04-28 12:04:57 -07:00
Pavel Yaskevich
71372bce67 [Diagnostics] Switch to use contextual purpose associated with locator
`ContextualFailure` is the main beneficiary of additional information
associated with `ContextualType` element because it no longer has to
query solution for "purpose" of the contextual information.

Resolves: rdar://68795727
2021-04-26 09:51:25 -07:00