Skip to content

[ASTPrinter] Fix issue where printing if-let variables shows their type as optional #31742

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
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
12 changes: 10 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2612,9 +2612,17 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
auto type = decl->getInterfaceType();
Printer << ": ";
TypeLoc tyLoc;
if (auto *repr = decl->getTypeReprOrParentPatternTypeRepr())
if (auto *repr = decl->getTypeReprOrParentPatternTypeRepr()) {
// Workaround for if-let statements. The parser creates a `OptionalTypeRepr`
// even though the user-written declared type for the if-let variable
// is non-optional. Get the non-optional type so we can print it correctly.
if (auto *optRepr = dyn_cast<OptionalTypeRepr>(repr)) {
if (type && !isa<OptionalType>(type.getPointer())) {
repr = optRepr->getBase();
}
}
tyLoc = TypeLoc(repr, type);
else
} else
tyLoc = TypeLoc::withoutLoc(type);

Printer.printDeclResultTypePre(decl, tyLoc);
Expand Down
4 changes: 4 additions & 0 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,10 @@ parseOptionalPatternTypeAnnotation(ParserResult<Pattern> result,

// In an if-let, the actual type of the expression is Optional of whatever
// was written.
// FIXME: This is not good, `TypeRepr`s are supposed to represent what the
// user actually wrote in source (that's why they don't have any `isImplicit`
// bit). This synthesized `OptionalTypeRepr` leads to workarounds in other
// parts where we want to reason about the types as perceived by the user.
if (isOptional)
repr = new (Context) OptionalTypeRepr(repr, SourceLoc());

Expand Down
14 changes: 14 additions & 0 deletions test/SourceKit/CursorInfo/sr-12631-if-let-type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let foo: Int? = 123
if let bar: Int = foo {
_ = bar
}

// RUN: %sourcekitd-test -req=cursor -pos=2:8 %s -- %s | %FileCheck --check-prefix=CHECK-BAR %s
// RUN: %sourcekitd-test -req=cursor -pos=3:7 %s -- %s | %FileCheck --check-prefix=CHECK-BAR %s
// RUN: %sourcekitd-test -req=cursor -pos=1:5 %s -- %s | %FileCheck --check-prefix=CHECK-FOO %s

// CHECK-BAR: <Declaration>let bar: <Type usr="s:Si">Int</Type></Declaration>
// CHECK-BAR-NEXT: <decl.var.local><syntaxtype.keyword>let</syntaxtype.keyword> <decl.name>bar</decl.name>: <decl.var.type><ref.struct usr="s:Si">Int</ref.struct></decl.var.type></decl.var.local>

// CHECK-FOO: <Declaration>let foo: <Type usr="s:Si">Int</Type>?</Declaration>
// CHECK-FOO-NEXT: <decl.var.global><syntaxtype.keyword>let</syntaxtype.keyword> <decl.name>foo</decl.name>: <decl.var.type><ref.struct usr="s:Si">Int</ref.struct>?</decl.var.type></decl.var.global>