Skip to content

Commit edfc2ad

Browse files
authored
Merge pull request #3477 from swiftwasm/main
[pull] swiftwasm from main
2 parents 9a7c0d0 + fc6f9e0 commit edfc2ad

File tree

8 files changed

+75
-49
lines changed

8 files changed

+75
-49
lines changed

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,9 @@ static parseJsonEmit(SDKContext &Ctx, StringRef FileName) {
21652165

21662166
// Load the input file.
21672167
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
2168-
vfs::getFileOrSTDIN(*Ctx.getSourceMgr().getFileSystem(), FileName);
2168+
vfs::getFileOrSTDIN(*Ctx.getSourceMgr().getFileSystem(), FileName,
2169+
/*FileSize*/-1, /*RequiresNullTerminator*/true,
2170+
/*IsVolatile*/false, /*RetryCount*/30);
21692171
if (!FileBufOrErr) {
21702172
llvm_unreachable("Failed to read JSON file");
21712173
}

lib/IDE/Refactoring.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6137,26 +6137,30 @@ class AsyncConverter : private SourceEntityWalker {
61376137
return OutputStr;
61386138
}
61396139

6140+
/// Retrieves the SourceRange of the preceding comment, or an invalid range if
6141+
/// there is no preceding comment.
6142+
CharSourceRange getPrecedingCommentRange(SourceLoc Loc) {
6143+
auto Tokens = SF->getAllTokens();
6144+
auto TokenIter = token_lower_bound(Tokens, Loc);
6145+
if (TokenIter == Tokens.end() || !TokenIter->hasComment())
6146+
return CharSourceRange();
6147+
return TokenIter->getCommentRange();
6148+
}
6149+
61406150
/// Retrieves the location for the start of a comment attached to the token
61416151
/// at the provided location, or the location itself if there is no comment.
61426152
SourceLoc getLocIncludingPrecedingComment(SourceLoc Loc) {
6143-
auto Tokens = SF->getAllTokens();
6144-
auto TokenIter = token_lower_bound(Tokens, Loc);
6145-
if (TokenIter != Tokens.end() && TokenIter->hasComment())
6146-
return TokenIter->getCommentStart();
6147-
return Loc;
6153+
auto CommentRange = getPrecedingCommentRange(Loc);
6154+
if (CommentRange.isInvalid())
6155+
return Loc;
6156+
return CommentRange.getStart();
61486157
}
61496158

6150-
/// If the provided SourceLoc has a preceding comment, print it out. Returns
6151-
/// true if a comment was printed, false otherwise.
6152-
bool printCommentIfNeeded(SourceLoc Loc, bool AddNewline = false) {
6153-
auto PrecedingLoc = getLocIncludingPrecedingComment(Loc);
6154-
if (Loc == PrecedingLoc)
6155-
return false;
6156-
if (AddNewline)
6157-
OS << "\n";
6158-
OS << CharSourceRange(SM, PrecedingLoc, Loc).str();
6159-
return true;
6159+
/// If the provided SourceLoc has a preceding comment, print it out.
6160+
void printCommentIfNeeded(SourceLoc Loc) {
6161+
auto CommentRange = getPrecedingCommentRange(Loc);
6162+
if (CommentRange.isValid())
6163+
OS << "\n" << CommentRange.str();
61606164
}
61616165

61626166
void convertNodes(const NodesToPrint &ToPrint) {
@@ -6171,8 +6175,6 @@ class AsyncConverter : private SourceEntityWalker {
61716175

61726176
// First print the nodes we've been asked to print.
61736177
for (auto Node : ToPrint.getNodes()) {
6174-
OS << "\n";
6175-
61766178
// If we need to print comments, do so now.
61776179
while (!CommentLocs.empty()) {
61786180
auto CommentLoc = CommentLocs.back().getOpaquePointerValue();
@@ -6187,16 +6189,13 @@ class AsyncConverter : private SourceEntityWalker {
61876189

61886190
printCommentIfNeeded(CommentLocs.pop_back_val());
61896191
}
6192+
OS << "\n";
61906193
convertNode(Node);
61916194
}
61926195

61936196
// We're done printing nodes. Make sure to output the remaining comments.
6194-
bool HasPrintedComment = false;
6195-
while (!CommentLocs.empty()) {
6196-
HasPrintedComment |=
6197-
printCommentIfNeeded(CommentLocs.pop_back_val(),
6198-
/*AddNewline*/ !HasPrintedComment);
6199-
}
6197+
while (!CommentLocs.empty())
6198+
printCommentIfNeeded(CommentLocs.pop_back_val());
62006199
}
62016200

62026201
void convertNode(ASTNode Node, SourceLoc StartOverride = {},

lib/Sema/CSGen.cpp

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,38 +2191,54 @@ namespace {
21912191
}
21922192
}
21932193

2194-
if (!varType)
2194+
if (!varType) {
21952195
varType = CS.createTypeVariable(CS.getConstraintLocator(locator),
21962196
TVO_CanBindToNoEscape);
21972197

2198+
// If this is either a `weak` declaration or capture e.g.
2199+
// `weak var ...` or `[weak self]`. Let's wrap type variable
2200+
// into an optional.
2201+
if (optionality == ReferenceOwnershipOptionality::Required)
2202+
varType = TypeChecker::getOptionalType(var->getLoc(), varType);
2203+
}
2204+
21982205
// When we are supposed to bind pattern variables, create a fresh
21992206
// type variable and a one-way constraint to assign it to either the
22002207
// deduced type or the externally-imposed type.
22012208
Type oneWayVarType;
22022209
if (bindPatternVarsOneWay) {
22032210
oneWayVarType = CS.createTypeVariable(
22042211
CS.getConstraintLocator(locator), TVO_CanBindToNoEscape);
2205-
CS.addConstraint(
2206-
ConstraintKind::OneWayEqual, oneWayVarType,
2207-
externalPatternType ? externalPatternType : varType, locator);
2208-
}
2209-
2210-
// If there is an externally-imposed type.
22112212

2212-
switch (optionality) {
2213-
case ReferenceOwnershipOptionality::Required:
2214-
varType = TypeChecker::getOptionalType(var->getLoc(), varType);
2215-
assert(!varType->hasError());
2213+
// If there is an externally-imposed pattern type and the
2214+
// binding/capture is marked as `weak`, let's make sure
2215+
// that the imposed type is optional.
2216+
//
2217+
// Note that there is no need to check `varType` since
2218+
// it's only "externally" bound if this pattern isn't marked
2219+
// as `weak`.
2220+
if (externalPatternType &&
2221+
optionality == ReferenceOwnershipOptionality::Required) {
2222+
// If the type is not yet known, let's add a constraint
2223+
// to make sure that it can only be bound to an optional type.
2224+
if (externalPatternType->isTypeVariableOrMember()) {
2225+
auto objectTy = CS.createTypeVariable(
2226+
CS.getConstraintLocator(locator.withPathElement(
2227+
ConstraintLocator::OptionalPayload)),
2228+
TVO_CanBindToLValue | TVO_CanBindToNoEscape);
22162229

2217-
if (oneWayVarType) {
2218-
oneWayVarType =
2219-
TypeChecker::getOptionalType(var->getLoc(), oneWayVarType);
2230+
CS.addConstraint(ConstraintKind::OptionalObject,
2231+
externalPatternType, objectTy, locator);
2232+
} else if (!externalPatternType->getOptionalObjectType()) {
2233+
// TODO(diagnostics): A tailored fix to indiciate that `weak`
2234+
// should have an optional type.
2235+
return Type();
2236+
}
22202237
}
2221-
break;
22222238

2223-
case ReferenceOwnershipOptionality::Allowed:
2224-
case ReferenceOwnershipOptionality::Disallowed:
2225-
break;
2239+
CS.addConstraint(ConstraintKind::OneWayEqual, oneWayVarType,
2240+
externalPatternType ? externalPatternType : varType,
2241+
locator);
22262242
}
22272243

22282244
// If we have a type to ascribe to the variable, do so now.

test/Constraints/result_builder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,19 @@ let ts1 = MyTupleStruct {
788788

789789
// CHECK: MyTupleStruct<(Int, String, Optional<String>), (Double, String)>(first: (Function), second: (3.14159, "blah"))
790790
print(ts1)
791+
792+
// Make sure that `weakV` is `Test?` and not `Test??`
793+
func test_weak_optionality_stays_the_same() {
794+
class Test {
795+
func fn() -> Int { 42 }
796+
}
797+
798+
tuplify(true) { c in
799+
weak var weakV: Test? = Test()
800+
801+
0
802+
if let v = weakV {
803+
v.fn()
804+
}
805+
}
806+
}

test/refactoring/ConvertAsync/basic.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,6 @@ func testPreserveComments2() {
835835
// PRESERVE-COMMENTS-ERROR-NEXT: // f
836836
// PRESERVE-COMMENTS-ERROR-NEXT: print("fun")
837837
// PRESERVE-COMMENTS-ERROR-NEXT: // g
838-
// PRESERVE-COMMENTS-ERROR-NEXT: {{ }}
839838
// PRESERVE-COMMENTS-ERROR-NEXT: }
840839

841840
// RUN: %refactor -convert-to-async -dump-text -source-filename %s -pos=%(line+1):1 | %FileCheck -check-prefix=PRESERVE-TRAILING-COMMENT-FN %s

test/refactoring/ConvertAsync/convert_function.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ func callNonAsyncInAsyncComment(_ completion: @escaping (String) -> Void) {
226226
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // i
227227
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
228228
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // j
229-
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: {{ }}
230229
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: // k
231230
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }
232231
// CALL-NON-ASYNC-IN-ASYNC-COMMENT-NEXT: }

test/refactoring/ConvertAsync/convert_params_single.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,12 @@ func testParamsSingle() async throws {
8787
// BOUND-COMMENT-NEXT: // l
8888
// BOUND-COMMENT-NEXT: print("after")
8989
// BOUND-COMMENT-NEXT: // m
90-
// BOUND-COMMENT-NEXT: {{ }}
9190
// BOUND-COMMENT-NEXT: } catch let bad {
9291
// BOUND-COMMENT-NEXT: // d
9392
// BOUND-COMMENT-NEXT: // e
9493
// BOUND-COMMENT-NEXT: print("got error \(bad)")
9594
// BOUND-COMMENT-NEXT: // f
9695
// BOUND-COMMENT-NEXT: // g
97-
// BOUND-COMMENT-NEXT: {{ }}
9896
// BOUND-COMMENT-NEXT: }
9997

10098

test/refactoring/ConvertAsync/convert_result.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ func testResultConversion() async throws {
381381
// NESTEDBREAK-COMMENT-NEXT: // l
382382
// NESTEDBREAK-COMMENT-NEXT: print("after")
383383
// NESTEDBREAK-COMMENT-NEXT: // m
384-
// NESTEDBREAK-COMMENT-NEXT: {{ }}
385384
// NESTEDBREAK-COMMENT-NOT: }
386385

387386
// RUN: %refactor-check-compiles -convert-call-to-async-alternative -dump-text -source-filename %s -pos=%(line+1):3 | %FileCheck -check-prefix=ERROR-BLOCK-COMMENT %s
@@ -416,13 +415,11 @@ func testResultConversion() async throws {
416415
// ERROR-BLOCK-COMMENT-NEXT: // h
417416
// ERROR-BLOCK-COMMENT-NEXT: print("after")
418417
// ERROR-BLOCK-COMMENT-NEXT: // i
419-
// ERROR-BLOCK-COMMENT-NEXT: {{ }}
420418
// ERROR-BLOCK-COMMENT-NEXT: } catch {
421419
// ERROR-BLOCK-COMMENT-NEXT: // e
422420
// ERROR-BLOCK-COMMENT-NEXT: print("fail")
423421
// ERROR-BLOCK-COMMENT-NEXT: // f
424422
// ERROR-BLOCK-COMMENT-NEXT: // g
425-
// ERROR-BLOCK-COMMENT-NEXT: {{ }}
426423
// ERROR-BLOCK-COMMENT-NEXT: }
427424
// ERROR-BLOCK-COMMENT-NOT: }
428425

0 commit comments

Comments
 (0)