Commit Graph

2098 Commits

Author SHA1 Message Date
Pavel Yaskevich
e5d26627db [CSSimplify] Replace locatorEndsWith with isLastElement accessor 2022-08-31 12:12:16 -07:00
Slava Pestov
5c32f2136e AST: Introduce RequirementKind::SameCount 2022-08-23 11:12:00 -04:00
Slava Pestov
7a16b0275b AST: Allow one-element tuple types to be constructed
These will never appear in the source language, but can arise
after substitution when the original type is a tuple type with
a pack expansion type.

Two examples:
- original type: (Int, T...), substitution T := {}
- original type: (T...), substitution T := {Int}

We need to model these correctly to maintain invariants.

Callers that previously used to rely on TupleType::get()
returning a ParenType now explicitly check for the one-element
case instead.
2022-08-23 11:12:00 -04:00
Slava Pestov
e8dc19a70b Sema: Stop calling getAllConformances() on protocols 2022-08-23 00:03:37 -04:00
Pavel Yaskevich
4d039686fd [CSSimplify] Failure to bind type variable to invalid dependent member makes it a hole
If the failure is not reflected in constraint system it would let
the solver to form a _valid_ solution as if the constraint between
the type variable and the unresolved dependent member type never
existed.

Resolves: https://github.com/apple/swift/issues/60649
2022-08-19 11:39:40 -07:00
Amritpan Kaur
fd41a39294 [ConstraintSystem] Refactor solver state depth to its own function for easier indent editing in future. 2022-08-10 13:29:02 -07:00
Slava Pestov
11eb11833f Sema: Fix deep equality matching for parameterized protocol types
Fixes rdar://problem/98356057.
2022-08-09 23:45:39 -04:00
Pavel Yaskevich
b39302a585 Merge pull request #60203 from xedin/rdar-96997534
[CSGen] Allow `_` pattern to be a hole
2022-08-08 10:14:18 -07:00
Pavel Yaskevich
5ee5a22cf1 [TypeChecker] NFC: Add a dedicated method to get outermost attached wrapper
The outermost wrapper is the one at index `0` in the wrapper list
but it's easy for humans to make a reverse assumption since outermost
is the back of the list. Let's add a dedicated method to reduce error
probability of the property wrapper APIs.
2022-08-04 17:30:02 -07:00
Pavel Yaskevich
61a850dfc3 [CSFix] Improve missing .rawValue fix in ambiguity conditions 2022-08-03 15:50:33 -07:00
Pavel Yaskevich
ac89df5c12 [CSFix] Generalize a fix for unresolved pattern decl
The fix should support both named (i.e. `test(a)` and "any" patterns
i.e. `test(_)`.
2022-08-03 15:50:33 -07:00
Hamish Knight
9da53193da [AST] Remove ParameterTypeFlags from ParenType and TupleType
The last clients that relied on stashing parameter
type flags on these types are now gone.
2022-08-02 13:56:32 +01:00
Hamish Knight
6e51841d14 [AST] Enforce that composeTuple drops parameter flags
Callers may either assert that the parameter flags
are empty, or ask for them to be dropped.
2022-08-02 13:56:31 +01:00
Hamish Knight
1bf954c61f [CS] Better diagnose inout argument in tuple construction
Previously we relied on `TupleTypeElt::getType`
returning an `InOutType` to fail the tuple type
matching logic. Instead, add logic to reject any
inout arguments up-front with a more specific
diagnostic.

Also, while we're here, strip the `_const`
parameter flag, as it's not something that needs
to be considered for tuple construction.
2022-08-02 13:56:30 +01:00
Hamish Knight
4915513387 [CS] Filter out uncallable vars when simplifying applied overloads
This improves diagnostics as we can now consider
functions that don't line up exactly with the
argument list if no other viable candidates exist.
2022-07-26 12:51:53 +01:00
Hamish Knight
fe7c1f4b9b Refactor callable lookup
Move off `Type` based requests and onto `Decl`
based requests, utilizing name lookup's
`extractDirectlyReferencedNominalTypes` utility.
This allows us to better cache the results, and
avoids the need to guard against type variable
inputs when deciding whether or not to cache.
2022-07-25 22:03:55 +01:00
Hamish Knight
13fe88cc57 [AST] Rename isCallableNominalType -> isCallAsFunctionType
Make it explicit that we're checking for
`callAsFunction` instead of `dynamicallyCall`.
2022-07-25 22:03:55 +01:00
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