Skip to content

Commit 7b90e77

Browse files
authored
Merge pull request #988 from swiftwasm/release/5.3
[pull] swiftwasm-release/5.3 from release/5.3
2 parents c1f4e09 + ae293ab commit 7b90e77

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/AST/ASTPrinter.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -2612,9 +2612,17 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
26122612
auto type = decl->getInterfaceType();
26132613
Printer << ": ";
26142614
TypeLoc tyLoc;
2615-
if (auto *repr = decl->getTypeReprOrParentPatternTypeRepr())
2615+
if (auto *repr = decl->getTypeReprOrParentPatternTypeRepr()) {
2616+
// Workaround for if-let statements. The parser creates a `OptionalTypeRepr`
2617+
// even though the user-written declared type for the if-let variable
2618+
// is non-optional. Get the non-optional type so we can print it correctly.
2619+
if (auto *optRepr = dyn_cast<OptionalTypeRepr>(repr)) {
2620+
if (type && !isa<OptionalType>(type.getPointer())) {
2621+
repr = optRepr->getBase();
2622+
}
2623+
}
26162624
tyLoc = TypeLoc(repr, type);
2617-
else
2625+
} else
26182626
tyLoc = TypeLoc::withoutLoc(type);
26192627

26202628
Printer.printDeclResultTypePre(decl, tyLoc);

lib/Parse/ParsePattern.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,10 @@ parseOptionalPatternTypeAnnotation(ParserResult<Pattern> result,
11491149

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let foo: Int? = 123
2+
if let bar: Int = foo {
3+
_ = bar
4+
}
5+
6+
// RUN: %sourcekitd-test -req=cursor -pos=2:8 %s -- %s | %FileCheck --check-prefix=CHECK-BAR %s
7+
// RUN: %sourcekitd-test -req=cursor -pos=3:7 %s -- %s | %FileCheck --check-prefix=CHECK-BAR %s
8+
// RUN: %sourcekitd-test -req=cursor -pos=1:5 %s -- %s | %FileCheck --check-prefix=CHECK-FOO %s
9+
10+
// CHECK-BAR: <Declaration>let bar: <Type usr="s:Si">Int</Type></Declaration>
11+
// 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>
12+
13+
// CHECK-FOO: <Declaration>let foo: <Type usr="s:Si">Int</Type>?</Declaration>
14+
// 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>

0 commit comments

Comments
 (0)