Commit Graph

2231 Commits

Author SHA1 Message Date
Alex Hoppen
57d504cbcd Merge pull request #60062 from ahoppen/pr/placeholder-for-errors
[CS] Don’t fail constraint generation for ErrorExpr or if type fails to resolve
2022-07-20 14:15:43 +02:00
Alex Hoppen
f623440f01 [CS] Disfavor solutions that were unable to infer the variable type inside a named pattern
We need this to resolve a test failure in optional.swift.
2022-07-20 09:47:16 +02:00
Alex Hoppen
e14fa7291f [CS] Don’t fail constraint generation for ErrorExpr or if type fails to resolve
Instead of failing constraint generation by returning `nullptr` for an `ErrorExpr` or returning a null type when a type fails to be resolved, return a fresh type variable. This allows the constraint solver to continue further and produce more meaningful diagnostics.

Most importantly, it allows us to produce a solution where previously constraint generation for a syntactic element had failed, which is required to type check multi-statement closures in result builders inside the constraint system.
2022-07-20 09:46:12 +02:00
Pavel Yaskevich
b1930a89aa Merge pull request #60080 from xedin/rdar-88513939
[CSSimplify] Allow referencing typealias declarations via leading-dot…
2022-07-19 09:55:12 -07:00
Pavel Yaskevich
a35df66476 Merge pull request #60024 from xedin/rdar-96631324
[CSSimplify] Don't fix invalid r-value use for misplaced `&`
2022-07-19 09:15:00 -07:00
Pavel Yaskevich
12776182f0 [CSSimplify] Allow referencing typealias declarations via leading-dot syntax in generic context
Previously only static methods found on protocols were allowed but
it is reasonable to reference a typealias to perform an implicit call
to `.init` on its underlying type.

Resolves: rdar://88513939
2022-07-15 17:05:09 -07:00
John Holdsworth
d72b9a4d81 Cater for opening var existential followup. 2022-07-15 12:29:41 -07:00
John Holdsworth
6caf82705d Cater for case when existential to open is a "var". 2022-07-15 12:29:41 -07:00
Pavel Yaskevich
ff6112024e [CSSimplify] Don't fix invalid r-value use for misplaced &
When `&` is misplaced it creates r-value -> l-value mismatch
in the code which is just a consequence and shouldn't be diagnosed.

Resolves: rdar://96631324
2022-07-12 13:55:04 -07:00
Doug Gregor
db14ab140f Sink concurrency constraint fix behavior logic into "attempt" functions.
This is a cleaner pattern for the solver, thanks Pavel!
2022-07-08 13:33:14 -07:00
Doug Gregor
7436fb365a Rename CSFix::affectsSolutionScore() to impact(). 2022-07-08 13:03:12 -07:00
Doug Gregor
4fc4bd79e8 [Constraint solver] Track "isolated by preconcurrency" in the solver.
Rather than only setting the isolated-by-preconcurrency bit during
constraint application, track the closures it will be set for as part
of the constraint system and solution. Then, use that bit when
performing "strict concurrency context" checks and type adjustments,
so we don't treat an inferred-to-by-`@Sendable`-by-preconcurrency
closure in the solver as if it weren't related to preconcurrency.

Fixes the spurious warning from
https://github.com/apple/swift/issues/59910.
2022-07-07 23:46:30 -07:00
Doug Gregor
c2f88abfeb [Constraint system] Always add recoverable concurrency fixes.
This avoids us having to go down the less-efficient "salvage" path when
dealing with concurrency issues. It also fixes overloading behavior
when dealing with `@preconcurrency` and `@Sendable` functions,
such as in https://github.com/apple/swift/issues/59909.
2022-07-07 18:14:48 -07:00
Doug Gregor
2e3aa67c02 [Constraint solver] Treat downgraded errors as "disfavored overloads".
This allows us to still maintain them in the score kind, but not treat
them as being as severe as an error requiring a fix.
2022-07-07 18:12:56 -07:00
Doug Gregor
c564698625 [Constraint solver] Improve modeling of fix behavior.
Rather than re-using `DiagnosticBehavior` to describe how a fix should
act, introduce `FixBehavior` to cover the differences between (e.g.)
always-as-awarning and downgrade-to-warning. While here, split the
`isWarning` predicate into two different predicates:

* `canApplySolution`: Whether we can still apply a solution when it
contains this particular fix.
* `affectsSolutionScore`: Whether

These two predicates are currently tied together, because that's the
existing behavior, but we don't necessarily want them to stay that way.
2022-07-07 12:17:11 -07:00
Doug Gregor
218a3f79e0 [Constraint solver] Downgrade/ignore concurrency issues more generally.
Instead of the `warning` Boolean threaded through the solver's
diagnostics, thread `DiagnosticBehavior` to be used as the behavior
limit. Use this for concurrency checking (specifically dropped
`@Sendable` and dropped global actors) so the solver gets more control
over these diagnostics.

This change restores the diagnostics to a usable state after the prior
change, which introduced extra noise. The only change from existing
beavior is that dropping a global actor from a function type is now
always a warning in Swift < 6. This is partly intentional, because
there are some places where dropping the global actor is well-formed.
2022-07-01 11:45:44 -07:00
Doug Gregor
248b72bbff Track the pre-adjusted "reference" type for declaration reference. 2022-06-30 17:00:32 -07:00
Pavel Yaskevich
a7824b20b2 Revert "[CSSimplify] Disfavor choices that have injected callAsFunction" 2022-06-30 09:51:17 -07:00
Holly Borla
0597b8db04 Merge pull request #59740 from hborla/remove-existential-opening-option
[LangOptions] Remove the option to enable/disable implicit existential opening.
2022-06-30 08:53:13 -07:00
Pavel Yaskevich
28e69c83d8 Merge pull request #59762 from xedin/double-cgfloat-conv-improvements
[TypeChecker] Adjust Double<->CGFloat conversion to always preserve its location
2022-06-29 09:07:58 -07:00
Pavel Yaskevich
3fd3ff0637 [TypeChecker] Adjust Double<->CGFloat conversion to always preserve its location
Unfortunately current approach of making a conversion independent of location
doesn't work when conversion is required for multiple arguments to the
same call because solver expects that either there are no Double<->CGFloat
conversions, or one of them has already been applied which is not the case.

The reason why locations weren't preserved in the first place is due to
how a solution is applied to AST - AST is mutated first and then, if there
are any conversions, they are applied to the already mutated version of
original AST. This creates a problem for Double<->CGFloat which depends
on an overload choice of injected call and it's impossible to find it based
on the mutated AST. But it turns out that this is only an issue in two
specific cases - conversions against contextual type and after optional injection.
This situations could be mitigated by dropping parts of the locator which are
unimportant for the Double<->CGFloat conversion - anchor in case of contextual
and `OptionalPayload` element(s) in case of optional injection.

Resolves: https://github.com/apple/swift/issues/59374
2022-06-28 13:47:47 -07:00
Pavel Yaskevich
14dab87684 [ConstraintSystem] Discount effects while filtering call overload sets
Before these changes filtering would not favor some members because their
function types have `escaping` bit set when argument function type never
has it.

Double/CGFloat implicit conversion is one of such cases where `CGFloat.init`
overloads have `escaping` bit, which leads to solver checking more overloads
then it should.

Note that the difference in effects such as `async` is going to be handled
by scoring and ranking after partial solution is produced, so overloads that
differ only in `async` or `throws` are still going to be solved for as part
of "favored" set.
2022-06-28 13:07:23 -07:00
Holly Borla
6e8a581b42 [LangOptions] Remove the option to enable/disable implicit existential
opening.
2022-06-27 18:03:20 -07:00
Pavel Yaskevich
65bc269244 Merge pull request #59432 from xedin/rdar-94959816
[ConstraintSystem] Allow injecting `callAsFunction` after defaulted arguments
2022-06-27 13:31:45 -07:00
Holly Borla
429488f6c9 [Sema] Use ExistentialType for Any and AnyObject. 2022-06-17 18:29:15 -07:00
Pavel Yaskevich
2750e69267 [CSSimplify] Inject .callAsFunction upon trailing closure mismatch
Detect that extraneous closure belongs to `.init` call on a callable
type and let the solver inject `.callAsFunction` before fixing it.

This prevents incorrect "extraneous trailing closure" diagnostic when
issue is located inside of the closure itself.
2022-06-14 11:41:27 -07:00
Pavel Yaskevich
ea4ac2c332 [CSSimplify] Fix trailing closure vs. defaulted parameter matching
If current parameter cannot accept a trailing closure, let's match
current closure argument to it only if there is absolutely no
possible other choice e.g. no other parameters (defaulted or not)
that could accept the closure via a less restrictive backward scan.
2022-06-14 11:41:19 -07:00
Pavel Yaskevich
7ba07ae3ba Merge pull request #59210 from xedin/conflicting-patterns-in-case
[Diagnostics] Diagnose conflicting pattern variables
2022-06-14 11:30:34 -07:00
Luciano Almeida
c9f10040f7 Merge pull request #59277 from LucianoPAlmeida/dictionary-array-literals
[Sema] Allow TreatArrayLiteralAsDictionary fix to handle literals with more than one element
2022-06-07 09:39:45 -03:00
Luciano Almeida
42cf1b73de [Sema] Adjusting repairArraLiteralUsedAsDictionary to also ignore OptToOpt promotions 2022-06-07 01:18:35 -03:00
Luciano Almeida
c6f00fae98 [Sema] Allow TreatArrayLiteralAsDictionary fix to handle literals with more than one element 2022-06-05 22:36:35 -03:00
Pavel Yaskevich
f15a0b16cb [CSFix] Detect conflicting pattern variables
The fix indentifies the conflicting variables and the expected
"join" type e.g. `case .a(let x), .b(let x)` where `a(Int)` and
`b(String)`.
2022-06-03 16:31:27 -07:00
Pavel Yaskevich
644a720ce6 Merge pull request #59003 from xedin/rdar-92177656
[TypeChecker] Rework type-checking of `for-in` statements
2022-06-02 14:58:56 -07:00
Pavel Yaskevich
27419446b6 Merge pull request #59064 from xedin/issue-59058
[CSSimplify] If function types mismatch on labels record a contextual…
2022-06-02 10:18:54 -07:00
Pavel Yaskevich
ef0523fe29 [ConstraintSystem] NFC: Remove obsolete ValueWitness constraint 2022-05-30 23:17:41 -07:00
Pavel Yaskevich
5f0dcb572b [ConstraintSystem] Implicitly open existential type of for-in sequence
This allows to use `for-in` statement to iterate over i.e. `any Collection`
and other existentials that conform to `Sequence` protocol.
2022-05-30 23:17:41 -07:00
Pavel Yaskevich
e9b06471a9 [CSSimplify] Don't record requirement failure if one of the sides is unresolved 2022-05-30 23:17:41 -07:00
Pavel Yaskevich
0989cd9a06 [CSSimplify] Ignore missing next member on $generator
The actual issue in this cases is that the type of a sequence
doesn't conform to `Sequence` protocol, which means that it's
impossible to deduce a base for `next` call.
2022-05-30 23:17:41 -07:00
Pavel Yaskevich
86165291aa [TypeChecker] Change the way for-in statement in type-checked
Instead of asking SILGen to build calls to `makeIterator` and
`$generator.next()`, let's synthesize and type-check them
together with the rest of for-in preamble. This greatly simplifies
interaction between Sema and SILGen for for-in statements.
2022-05-30 23:17:41 -07:00
Pavel Yaskevich
1be0eafb2c [CSSimplify] Ignore missing makeIterator member
The actual issue in this cases is that the type of a sequence
doesn't conform to `Sequence` protocol.
2022-05-30 23:17:41 -07:00
Doug Gregor
72c92892fd Introduce feature ForwardTrailingClosures for SE-0286.
Replace the "Swift version 6" checks for forward-scanning trailing
closure matching with checks for this new feature.
2022-05-30 08:32:56 -07:00
Doug Gregor
93fef0591b Merge pull request #59113 from DougGregor/enable-experimental-feature
Add -enable-experimental-feature X for experimental features.
2022-05-27 15:07:31 -07:00
Doug Gregor
333ae3f830 Add experimental feature for one-way closure parameters 2022-05-27 11:52:29 -07:00
Pavel Yaskevich
689e9e3861 Merge pull request #42559 from xedin/se-0352-as-any-coercion
[TypeChecker] SE-0352: Require coercion if result type contains existential(s) that would loose generic requirements
2022-05-27 09:44:31 -07:00
Pavel Yaskevich
8eba8907a2 [CSSimplify] Don't suppress existential opening if as any ... is in parens 2022-05-26 15:34:40 -07:00
Doug Gregor
6d82448dc8 Add -enable-experimental-feature X for experimental features.
Experimental features can only be enabled in non-production (+Asserts)
builds. They can be detected with `hasFeature` in the same manner as
"future" features.

The `-enable-experimental-feature X` flag will also look for future
features by that name, so that when an experimental feature becomes an
accepted future feature, it will still be enabled in the same manner.

Switch variadic generics over to this approach, eliminating the
specific LangOption for it.
2022-05-26 11:50:39 -07:00
Robert Widmann
c9ddfbc800 Merge pull request #59090 from CodaFi/unknown-unknowns
Handle ParamDecls and VarDecls in Argument Matching Diagnostics
2022-05-26 10:34:34 -07:00
Pavel Yaskevich
7d8af2d03a Merge pull request #58953 from xedin/fix-callAsFunction-perf
[CSSimplify] Disfavor choices that have injected `callAsFunction`
2022-05-26 09:51:33 -07:00
Pavel Yaskevich
7898b5a088 [CSSimplify] Disfavor choices that have injected callAsFunction
Ambiguities like:

```
struct S {
  init(v: Int) {}
  init(v: Int, _: () -> Void) {}

  func callAsFunction(_: () -> Void) {}
}

S(v: 42) {
}
```

Should always be resolved in favor of choice that doesn't require
injection of `.callAsFunction`, so let's try to avoid solving if
such an overload has already been found.
2022-05-26 00:47:34 -07:00
Pavel Yaskevich
0a5b3f0727 [TypeChecker] SE-0324: Extend Swift -> C pointer conversions to inout
Fixes an oversight where `inout` -> C pointer conversion wasn't covered
by implementation of new pointer conversion semantics proposed by SE-0324.

Resolves: rdar://92583588
2022-05-25 20:55:22 -07:00