Commit Graph

2101 Commits

Author SHA1 Message Date
Xi Ge
03c76bd32d sema: diagnose passing a non-constant value into a constant parameter 2021-11-22 11:52:57 -08:00
Hamish Knight
237338b504 Merge pull request #40224 from hamishknight/super-tuple-shuffle 2021-11-18 09:50:09 +00:00
Robert Widmann
1faf7edbd6 Merge pull request #40193 from CodaFi/sequential-access-patterns
Model Sequence Archetypes
2021-11-17 13:19:31 -08:00
Hamish Knight
4aaec65780 [CS] Warn on mismatched tuple labels for subtyping
This is something that we'd like to fix to bring
in line with tuple conversion, so start warning on
cases where it occurs.
2021-11-17 17:06:21 +00:00
Hamish Knight
da36a2cb88 [CS] Restore a type variable for compatibility with rdar://85263844
Despite being otherwise disconnected from the
constraint system, it's possible for it to affect
how we type-check tuple matches in certain cases.

This is due to the fact that:
- It can have a lower type variable ID than an
opened generic parameter type, so becomes the
representative when merged with it. And because it
has a different locator, this can influence
binding prioritization.
- Tuple subtyping is broken, as it's currently a
*weaker* relationship than conversion.

Therefore, temporarily restore this bit of logic
for language versions < 6. If possible, we should
try and fix tuple subtying in Swift 6 mode to not
accept label mismatches, so that it's not more
permissive than tuple conversion.

rdar://85263844
2021-11-17 17:06:19 +00:00
Robert Widmann
e7e11df927 Model Sequence Archetypes 2021-11-16 11:38:57 -08:00
Pavel Yaskevich
83033198c3 [TypeChecker] Fix constraint solver to respect LeaveClosureBodyUnchecked flag 2021-11-15 16:42:05 -08:00
Pavel Yaskevich
3927f56dbd [ConstraintSystem] Warn about discarded expressions found in multi-statement closures 2021-11-15 16:42:04 -08:00
Pavel Yaskevich
c4b3f86a70 [Diagnostics] Don't assume that contextual type for optional chain is optional
While checking validity of the optional chain, let's not assume
that the contextual type associated with is optional because
enclosing expression could too be invalid.

Resolves: rdar://85166519
2021-11-09 09:42:45 -08:00
Pavel Yaskevich
0c70f5650f Merge pull request #39592 from Jumhyn/contextual-placeholder-followup
Remove default argument from getContextualType
2021-11-08 10:00:36 -08:00
Pavel Yaskevich
7f94bbf29b [AST] NFC: Drop Type from TypeBase::isCGFloatType
This makes naming consistent with other accessors e.g. `isDouble`
2021-11-03 18:09:14 -07:00
Pavel Yaskevich
9e3a0586c4 [CSSimplify] Detect and fix implicit Double <-> CGFloat conversion via optional chaining
Passing Double? to CGFloat? and vice versa is not supported but
there was one case which solver still allowed to get through -
optional chains, which is not fixed.
2021-11-03 14:42:13 -07:00
Pavel Yaskevich
f765390c62 [ConstraintSystem] Convert Fixes to a set vector to avoid duplicates 2021-10-22 10:09:09 -07:00
Pavel Yaskevich
d74e8c9407 [ConstraintSystem] Convert ConversionRestrictions to a map vector to avoid duplicates 2021-10-22 10:09:08 -07:00
Pavel Yaskevich
19c253bec2 [ConstraintSystem] Convert argumentMatchingChoices to a map vector to avoid duplicates 2021-10-22 10:09:08 -07:00
Pavel Yaskevich
e95e43d019 [ConstraintSystem] Convert ImplicitValueConversions to a map vector to avoid duplicates 2021-10-22 10:09:08 -07:00
Pavel Yaskevich
568c27f551 [ConstraintSystem] Convert Opened{Existential}Types to a map vector to avoid duplicates
Same reasons as with `DefaultConstraints` but also improves
code ergonomics around opened types.
2021-10-22 10:09:08 -07:00
Doug Gregor
0c9f9f7a43 Use a SmallSetVector to eliminate duplicates 2021-10-20 23:15:21 -07:00
Doug Gregor
45824befd2 Infer 'isolated' closure parameters from context.
When a closure is provided with a contextual type that has isolated
parameters, infer that the corresponding closure parameter is "isolated".

Fixes rdar://83732479.
2021-10-20 21:51:42 -07:00
Hamish Knight
287fa8e8de [CS] Refactor IUO handling
The current IUO design always forms a disjunction
at the overload reference, for both:

- An IUO property `T!`, forming `$T := T? or T`
- An IUO-returning function `() -> T!`, forming `$T := () -> T? or () -> T`

This is simple in concept, however it's suboptimal
for the latter case of IUO-returning functions for
a couple of reasons:

- The arguments cannot be matched independently of
  the disjunction
- There's some awkwardness when it comes e.g wrapping
  the overload type in an outer layer of optionality
  such as `(() -> T!)?`:
  - The binding logic has to "adjust" the correct
    reference type after forming the disjunction.
  - The applicable fn solving logic needs a special
    case to handle such functions.
- The CSApply logic needs various hacks such as
  ImplicitlyUnwrappedFunctionConversionExpr to
  make up for the fact that there's no function
  conversion for IUO functions, we can only force
  unwrap the function result.
  - This lead to various crashes in cases where
    we we'd fail to detect the expr and peephole
    the force unwrap.
  - This also lead to crashes where the solver
    would have a different view of the world than
    CSApply, as the former would consider an
    unwrapped IUO function to be of type `() -> T`
    whereas CSApply would correctly see the overload
    as being of type `() -> T?`.

To remedy these issues, IUO-returning functions no
longer have their disjunction formed at the overload
reference. Instead, a disjunction is formed when
matching result types for the applicable fn
constraint, using the callee locator to determine
if there's an IUO return to consider. CSApply then
consults the callee locator when finishing up
applies, and inserts the force unwraps as needed,
eliminating ImplicitlyUnwrappedFunctionConversionExpr.

This means that now all IUO disjunctions are of the
form `$T := T? or T`. This will hopefully allow a
further refactoring away from using disjunctions
and instead using type variable binding logic to
apply the correct unwrapping.

Fixes SR-10492.
2021-10-12 14:14:33 +01:00
Hamish Knight
aba30e7f72 Merge pull request #39543 from hamishknight/constructing-a-tier-list 2021-10-12 14:05:25 +01:00
Hamish Knight
b8e4c676c6 [CS] Remove function component constraints
FunctionInput relies on being able to represent
parameter lists as tuples, which won't be possible
once parameter flags are stripped from tuple types.
FunctionResult is reasonable, but is currently
unused.
2021-10-12 09:51:45 +01:00
Hamish Knight
fee31c69f0 [CS] Move constructor ranking rule into CSRanking
Previously we were introducing a type variable
to mark a constructor's parameter list as
`TVO_PrefersSubtypeBinding`. Unfortunately this
relies on representing the parameter list as a
tuple, which will no longer be properly supported
once param flags are removed from tuple types.

Move the logic into CSRanking such that we pick up
and compare the parameter lists when comparing
overload bindings. For now, this still relies on
comparing the parameter lists as tuples, as there's
some subtle tuple subtyping rules that could
potentially affect source compatibility here, but
at least we can explicitly strip the parameter
flags and localise the hack to CSRanking rather
than exposing it as a constraint.
2021-10-12 09:51:44 +01:00
Hamish Knight
a1abcee108 [CS] Introduce ConstructorMemberType locator elt
Use this new element to represent the overload type
for a constructor call, and have it store a bit
indicating whether the call is for a short-form
`X(...)` or self-delegating `self.init(...)` call.
2021-10-12 09:51:44 +01:00
LucianoAlmeida
6e072ba816 [Sema] Increase impact of conformance failure in generic arguments for operators 2021-10-11 21:47:44 -03:00
Luciano Almeida
ca8b660a1c Merge pull request #39648 from LucianoPAlmeida/SR-15281-cast
[SR-15281] [Sema] Couple of contextual mismatch and runtime cast diagnostic fixes
2021-10-10 08:03:24 -03:00
LucianoAlmeida
5424797e79 [Sema] Make constraint restriction to store TypeBase * instead of Type 2021-10-09 14:26:21 -03:00
LucianoAlmeida
4e689bba5d [NFC] Adjusting comments for clarity 2021-10-09 14:26:21 -03:00
LucianoAlmeida
8c424383a6 [Sema] Account for value to optional restriction when recording runtime cast warnings 2021-10-09 14:26:21 -03:00
Pavel Yaskevich
99a9828a03 [ConstraintSystem] Add a new contextual purpose - CTP_CaseStmt 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
7197fac8f5 [ConstraintSystem] Add a new contextual purpose - CTP_ForEachSequence
A contextual purpose for a sequence expression associated with
`for-in` statement, that decays into a `ConformsTo` constraint
to a `Sequence` or `AsyncSequence` protocol.

Note that CTP_ForEachSequence is almost identical to CTP_ForEachStmt
but the meaning of latter is overloaded, so to avoid breaking solution
targets I have decided to add a new purpose for now.
2021-10-08 10:08:02 -07:00
Pavel Yaskevich
55bae6150d [Constraint] Allow closure body elements to carry contextual information 2021-10-08 10:08:02 -07:00
Pavel Yaskevich
91eecdb6d0 [CSClosure] Initial support for multi-statement closures
Implement constraint generation for brace statements and
refactor closure contraint generation to accept multi-statement
closures.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
73dffb3125 [ConstraintSystem] Don't require a type variable per closure body element
Since each of the body elements is already going to have a type associated
with it there is no need to create yet another type variable.
2021-10-08 10:08:01 -07:00
Pavel Yaskevich
2b4691048e [ConstraintSystem] Add a skeleton of Conjunction constraint
It's similar to disjunction constraint but represents an "and"
relationship between its elements instead of "or", so all of the
elements have to produce a solution for conjunction constraint
to be considered solved successfully.
2021-10-08 10:08:00 -07:00
Pavel Yaskevich
eb8eabfba7 [ConstraintSystem] Add a skeleton of ClosureBodyElement constraint 2021-10-08 10:08:00 -07:00
Pavel Yaskevich
1c0a306c75 Merge pull request #37956 from xedin/implicit-swift-to-c-ptr-conversions
[TypeChecker] Implement limited set of conversions between Swift and C pointers
2021-10-06 14:35:22 -07:00
Frederick Kellison-Linn
b433724735 Remove default argument from getContextualType 2021-10-05 17:04:54 -04:00
Pavel Yaskevich
ed45116be8 Merge pull request #39458 from xedin/result-builder-labeled-tuple-pattern
[ResultBuilders] A couple pattern matching fixes to make it consistent with `TypeCheckPattern`
2021-10-04 09:29:48 -07:00
Holly Borla
9bf9de8ca6 Merge pull request #39188 from amritpan/projected-value-labels
[CSSimplify] Fix a bug where the omitted projected value label was missing as an acceptable parameter label for property wrappers
2021-09-28 16:57:28 -07:00
Pavel Yaskevich
7a854ee1cc [CSSimplify] Make solver behavior consistent with TypeCheckPattern for paren patterns
A single paren pattern becomes a labeled tuple pattern
e.g. `case .test(let value):` should be able to match
`case test(result: Int)`. Note that it also means that:
`cast test(result: (String, Int))` would be matched against
e.g. `case .test((let x, let y))` but that fails during
pattern coercion (behavior consistent with what happens in
`TypeCheckPattern`).
2021-09-25 11:42:21 -07:00
Pavel Yaskevich
a9fdb7ad66 [CSSimplify] Detect and diagnose Swift -> C pointer conversion failures
Detect and diagnose situations when Swift -> C pointer conversion
is unsupported due to method not being imported from C header.
2021-09-20 17:22:26 -07:00
Pavel Yaskevich
25762c25ee [CSSimplify] Make sure that Swift -> C pointer conversion always waits for optionals
Wait for a value-to-optional promotion or optional-to-optional conversion
to happen before attempting Swift -> C pointer conversion.
2021-09-20 17:22:26 -07:00
Pavel Yaskevich
f53bcea60c [CSFix] Add a tailored fix for invalid Swift -> C pointer conversions
This is a skeleton of a fix that would be used to diagnose situations
when Swift -> C pointer conversion was attempted on a Swift function.
2021-09-20 17:22:26 -07:00
Pavel Yaskevich
5cb8604b60 [ConstraintSystem] De-prioritize solutions with Swift -> C pointer conversions
Just like other implicit conversions - always prefer solutions with
the lowest possible number of them.
2021-09-20 17:22:26 -07:00
Pavel Yaskevich
f368e5a7b6 [ConstraintSystem] NFC: Extract Swift -> C pointer conversion simplification into a separate method 2021-09-20 17:22:26 -07:00
Pavel Yaskevich
d95e92c92c [CSSimplify] Allow implicit Swift -> C pointer conversion within optional types
Support Swift -> C pointer conversions even if argument required
a value to optional promotion or is optional.
2021-09-20 17:22:25 -07:00
Pavel Yaskevich
9e7a670295 [ConstraintSystem] Implement new Swift -> C pointer conversions
Allow following conversions in argument positions
(applies only to call to imported C/ObjC declarations):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> <-> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
2021-09-20 17:22:25 -07:00
Pavel Yaskevich
3a41f7af5f [ConstraintSystem] Add a new conversion - Swift to C pointers
Following pointer conversions are supported in argument positions (when referencing C/ObjC functions):

- Unsafe[Mutable]RawPointer -> Unsafe[Mutable]Pointer<[U]Int>
- Unsafe[Mutable]Pointer<Int{8, 16, ...}> -> Unsafe[Mutable]Pointer<UInt{8, 16, ...}>
2021-09-20 17:22:25 -07:00
Pavel Yaskevich
a061a966d1 Merge pull request #39251 from xedin/classof-for-fixes
[CSFix] Add support for `classof` for all fixes
2021-09-20 14:33:06 -07:00