Skip to content

[SourceKit] Determine correctly whether typed variables in if/guard/while-let-statements have explicit type annotations #38084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5806,11 +5806,24 @@ void VarDecl::setNamingPattern(NamedPattern *Pat) {
}

TypeRepr *VarDecl::getTypeReprOrParentPatternTypeRepr() const {
if (auto *param = dyn_cast<ParamDecl>(this))
return param->getTypeRepr();

if (auto *parentPattern = dyn_cast_or_null<TypedPattern>(getParentPattern()))
return parentPattern->getTypeRepr();
if (auto *Param = dyn_cast<ParamDecl>(this))
return Param->getTypeRepr();

auto *ParentPattern = getParentPattern();

if (auto *TyPattern = dyn_cast_or_null<TypedPattern>(ParentPattern))
return TyPattern->getTypeRepr();

// Handle typed if/guard/while-let as a special case (e.g. `if let x: Int
// = Optional.some(4)`), since the `TypedPattern` is not the top-level
// pattern here - instead it is an implicit `OptionalSomePattern`
if (auto *SomePattern =
dyn_cast_or_null<OptionalSomePattern>(ParentPattern)) {
if (auto *TyPattern =
dyn_cast<TypedPattern>(SomePattern->getSubPattern())) {
return TyPattern->getTypeRepr();
}
}

return nullptr;
}
Expand Down
21 changes: 21 additions & 0 deletions test/SourceKit/VariableType/case.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
if case let x? = Optional.some(5) {}
if case let y: Int? = Optional.some(5) {}
guard case let z: String? = Optional.some("a") else { fatalError() }
while case let w: String? = Optional.some("b") {}

enum Pair<U, V> {
case pair(U, V)
}

switch Pair.pair(Optional.some(0), "test") {
case .pair(let u, var v):
break
}

// RUN: %sourcekitd-test -req=collect-var-type %s -- %s | %FileCheck %s
// CHECK: (1:13, 1:14): Int (explicit type: 0)
// CHECK: (2:13, 2:14): Int? (explicit type: 1)
// CHECK: (3:16, 3:17): String? (explicit type: 1)
// CHECK: (4:16, 4:17): String? (explicit type: 1)
// CHECK: (11:16, 11:17): Int? (explicit type: 0)
// CHECK: (11:23, 11:24): String (explicit type: 0)
17 changes: 17 additions & 0 deletions test/SourceKit/VariableType/if-let.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
guard var opt: Int = .some(23) else { fatalError() }

func foo() {
guard let x = .some("abc") else { return }
guard let y: String = .some("def") else { return }

if let z: Int = .some(4) {}

while var w: Int = .some(5) {}
}

// RUN: %sourcekitd-test -req=collect-var-type %s -- %s | %FileCheck %s
// CHECK: (1:11, 1:14): Int (explicit type: 1)
// CHECK: (4:13, 4:14): String (explicit type: 0)
// CHECK: (5:13, 5:14): String (explicit type: 1)
// CHECK: (7:10, 7:11): Int (explicit type: 1)
// CHECK: (9:13, 9:14): Int (explicit type: 1)