mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
To compute the expected type of a call pattern (which is the return type of the function if that call pattern is being used), we called `getTypeForCompletion` for the entire call, in the same way that we do for the code completion token. However, this pattern does not generally work. For the code completion token it worked because the code completion expression doesn’t have an inherent type and it inherits the type solely from its context. Calls, however, have an inherent return type and that type gets assigned as the `typeForCompletion`. Implement targeted checks for the two most common cases where an expected type exists: If the call that we suggest call patterns for is itself an argument to another function or if it is used in a place that has a contextual type in the constraint system (eg. a variable binding or a `return` statement). This means that we no longer return `Convertible` for call patterns in some more complex scenarios. But given that this information was computed based on incorrect results and that in those cases all call patterns had a `Convertible` type relation, I think that’s acceptable. Fixing this would require recording more information in the constraints system, which is out-of-scope for now.
49 lines
1.8 KiB
Swift
49 lines
1.8 KiB
Swift
// RUN: %batch-code-completion
|
|
|
|
enum DragState {
|
|
case inactive
|
|
case dragging(translationX: Int, translationY: Int)
|
|
case defaulted(x: Int = 2, y: Int = 3, z: Int, extra: Int)
|
|
}
|
|
|
|
func testInit() {
|
|
var state = DragState.inactive
|
|
state = .dragging(#^SIGNATURE^#)
|
|
// SIGNATURE: Begin completions, 1 item
|
|
// SIGNATURE: Pattern/CurrNominal/Flair[ArgLabels]: ['(']{#translationX: Int#}, {#translationY: Int#}[')'][#DragState#];
|
|
|
|
state = .dragging(translationX: 2, #^ARGUMENT^#)
|
|
// ARGUMENT: Begin completions, 1 item
|
|
// ARGUMENT: Pattern/Local/Flair[ArgLabels]: {#translationY: Int#}[#Int#];
|
|
|
|
state = .defaulted(#^DEFAULTED^#)
|
|
// DEFAULTED: Begin completions, 1 items
|
|
// DEFAULTED: Pattern/CurrNominal/Flair[ArgLabels]: ['(']{#x: Int#}, {#y: Int#}, {#z: Int#}, {#extra: Int#}[')'][#DragState#];
|
|
|
|
state = .defaulted(x: 1, #^DEFAULTEDARG^#)
|
|
state = .defaulted(x: "Wrong type", #^ERRORTYPE?check=DEFAULTEDARG^#)
|
|
// DEFAULTEDARG: Begin completions, 2 items
|
|
// DEFAULTEDARG: Pattern/Local/Flair[ArgLabels]: {#y: Int#}[#Int#];
|
|
// DEFAULTEDARG: Pattern/Local/Flair[ArgLabels]: {#z: Int#}[#Int#];
|
|
|
|
state = .defaulted(wrongLabel: 2, #^ERRORARG^#)
|
|
// ERRORARG: Begin completions, 3 items
|
|
// ERRORARG: Pattern/Local/Flair[ArgLabels]: {#x: Int#}[#Int#];
|
|
// ERRORARG: Pattern/Local/Flair[ArgLabels]: {#y: Int#}[#Int#];
|
|
// ERRORARG: Pattern/Local/Flair[ArgLabels]: {#z: Int#}[#Int#];
|
|
}
|
|
|
|
func testMatch() {
|
|
var state = DragState.inactive
|
|
let localInt = 42
|
|
switch state {
|
|
case .dragging(translationX: 2, #^MATCH_ARGY^#):
|
|
// FIXME: This should have an identical type relation
|
|
// MATCH_ARGY: Decl[LocalVar]/Local/TypeRelation[Convertible]: localInt[#Int#]; name=localInt
|
|
// FIXME: We should offer 'translationY:' (without any flair since it's optional), `let translationY`, and `_`
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
}
|