Commit Graph

733 Commits

Author SHA1 Message Date
Alex Hoppen
fd8e34913a Merge pull request #36551 from ahoppen/pr/internal-labels-in-closures
[CodeComplete] Default parameter names of completed closure to internal names
2021-04-06 15:30:26 +02:00
Alex Hoppen
865e80f9c4 [CodeComplete] Default parameter names of completed closure to internal names
If have a function that takes a trailing closure as follows
```
func sort(callback: (_ left: Int, _ right: Int) -> Bool) {}
```
completing a call to `sort` and expanding the trailing closure results in
```
sort { <#Int#>, <#Int#> in
  <#code#>
}
```

We should be doing a better job here and defaulting the trailing closure's to the internal names specified in the function signature. I.e. the final result should be
```
sort { left, right in
  <#code#>
}
```

This commit does exactly that.

Firstly, it keeps track of the closure's internal names (as specified in the declaration of `sort`) in the closure's type through a new `InternalLabel` property in `AnyFunctionType::Param`. Once the type containing the parameter gets canonicalized, the internal label is dropped.

Secondly, it adds a new option to `ASTPrinter` to always try and print parameter labels. With this option set to true, it will always print external paramter labels and, if they are present, print the internal parameter label as `_ <internalLabel>`.

Finally, we can use this new printing mode to print the trailing closure’s type as
```
<#T##callback: (Int, Int) -> Bool##(_ left: Int, _ right: Int) -> Bool#>
```

This is already correctly expanded by code-expand to the desired result. I also added a test case for that behaviour.
2021-04-01 19:14:19 +02:00
Pavel Yaskevich
f00c578761 Merge pull request #34401 from xedin/implicit-cgfloat-conversion
[DNM][TypeChecker] Implement Double <-> CGFloat implicit conversion
2021-03-31 10:20:28 -07:00
Pavel Yaskevich
a51e3da285 [CSGen] Avoid failing due to invalid explicit closure parameters
If closure parameter has an explicit type, type resolution
would diagnose the issue and cache the resulting error type for
future use. Invalid types currently fail constraint generation,
which doesn't play well with result builders because constraint
generation for their bodies happens during solving.

Let's handle invalid parameters gracefully, replace them with
placeholders and let constraint generation proceed.

Resolves: rdar://75409111
2021-03-26 10:38:18 -07:00
Holly Borla
6f4b62a93c Merge pull request #36568 from hborla/property-wrapper-constraint
[ConstraintSystem] Add a property-wrapper constraint to allow for inference of implicit property wrappers
2021-03-24 17:13:51 -07:00
Holly Borla
76c4c3dc3f [Diagnostics] Add back a dedicated fix/diagnostic for using an invalid
type as a property wrapper.
2021-03-24 11:24:21 -07:00
Holly Borla
f05589f6c4 [Diagnostics] Improve diagnostics for passing an invalid projected
value argument.
2021-03-24 11:24:21 -07:00
Pavel Yaskevich
d7e0971764 Merge pull request #36531 from LucianoPAlmeida/SR-14096-kp-as-fn
[Sema][SR-14096] Change conditions in which key path component mismatch fix are diagnosed
2021-03-22 10:42:08 -07:00
Luciano Almeida
0bca6ce73c [Sema] Do not skip key path last component/value equal constraint failure fix 2021-03-21 18:29:47 -03:00
Doug Gregor
52096a640e SE-0302: Rename ConcurrentValue/@concurrent to Sendable/@Sendable. 2021-03-18 23:48:21 -07:00
Pavel Yaskevich
1dd914a651 Merge pull request #36445 from xedin/rdar-75367157
[Diagnostics] Check whether missing conformance could be fixed by using `.rawValue`
2021-03-17 15:24:33 -07:00
Doug Gregor
d6fc1fd60f Implement conversions for global-actor-qualified functions.
One can convert from a function value without a global actor to a
similar function type that does have a global actor, but one cannot
remove or change a global actor on a function type.
2021-03-17 00:51:26 -07:00
Pavel Yaskevich
8c6017687a [ConstraintSystem] Limit new implicit conversion to only Double <-> CGFloat 2021-03-17 00:18:10 -07:00
Pavel Yaskevich
2b935bab7c [ConstraintSystem] Add conversion restriction to cover implicit conversion from/to CGFloat 2021-03-17 00:18:08 -07:00
Pavel Yaskevich
45c73b30fd [Diagnostics] Even though missing conformance could be fixed by .rawValue still diagnose it as conformance issue
Conformance requirement failure is a more descriptive error,
augment it with a note about missing `.rawValue`.
2021-03-17 00:14:51 -07:00
Pavel Yaskevich
e92c6eb68c [Diagnostics] NFC: Use non-initializer diagnostic when raw representable type doesn't conform but .rawValue does 2021-03-15 17:29:31 -07:00
Pavel Yaskevich
2a9903dfe4 [Diagnostics] Check whether missing conformance could be fixed by using .rawValue
A lot of operators (and most likely regular functions a well) have
overloads that accept i.e. a generic parameter that conforms to
`StringProtocol`, so the best fix in situations when argument is
a raw representable type would be to check whether `.rawValue`
conforms to the expected protocol and use it if so.

Resolves rdar://problem/75367157
2021-03-15 16:53:45 -07:00
Pavel Yaskevich
fb89d11931 [ConstraintLocator] NFC: Adjust name of isForKeyPathComponent to clarify its meaning
New name is `isInKeyPathComponent` since it's implemeneted as a
check for presence of `KeyPathComponent` element in the locator
that covers every sub-element contained in a key path component
as well.
2021-03-12 09:58:54 -08:00
Holly Borla
db387273f6 [ConstraintSystem] Don't allow dollar prefixes in argument labels unless
the parameter has an attached property wrapper.
2021-02-25 18:35:14 -08:00
Holly Borla
133dec0e05 [ConstraintSystem] Implement implicit property wrapper attribute
inference for closure parameters.
2021-02-25 18:35:14 -08:00
Holly Borla
522eedcfd1 [ConstraintSystem] Emit an error message when trying to pass a property
wrapper projection argument to a wrapped parameter with arguments in the
wrapper attribute.
2021-02-25 18:35:14 -08:00
Holly Borla
ea3fe03c98 [Property Wrappers] Add a constraint fix and diagnostic for the case
where the programmer tries to pass a projection argument to a wrapped
parameter without 'var projectedValue'.
2021-02-25 18:35:14 -08:00
Pavel Yaskevich
cf5e1fff8d [Diagnostics] Add a tailored diagnostic for contextual lookup on protocols without Self requirement 2021-02-23 11:33:11 -08:00
Pavel Yaskevich
2d8f002299 [CSDiagnostics] Adjacent diagnostics for the new rules of static member lookup in generic context 2021-02-23 11:33:11 -08:00
Pavel Yaskevich
e548366bb5 [TypeChecker] Add tests for static member refs on protocol metatypes feature 2021-02-23 11:33:11 -08:00
Pavel Yaskevich
feb8684dac [Diagnostics] Add a clarifying note about result type for invalid member ref diagnostic 2021-02-23 11:33:10 -08:00
Pavel Yaskevich
03d869ad8b [Diagnostics] Extract invalid result type from overload choice
Instead of threading it through the constraint system all the way
to a diagnostic, let's extract it from overload choice instead.
2021-02-23 11:33:10 -08:00
Pavel Yaskevich
9231acf202 [Diagnostics] Implement a tailored diagnostic for invalid static member refs on protocol metatypes 2021-02-23 11:32:24 -08:00
Frederick Kellison-Linn
e4ea1678dc Rename HoleType to PlaceholderType
HoleType basically served the same purpose as PlaceholderType. This commit unifies the two.
2021-02-16 22:59:19 -05:00
Luciano Almeida
d55eed46e8 [Sema][CSApply] Moving some checked cast diagnostics to a fix format (#34980)
* [Sema] Implementing is runtime check always true diagnostic as a fix

* [AST] Implement getWithoutThrows on function type

* [CSSimplify] Detect that checked cast types conversion is always true and record warning fix

* [test] Some additional test cases for SR-13789

* [Sema] Fixing typo on fix name

* [Sema] Move and adjust the ConditionalCast diagnostics to the fix format

* [Sema] Remove some checked cast diagnostics from check constraints and move to fix

* [Sema] Renaming checked cast coercible types fix

* [Sema] Some adjustments and rewrite on the logic for downcast record fix

* [Sema] Move logic of runtime function type to AllowUnsupportedRuntimeCheckedCast::attempt

* [Sema] Abstract checked cast fix logic to static function and minor adjustments

* [Sema] Renamings from review
2021-02-10 08:31:06 -03:00
Doug Gregor
ba8819eb58 [Concurrent] Introduce concurrent function types.
Introduce `@concurrent` attribute on function types, including:
* Parsing as a type attribute
* (De-/re-/)mangling for concurrent function types
* Implicit conversion from @concurrent to non-@concurrent
- (De-)serialization for concurrent function types
- AST printing and dumping support
2021-01-27 14:22:32 -08:00
Pavel Yaskevich
e086896f24 [Diagnostics] Avoid adding comma when reordering with last argument 2021-01-19 13:37:43 -08:00
Pavel Yaskevich
09f3461e3d [Diagnostics] Adjust OoO fix-it intervals to account for replacement of first argument
Current logic fix-it didn't account for the first argument being re-ordered.
The start position for the first argument should always be the next token
after left paren denoting a start of the argument list.

Resolves: rdar://problem/70764991
2021-01-19 13:37:32 -08:00
Pavel Yaskevich
5f20eb661e [Diagnostics] Adjust requirement failures to retrieve conformance information from locator
Conditional conformances previously relied on `CheckedConformances`
cache to retrieve information about conditional requirements from
base conformance, but that is not really necessary anymore since
`ConformanceRequirement` element could provide all required info.
2021-01-08 12:55:14 -08:00
Pavel Yaskevich
f0ff68e707 [Diagnostics] Special case requirement failures related to return statement/expression
Make sure that "affected" declaration is recognized as the one
to which result type is attached to if the requirement failure
is originated in contextual type of `return` statement/expression.

Resolves: SR-13992
Resolves: rdar://72819046
Resolves: rdar://57789606
2021-01-06 12:47:37 -08:00
Pavel Yaskevich
4917d4314b [ConstraintSystem] Use new ConformanceRequirement to record checked conformances and conditional requirements 2021-01-06 11:01:35 -08:00
Pavel Yaskevich
ec31d77fa6 [Diagnostics] Allow integer cast fix-its for assignment mismatches 2020-12-11 20:49:54 -08:00
Luciano Almeida
c8ea08df78 [Sema] Creating ambiguous base for none member warning diagnostic as a fix 2020-12-05 22:48:09 -03:00
Doug Gregor
50cdddaf95 [Concurrency] Enable implicit conversion from synchronous -> asynchronous. 2020-12-03 12:44:21 -08:00
Pavel Yaskevich
85181360b1 Merge pull request #34810 from xedin/rdar-66709164
[ConstraintSystem] Detect and diagnose extraneous return(s) in result build body
2020-12-02 15:51:10 -08:00
Luciano Almeida
ac65e6fcc4 [Sema] Detect if we should use a conditional binding when recording the CoerceToCheckedCast fix 2020-12-01 09:03:17 -03:00
Luciano Almeida
7df08f808d [Sema][test] Add SR-13899 test cases 2020-11-28 18:20:37 -03:00
Luciano Almeida
1479c0001f [Sema] Detach did you mean from error missing force downcast diagnostic moving to note 2020-11-28 18:16:07 -03:00
Pavel Yaskevich
03ede7427c [Diagnostics] Diagnose use of one or more return statements in a result builder body 2020-11-19 17:21:00 -08:00
Nathan Hawes
edbbefce91 [CodeCompletion][Sema] Add fix to treat empty or single-element array literals as dictionaries when used as such
In the single-element case, it is treated as the dictionary key.

func takesDict(_ x: [Int: String]) {}
takesDict([]) // diagnose with fixit to add missing ':'
takesDict([1]) // diagnose with fixit to add missing ': <#value#>'
takesDict([foo.<complete>]) // prioritise Int members in completion results -
                            // the user just hasn't written the value yet.

The above previously failed with a generic mismatch error in normal type
checking (due to the literal being parsed as an array literal) and code
completion could not pick up the expected type from the context.
2020-11-11 11:48:39 -08:00
Pavel Yaskevich
409aa35312 [Diagnostic] Add a diagnostic for invalid declaration refs
If AST node is a reference to an invalid declaration let's
diagnose it as such unless some errors have been emitted
(invalid declaration itself should have a diagnostic
describing a reason why it's invalid).
2020-11-04 12:15:45 -08:00
Pavel Yaskevich
9384813b49 [Diagnostics] Split "only concrete types conform to protocols" into a separate note 2020-10-27 14:52:23 -07:00
Pavel Yaskevich
1683fb91b6 [ConstraintSystem] Remove RValueAdjustment locator element
It was used for unresolved member and `.dynamicType` references
as well as a couple of other places, but now unresolved member
references no longer need that due to new implicit "chain result"
AST node and other places could use more precise locators
e.g. new `.dynamicType` locator or `sequence element` for `for in`
loops.
2020-10-26 00:40:45 -07:00
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00