Skip to content

Commit fee89b2

Browse files
Merge pull request #5437 from swiftwasm/main
[pull] swiftwasm from main
2 parents cfd8fed + 1db0947 commit fee89b2

File tree

15 files changed

+544
-312
lines changed

15 files changed

+544
-312
lines changed

CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,39 @@
55
66
## Swift 5.9
77

8+
* [SE-0380][]:
9+
10+
`if` and `switch` statements may now be used as expressions to:
11+
12+
* Return values from functions, properties, and closures (either with
13+
implicit or explicit `return`)
14+
* Throw errors using `throw`
15+
* Assign values to variables
16+
* Declare variables
17+
18+
Each branch of the `if` or `switch` must be a single expression, the value
19+
of which becomes the value of the overall expression when that branch is
20+
chosen.
21+
22+
```swift
23+
let bullet =
24+
if isRoot && (count == 0 || !willExpand) { "" }
25+
else if count == 0 { "- " }
26+
else if maxDepth <= 0 { "" }
27+
else { "" }
28+
```
29+
30+
```swift
31+
public static func width(_ x: Unicode.Scalar) -> Int {
32+
switch x.value {
33+
case 0..<0x80: 1
34+
case 0x80..<0x0800: 2
35+
case 0x0800..<0x1_0000: 3
36+
default: 4
37+
}
38+
}
39+
```
40+
841
* [#64927][]:
942

1043
Swift 5.9 introduces warnings that catch conversions from an inout
@@ -9732,6 +9765,7 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
97329765
[SE-0370]: <https://github.com/apple/swift-evolution/blob/main/proposals/0370-pointer-family-initialization-improvements.md>
97339766
[SE-0376]: <https://github.com/apple/swift-evolution/blob/main/proposals/0376-function-back-deployment.md>
97349767
[SE-0377]: <https://github.com/apple/swift-evolution/blob/main/proposals/0377-parameter-ownership-modifiers.md>
9768+
[SE-0380]: <https://github.com/apple/swift-evolution/blob/main/proposals/0380-if-switch-expressions.md>
97359769

97369770
[#64927]: <https://github.com/apple/swift/issues/64927>
97379771
[#42697]: <https://github.com/apple/swift/issues/42697>

include/swift/Frontend/BackDeploymentLibs.def

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
//===----------------------------------------------------------------------===//
2222

2323
#ifndef BACK_DEPLOYMENT_LIB
24-
# error "Must define BACK_DEPLOYMENT_LIB(Version, Filter, Library)"
24+
# error "Must define BACK_DEPLOYMENT_LIB(Version, Filter, Library, ForceLoad)"
2525
#endif
2626

27-
BACK_DEPLOYMENT_LIB((5, 0), all, "swiftCompatibility50")
28-
BACK_DEPLOYMENT_LIB((5, 1), all, "swiftCompatibility51")
29-
BACK_DEPLOYMENT_LIB((5, 0), executable, "swiftCompatibilityDynamicReplacements")
30-
BACK_DEPLOYMENT_LIB((5, 4), all, "swiftCompatibilityConcurrency")
31-
BACK_DEPLOYMENT_LIB((5, 6), all, "swiftCompatibility56")
27+
BACK_DEPLOYMENT_LIB((5, 0), all, "swiftCompatibility50", true)
28+
BACK_DEPLOYMENT_LIB((5, 1), all, "swiftCompatibility51", true)
29+
BACK_DEPLOYMENT_LIB((5, 0), executable, "swiftCompatibilityDynamicReplacements", true)
30+
BACK_DEPLOYMENT_LIB((5, 4), all, "swiftCompatibilityConcurrency", true)
31+
BACK_DEPLOYMENT_LIB((5, 6), all, "swiftCompatibility56", true)
32+
BACK_DEPLOYMENT_LIB((5, 8), all, "swiftCompatibilityPacks", false)
3233

3334
#undef BACK_DEPLOYMENT_LIB

include/swift/IDE/ArgumentCompletion.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
2929
Type ExpectedCallType;
3030
/// True if this is a subscript rather than a function call.
3131
bool IsSubscript;
32-
/// The reference to the FuncDecl or SubscriptDecl associated with the call.
33-
ConcreteDeclRef FuncDeclRef;
32+
/// The FuncDecl or SubscriptDecl associated with the call.
33+
ValueDecl *FuncD;
3434
/// The type of the function being called.
3535
AnyFunctionType *FuncTy;
3636
/// The index of the argument containing the completion location
@@ -50,14 +50,18 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
5050
/// Whether the surrounding context is async and thus calling async
5151
/// functions is supported.
5252
bool IsInAsyncContext;
53+
/// A bitfield to mark whether the parameter at a given index is optional.
54+
/// Parameters can be optional if they have a default argument or belong to
55+
/// a parameter pack.
56+
/// Indices are based on the parameters in \c FuncTy. Note that the number
57+
/// of parameters in \c FuncTy and \c FuncD is different when a parameter
58+
/// pack has been exploded.
59+
llvm::BitVector DeclParamIsOptional;
5360

5461
/// Types of variables that were determined in the solution that produced
5562
/// this result. This in particular includes parameters of closures that
5663
/// were type-checked with the code completion expression.
5764
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
58-
59-
/// Return the \c FuncDecl or \c SubscriptDecl associated with this call.
60-
ValueDecl *getFuncD() const { return FuncDeclRef.getDecl(); }
6165
};
6266

6367
CodeCompletionExpr *CompletionExpr;

lib/AST/ASTScopePrinting.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,10 @@ NullablePtr<const void> ASTScopeImpl::addressForPrinting() const {
148148
void GenericTypeOrExtensionScope::printSpecifics(llvm::raw_ostream &out) const {
149149
if (shouldHaveABody() && !doesDeclHaveABody())
150150
out << "<no body>";
151-
// Sadly, the following can trip assertions
152-
// else if (auto *n = getCorrespondingNominalTypeDecl().getPtrOrNull())
153-
// out << "'" << n->getFullName() << "'";
154-
// else
155-
// out << "<no extended nominal?!>";
151+
else if (auto *n = getCorrespondingNominalTypeDecl().getPtrOrNull())
152+
out << "'" << n->getName() << "'";
153+
else
154+
out << "<no extended nominal?!>";
156155
}
157156

158157
void GenericParamScope::printSpecifics(llvm::raw_ostream &out) const {

lib/Basic/TargetInfo.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ using namespace swift;
2323
/// Print information about a
2424
static void printCompatibilityLibrary(
2525
llvm::VersionTuple runtimeVersion, llvm::VersionTuple maxVersion,
26-
StringRef filter, StringRef libraryName, bool &printedAny,
27-
llvm::raw_ostream &out) {
26+
StringRef filter, StringRef libraryName, bool forceLoad,
27+
bool &printedAny, llvm::raw_ostream &out) {
2828
if (runtimeVersion > maxVersion)
2929
return;
3030

@@ -33,16 +33,21 @@ static void printCompatibilityLibrary(
3333
}
3434

3535
out << "\n";
36-
out << " {\n";
36+
out << " {";
3737

38-
out << " \"libraryName\": \"";
38+
out << "\n \"libraryName\": \"";
3939
swift::writeEscaped(libraryName, out);
40-
out << "\",\n";
40+
out << "\",";
4141

42-
out << " \"filter\": \"";
42+
out << "\n \"filter\": \"";
4343
swift::writeEscaped(filter, out);
44-
out << "\"\n";
45-
out << " }";
44+
out << "\"";
45+
46+
if (!forceLoad) {
47+
out << ",\n \"forceLoad\": false";
48+
}
49+
50+
out << "\n }";
4651

4752
printedAny = true;
4853
}
@@ -132,10 +137,10 @@ void targetinfo::printTripleInfo(const llvm::Triple &triple,
132137
// Compatibility libraries that need to be linked.
133138
out << " \"compatibilityLibraries\": [";
134139
bool printedAnyCompatibilityLibrary = false;
135-
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
136-
printCompatibilityLibrary( \
140+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
141+
printCompatibilityLibrary( \
137142
*runtimeVersion, llvm::VersionTuple Version, #Filter, LibraryName, \
138-
printedAnyCompatibilityLibrary, out);
143+
ForceLoad, printedAnyCompatibilityLibrary, out);
139144
#include "swift/Frontend/BackDeploymentLibs.def"
140145

141146
if (printedAnyCompatibilityLibrary) {

lib/Driver/DarwinToolChains.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
406406
runtimeCompatibilityVersion = llvm::VersionTuple(5, 5);
407407
} else if (value.equals("5.6")) {
408408
runtimeCompatibilityVersion = llvm::VersionTuple(5, 6);
409+
} else if (value.equals("5.8")) {
410+
runtimeCompatibilityVersion = llvm::VersionTuple(5, 8);
409411
} else if (value.equals("none")) {
410412
runtimeCompatibilityVersion = None;
411413
} else {
@@ -419,7 +421,8 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
419421
if (runtimeCompatibilityVersion) {
420422
auto addBackDeployLib = [&](llvm::VersionTuple version,
421423
BackDeployLibFilter filter,
422-
StringRef libraryName) {
424+
StringRef libraryName,
425+
bool forceLoad) {
423426
if (*runtimeCompatibilityVersion > version)
424427
return;
425428

@@ -431,14 +434,16 @@ toolchains::Darwin::addArgsToLinkStdlib(ArgStringList &Arguments,
431434
llvm::sys::path::append(BackDeployLib, "lib" + libraryName + ".a");
432435

433436
if (llvm::sys::fs::exists(BackDeployLib)) {
434-
Arguments.push_back("-force_load");
437+
if (forceLoad)
438+
Arguments.push_back("-force_load");
435439
Arguments.push_back(context.Args.MakeArgString(BackDeployLib));
436440
}
437441
};
438442

439-
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
440-
addBackDeployLib( \
441-
llvm::VersionTuple Version, BackDeployLibFilter::Filter, LibraryName);
443+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
444+
addBackDeployLib( \
445+
llvm::VersionTuple Version, BackDeployLibFilter::Filter, \
446+
LibraryName, ForceLoad);
442447
#include "swift/Frontend/BackDeploymentLibs.def"
443448
}
444449

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,8 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
26072607
runtimeCompatibilityVersion = llvm::VersionTuple(5, 5);
26082608
} else if (version.equals("5.6")) {
26092609
runtimeCompatibilityVersion = llvm::VersionTuple(5, 6);
2610+
} else if (version.equals("5.8")) {
2611+
runtimeCompatibilityVersion = llvm::VersionTuple(5, 8);
26102612
} else {
26112613
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
26122614
versionArg->getAsString(Args), version);

lib/IDE/ArgumentCompletion.cpp

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,8 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
4747
break;
4848
}
4949

50-
// We work with the parameter from the function type and the declaration
51-
// because they contain different information that we need.
52-
//
53-
// Since not all function types are backed by declarations (e.g. closure
54-
// paramters), `DeclParam` might be `nullptr`.
5550
const AnyFunctionType::Param *TypeParam = &ParamsToPass[Idx];
56-
const ParamDecl *DeclParam = nullptr;
57-
if (Res.FuncDeclRef) {
58-
DeclParam = getParameterAt(Res.FuncDeclRef, Idx);
59-
}
60-
61-
bool Required = true;
62-
if (DeclParam && DeclParam->isDefaultArgument()) {
63-
Required = false;
64-
} else if (DeclParam && DeclParam->getType()->is<PackExpansionType>()) {
65-
Required = false;
66-
} else if (TypeParam->isVariadic()) {
67-
Required = false;
68-
}
51+
bool Required = !Res.DeclParamIsOptional[Idx];
6952

7053
if (TypeParam->hasLabel() && !(IsCompletion && Res.IsNoninitialVariadic)) {
7154
// Suggest parameter label if parameter has label, we are completing in it
@@ -225,7 +208,7 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
225208

226209
// If this is a duplicate of any other result, ignore this solution.
227210
if (llvm::any_of(Results, [&](const Result &R) {
228-
return R.FuncDeclRef == Info.ValueRef &&
211+
return R.FuncD == Info.getValue() &&
229212
nullableTypesEqual(R.FuncTy, Info.ValueTy) &&
230213
nullableTypesEqual(R.BaseType, Info.BaseTy) &&
231214
R.ParamIdx == ParamIdx &&
@@ -241,11 +224,34 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
241224
if (Info.ValueTy) {
242225
FuncTy = Info.ValueTy->lookThroughAllOptionalTypes()->getAs<AnyFunctionType>();
243226
}
227+
228+
// Determine which parameters are optional. We need to do this in
229+
// `sawSolutionImpl` because it accesses the substitution map in
230+
// `Info.ValueRef`. This substitution map might contain type variables that
231+
// are allocated in the constraint system's arena and are freed once we reach
232+
// `deliverResults`.
233+
llvm::BitVector DeclParamIsOptional;
234+
if (FuncTy) {
235+
ArrayRef<AnyFunctionType::Param> ParamsToPass = FuncTy->getParams();
236+
for (auto Idx : range(0, ParamsToPass.size())) {
237+
bool Optional = false;
238+
if (Info.ValueRef) {
239+
if (const ParamDecl *DeclParam = getParameterAt(Info.ValueRef, Idx)) {
240+
Optional |= DeclParam->isDefaultArgument();
241+
Optional |= DeclParam->getType()->is<PackExpansionType>();
242+
}
243+
}
244+
const AnyFunctionType::Param *TypeParam = &ParamsToPass[Idx];
245+
Optional |= TypeParam->isVariadic();
246+
DeclParamIsOptional.push_back(Optional);
247+
}
248+
}
249+
244250
Results.push_back({ExpectedTy, ExpectedCallType,
245-
isa<SubscriptExpr>(ParentCall), Info.ValueRef, FuncTy,
251+
isa<SubscriptExpr>(ParentCall), Info.getValue(), FuncTy,
246252
ArgIdx, ParamIdx, std::move(ClaimedParams),
247253
IsNoninitialVariadic, Info.BaseTy, HasLabel, IsAsync,
248-
SolutionSpecificVarTypes});
254+
DeclParamIsOptional, SolutionSpecificVarTypes});
249255
}
250256

251257
void ArgumentTypeCheckCompletionCallback::computeShadowedDecls(
@@ -254,25 +260,25 @@ void ArgumentTypeCheckCompletionCallback::computeShadowedDecls(
254260
auto &ResultA = Results[i];
255261
for (size_t j = i + 1; j < Results.size(); ++j) {
256262
auto &ResultB = Results[j];
257-
if (!ResultA.getFuncD() || !ResultB.getFuncD() || !ResultA.FuncTy ||
263+
if (!ResultA.FuncD || !ResultB.FuncD || !ResultA.FuncTy ||
258264
!ResultB.FuncTy) {
259265
continue;
260266
}
261-
if (ResultA.getFuncD()->getName() != ResultB.getFuncD()->getName()) {
267+
if (ResultA.FuncD->getName() != ResultB.FuncD->getName()) {
262268
continue;
263269
}
264270
if (!ResultA.FuncTy->isEqual(ResultB.FuncTy)) {
265271
continue;
266272
}
267273
ProtocolDecl *inProtocolExtensionA =
268-
ResultA.getFuncD()->getDeclContext()->getExtendedProtocolDecl();
274+
ResultA.FuncD->getDeclContext()->getExtendedProtocolDecl();
269275
ProtocolDecl *inProtocolExtensionB =
270-
ResultB.getFuncD()->getDeclContext()->getExtendedProtocolDecl();
276+
ResultB.FuncD->getDeclContext()->getExtendedProtocolDecl();
271277

272278
if (inProtocolExtensionA && !inProtocolExtensionB) {
273-
ShadowedDecls.insert(ResultA.getFuncD());
279+
ShadowedDecls.insert(ResultA.FuncD);
274280
} else if (!inProtocolExtensionA && inProtocolExtensionB) {
275-
ShadowedDecls.insert(ResultB.getFuncD());
281+
ShadowedDecls.insert(ResultB.FuncD);
276282
}
277283
}
278284
}
@@ -313,37 +319,35 @@ void ArgumentTypeCheckCompletionCallback::deliverResults(
313319
}
314320
if ((BaseNominal = BaseTy->getAnyNominal())) {
315321
SemanticContext = SemanticContextKind::CurrentNominal;
316-
if (Result.getFuncD() &&
317-
Result.getFuncD()->getDeclContext()->getSelfNominalTypeDecl() !=
322+
if (Result.FuncD &&
323+
Result.FuncD->getDeclContext()->getSelfNominalTypeDecl() !=
318324
BaseNominal) {
319325
SemanticContext = SemanticContextKind::Super;
320326
}
321327
} else if (BaseTy->is<TupleType>() || BaseTy->is<SubstitutableType>()) {
322328
SemanticContext = SemanticContextKind::CurrentNominal;
323329
}
324330
}
325-
if (SemanticContext == SemanticContextKind::None && Result.getFuncD()) {
326-
if (Result.getFuncD()->getDeclContext()->isTypeContext()) {
331+
if (SemanticContext == SemanticContextKind::None && Result.FuncD) {
332+
if (Result.FuncD->getDeclContext()->isTypeContext()) {
327333
SemanticContext = SemanticContextKind::CurrentNominal;
328-
} else if (Result.getFuncD()->getDeclContext()->isLocalContext()) {
334+
} else if (Result.FuncD->getDeclContext()->isLocalContext()) {
329335
SemanticContext = SemanticContextKind::Local;
330-
} else if (Result.getFuncD()->getModuleContext() ==
331-
DC->getParentModule()) {
336+
} else if (Result.FuncD->getModuleContext() == DC->getParentModule()) {
332337
SemanticContext = SemanticContextKind::CurrentModule;
333338
}
334339
}
335340
if (Result.FuncTy) {
336341
if (auto FuncTy = Result.FuncTy) {
337-
if (ShadowedDecls.count(Result.getFuncD()) == 0) {
342+
if (ShadowedDecls.count(Result.FuncD) == 0) {
338343
// Don't show call pattern completions if the function is
339344
// overridden.
340345
if (Result.IsSubscript) {
341346
assert(SemanticContext != SemanticContextKind::None);
342-
auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.getFuncD());
347+
auto *SD = dyn_cast_or_null<SubscriptDecl>(Result.FuncD);
343348
Lookup.addSubscriptCallPattern(FuncTy, SD, SemanticContext);
344349
} else {
345-
auto *FD =
346-
dyn_cast_or_null<AbstractFunctionDecl>(Result.getFuncD());
350+
auto *FD = dyn_cast_or_null<AbstractFunctionDecl>(Result.FuncD);
347351
Lookup.addFunctionCallPattern(FuncTy, FD, SemanticContext);
348352
}
349353
}

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,8 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
511511
// harmless aside from code size.
512512
if (!IRGen.Opts.UseJIT) {
513513
auto addBackDeployLib = [&](llvm::VersionTuple version,
514-
StringRef libraryName) {
514+
StringRef libraryName,
515+
bool forceLoad) {
515516
Optional<llvm::VersionTuple> compatibilityVersion;
516517
if (libraryName == "swiftCompatibilityDynamicReplacements") {
517518
compatibilityVersion = IRGen.Opts.
@@ -532,11 +533,11 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
532533

533534
this->addLinkLibrary(LinkLibrary(libraryName,
534535
LibraryKind::Library,
535-
/*forceLoad*/ true));
536+
forceLoad));
536537
};
537538

538-
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
539-
addBackDeployLib(llvm::VersionTuple Version, LibraryName);
539+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
540+
addBackDeployLib(llvm::VersionTuple Version, LibraryName, ForceLoad);
540541
#include "swift/Frontend/BackDeploymentLibs.def"
541542
}
542543
}

0 commit comments

Comments
 (0)