Skip to content

Commit 6c02aa9

Browse files
Merge pull request #4661 from swiftwasm/main
[pull] swiftwasm from main
2 parents 63bf579 + ed88700 commit 6c02aa9

File tree

65 files changed

+3755
-316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3755
-316
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,18 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
488488
the result type provides a generalization where the callee chooses the
489489
resulting type and value.
490490

491+
* The compiler now correctly emits errors for `@available` attributes on stored properties with the `lazy` modifier or with attached property wrappers. Previously, the attribute was accepted on this subset of stored properties but the resulting binary would crash at runtime when type metadata was unavailable.
492+
493+
```swift
494+
struct S {
495+
@available(macOS 99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
496+
lazy var a: Int = 42
497+
498+
@available(macOS 99, *) // error: stored properties cannot be marked potentially unavailable with '@available'
499+
@Wrapper var b: Int
500+
}
501+
```
502+
491503
* The compiler now correctly emits warnings for more kinds of expressions where a protocol conformance is used and may be unavailable at runtime. Previously, member reference expressions and type erasing expressions that used potentially unavailable conformances were not diagnosed, leading to potential crashes at runtime.
492504

493505
```swift

SwiftCompilerSources/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ function(add_swift_compiler_modules_library name)
168168
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
169169
"-parse-as-library" ${sources}
170170
"-wmo" ${swift_compile_options}
171+
# LLVM modules and headers.
172+
"-Xcc" "-I" "-Xcc" "${LLVM_MAIN_INCLUDE_DIR}"
173+
# Generated LLVM headers.
174+
"-Xcc" "-I" "-Xcc" "${LLVM_INCLUDE_DIR}"
171175
# Bridging modules and headers.
172176
"-Xcc" "-I" "-Xcc" "${SWIFT_SOURCE_DIR}/include"
173177
# Generated C headers.

SwiftCompilerSources/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ private extension Target {
2020
"-Xfrontend", "-enable-experimental-cxx-interop",
2121
// Bridging modules and headers
2222
"-Xcc", "-I", "-Xcc", "../include",
23+
// LLVM modules and headers
24+
"-Xcc", "-I", "-Xcc", "../../llvm-project/llvm/include",
2325
"-cross-module-optimization"
2426
]),
2527
]

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public struct DiagnosticEngine {
8080
highlight: CharSourceRange? = nil,
8181
fixIts: [DiagnosticFixIt] = []) {
8282

83-
let bridgedSourceLoc: BridgedSourceLoc = position.bridged
83+
let bridgedSourceLoc: swift.SourceLoc = position.bridged
8484
let bridgedHighlightRange: BridgedCharSourceRange = highlight.bridged
8585
var bridgedArgs: [BridgedDiagnosticArgument] = []
8686
var bridgedFixIts: [BridgedDiagnosticFixIt] = []

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public struct SourceLoc {
2121
self.locationInFile = locationInFile
2222
}
2323

24-
public init?(bridged: BridgedSourceLoc) {
25-
guard let locationInFile = bridged.pointer else {
24+
public init?(bridged: swift.SourceLoc) {
25+
guard bridged.isValid() else {
2626
return nil
2727
}
28-
self.init(locationInFile: locationInFile)
28+
self.locationInFile = bridged.getOpaquePointerValue().assumingMemoryBound(to: UInt8.self)
2929
}
3030

31-
public var bridged: BridgedSourceLoc {
32-
.init(pointer: locationInFile)
31+
public var bridged: swift.SourceLoc {
32+
.init(llvm.SMLoc.getFromPointer(locationInFile))
3333
}
3434
}
3535

@@ -40,8 +40,8 @@ extension SourceLoc {
4040
}
4141

4242
extension Optional where Wrapped == SourceLoc {
43-
public var bridged: BridgedSourceLoc {
44-
self?.bridged ?? .init(pointer: nil)
43+
public var bridged: swift.SourceLoc {
44+
self?.bridged ?? .init()
4545
}
4646
}
4747

@@ -68,6 +68,6 @@ public struct CharSourceRange {
6868

6969
extension Optional where Wrapped == CharSourceRange {
7070
public var bridged: BridgedCharSourceRange {
71-
self?.bridged ?? .init(start: .init(pointer: nil), byteLength: 0)
71+
self?.bridged ?? .init(start: .init(), byteLength: 0)
7272
}
7373
}

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public func _RegexLiteralParsingFn(
9292
_ versionOut: UnsafeMutablePointer<CUnsignedInt>,
9393
_ captureStructureOut: UnsafeMutableRawPointer,
9494
_ captureStructureSize: CUnsignedInt,
95-
_ bridgedDiagnosticBaseLoc: BridgedSourceLoc,
95+
_ bridgedDiagnosticBaseLoc: swift.SourceLoc,
9696
_ bridgedDiagnosticEngine: BridgedDiagnosticEngine
9797
) -> Bool {
9898
let str = String(cString: inputPtr)

docs/DevelopmentTips.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@ Copy the invocation that has ` -o <build-path>/swift-macosx-x86_64/stdlib/publi
2323
### Choosing the bootstrapping mode
2424
By default, the compiler builds with the `boostrapping-with-hostlibs` (macOS) or `bootstrapping` (Linux) bootstrapping mode. To speed up local development it's recommended to build with the `hosttools` mode: `utils/build-script --bootstrapping=hosttools`.
2525

26-
It requires a recently new swift toolchain to be installed on your build machine. On macOS this comes with your Xcode installation.
26+
It requires a recently new swift toolchain to be installed on your build machine. You might need to download and install a nightly Swift toolchain to build the Swift project in `hosttools` mode.
2727

2828
Not that changing the bootstrapping mode needs a reconfiguration.
2929

30+
#### Using a locally built Swift toolchain
31+
32+
If you do not want to install a nightly Swift toolchain, or you need to debug Swift code within SwiftCompilerSources, you can build the Swift toolchain in `boostrapping-with-hostlibs` mode on your local machine once, and then use this toolchain to iterate on your changes with the `hosttools` mode:
33+
34+
* Build the toolchain locally in `boostrapping-with-hostlibs` mode: `./utils/build-toolchain com.yourname`.
35+
* Copy the `swift-LOCAL-YYYY-MM-DD.xctoolchain` file from `./swift-nightly-install/Library/Developer/Toolchains` to `/Library/Developer/Toolchains`.
36+
* Launch Xcode, in the menu bar select _Xcode_ > _Toolchains_ > _Local Swift Development Snapshot YYYY-MM-DD_.
37+
* Remove the Swift build directory: `./build`.
38+
* Run the Swift build script with the locally built Swift toolchain in `hosttools` mode: `TOOLCHAINS=com.yourname.YYYYMMDD ./utils/build-script --bootstrapping=hosttools`. Repeat this step as you iterate on your change.
39+
40+
To debug using LLDB, run LLDB from the locally built toolchain with a couple of environment variables set:
41+
```
42+
DYLD_LIBRARY_PATH=/Library/Developer/Toolchains/swift-LOCAL-YYYY-MM-DD.xctoolchain/usr/lib/swift/macosx DYLD_FRAMEWORK_PATH=/Applications/Xcode.app/Contents/Developer/Library/Frameworks /Library/Developer/Toolchains/swift-LOCAL-YYYY-MM-DD.xctoolchain/usr/bin/lldb
43+
```
44+
3045
### Working with two build directories
3146
For developing and debugging you are probably building a debug configuration of swift. But it's often beneficial to also build a release-assert configuration in parallel (`utils/build-script -R`).
3247

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
#include <stdbool.h>
1919
#include <stddef.h>
2020

21-
#ifdef __cplusplus
22-
extern "C" {
23-
#endif
24-
2521
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2622

2723
//===----------------------------------------------------------------------===//
@@ -56,7 +52,7 @@ typedef struct {
5652
} BridgedDiagnosticArgument;
5753

5854
typedef struct {
59-
BridgedSourceLoc start;
55+
swift::SourceLoc start;
6056
SwiftInt byteLength;
6157
BridgedStringRef text;
6258
} BridgedDiagnosticFixIt;
@@ -70,7 +66,7 @@ typedef struct {
7066
} BridgedOptionalDiagnosticEngine;
7167

7268
// FIXME: Can we bridge InFlightDiagnostic?
73-
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, BridgedSourceLoc loc,
69+
void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, swift::SourceLoc loc,
7470
BridgedDiagID diagID, BridgedArrayRef arguments,
7571
BridgedCharSourceRange highlight,
7672
BridgedArrayRef fixIts);
@@ -79,8 +75,4 @@ bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine);
7975

8076
SWIFT_END_NULLABILITY_ANNOTATIONS
8177

82-
#ifdef __cplusplus
83-
} // extern "C"
84-
#endif
85-
8678
#endif // SWIFT_AST_ASTBRIDGING_H

include/swift/AST/Decl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6414,9 +6414,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
64146414
/// A function is concurrent if it has the @Sendable attribute.
64156415
bool isSendable() const;
64166416

6417-
/// Determine if function has 'nonisolated' attribute
6418-
bool isNonisolated() const;
6419-
64206417
/// Returns true if the function is a suitable 'async' context.
64216418
///
64226419
/// Functions that are an 'async' context can make calls to 'async' functions.

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4495,8 +4495,8 @@ ERROR(actor_inheritance,none,
44954495
(bool))
44964496

44974497
ERROR(actor_protocol_illegal_inheritance,none,
4498-
"non-actor type %0 cannot conform to the 'Actor' protocol",
4499-
(DeclName))
4498+
"non-actor type %0 cannot conform to the %1 protocol",
4499+
(DeclName, DeclName))
45004500
ERROR(distributed_actor_conformance_missing_system_type,none,
45014501
"distributed actor %0 does not declare ActorSystem it can be used with.",
45024502
(DeclName))

include/swift/AST/KnownProtocols.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
BUILTIN_EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(name, "_" #name)
6060

6161
PROTOCOL(Actor)
62+
PROTOCOL(AnyActor)
6263
PROTOCOL(Sequence)
6364
PROTOCOL(Identifiable)
6465
PROTOCOL(IteratorProtocol)

include/swift/Basic/BasicBridging.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
#define SWIFT_BASIC_BASICBRIDGING_H
1515

1616
#include "swift/Basic/BridgedSwiftObject.h"
17+
#include "swift/Basic/SourceLoc.h"
1718
#include <stddef.h>
1819

19-
#ifdef __cplusplus
20-
extern "C" {
21-
#endif
22-
2320
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2421

2522
typedef intptr_t SwiftInt;
@@ -48,18 +45,10 @@ void freeBridgedStringRef(BridgedStringRef str);
4845
//===----------------------------------------------------------------------===//
4946

5047
typedef struct {
51-
const unsigned char * _Nullable pointer;
52-
} BridgedSourceLoc;
53-
54-
typedef struct {
55-
BridgedSourceLoc start;
48+
swift::SourceLoc start;
5649
SwiftInt byteLength;
5750
} BridgedCharSourceRange;
5851

5952
SWIFT_END_NULLABILITY_ANNOTATIONS
6053

61-
#ifdef __cplusplus
62-
} // extern "C"
63-
#endif
64-
6554
#endif

include/swift/Basic/BridgingUtils.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,14 @@ inline llvm::ArrayRef<T> getArrayRef(BridgedArrayRef bridged) {
3636
return {static_cast<const T *>(bridged.data), bridged.numElements};
3737
}
3838

39-
inline SourceLoc getSourceLoc(const BridgedSourceLoc &bridged) {
40-
return SourceLoc(
41-
llvm::SMLoc::getFromPointer((const char *)bridged.pointer));
42-
}
43-
44-
inline BridgedSourceLoc getBridgedSourceLoc(const SourceLoc &loc) {
45-
return {static_cast<const unsigned char *>(loc.getOpaquePointerValue())};
46-
}
47-
4839
inline CharSourceRange
4940
getCharSourceRange(const BridgedCharSourceRange &bridged) {
50-
auto start = getSourceLoc(bridged.start);
51-
return CharSourceRange(start, bridged.byteLength);
41+
return CharSourceRange(bridged.start, bridged.byteLength);
5242
}
5343

5444
inline BridgedCharSourceRange
5545
getBridgedCharSourceRange(const CharSourceRange &range) {
56-
auto start = getBridgedSourceLoc(range.getStart());
57-
return {start, range.getByteLength()};
46+
return {range.getStart(), range.getByteLength()};
5847
}
5948

6049
/// Copies the string in an malloc'ed memory and the caller is responsible for

include/swift/Parse/RegexParserBridging.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#include "swift/AST/ASTBridging.h"
1818
#include <stdbool.h>
1919

20-
#ifdef __cplusplus
21-
extern "C" {
22-
#endif
23-
2420
/// Attempt to lex a regex literal string. Takes the following arguments:
2521
///
2622
/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the
@@ -60,12 +56,8 @@ typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull,
6056
/*VersionOut*/ unsigned *_Nonnull,
6157
/*CaptureStructureOut*/ void *_Nonnull,
6258
/*CaptureStructureSize*/ unsigned,
63-
/*DiagnosticBaseLoc*/ BridgedSourceLoc,
59+
/*DiagnosticBaseLoc*/ swift::SourceLoc,
6460
BridgedDiagnosticEngine);
6561
void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn);
6662

67-
#ifdef __cplusplus
68-
} // extern "C"
69-
#endif
70-
7163
#endif // REGEX_PARSER_BRIDGING

lib/AST/ASTBridging.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ getDiagnosticArgument(const BridgedDiagnosticArgument &bridged) {
3838
} // namespace
3939

4040
void DiagnosticEngine_diagnose(
41-
BridgedDiagnosticEngine bridgedEngine, BridgedSourceLoc bridgedLoc,
41+
BridgedDiagnosticEngine bridgedEngine, SourceLoc loc,
4242
BridgedDiagID bridgedDiagID,
4343
BridgedArrayRef /*BridgedDiagnosticArgument*/ bridgedArguments,
4444
BridgedCharSourceRange bridgedHighlight,
4545
BridgedArrayRef /*BridgedDiagnosticFixIt*/ bridgedFixIts) {
4646
auto *D = getDiagnosticEngine(bridgedEngine);
4747

48-
auto loc = getSourceLoc(bridgedLoc);
4948
auto diagID = static_cast<DiagID>(bridgedDiagID);
5049
SmallVector<DiagnosticArgument, 2> arguments;
5150
for (auto bridgedArg :
@@ -62,7 +61,7 @@ void DiagnosticEngine_diagnose(
6261

6362
// Add fix-its.
6463
for (auto bridgedFixIt : getArrayRef<BridgedDiagnosticFixIt>(bridgedFixIts)) {
65-
auto range = CharSourceRange(getSourceLoc(bridgedFixIt.start),
64+
auto range = CharSourceRange(bridgedFixIt.start,
6665
bridgedFixIt.byteLength);
6766
auto text = getStringRef(bridgedFixIt.text);
6867
inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text);

lib/AST/Decl.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7781,10 +7781,6 @@ bool AbstractFunctionDecl::isSendable() const {
77817781
return getAttrs().hasAttribute<SendableAttr>();
77827782
}
77837783

7784-
bool AbstractFunctionDecl::isNonisolated() const {
7785-
return getAttrs().hasAttribute<NonisolatedAttr>();
7786-
}
7787-
77887784
bool AbstractFunctionDecl::isBackDeployed() const {
77897785
if (getAttrs().hasAttribute<BackDeployAttr>())
77907786
return true;

lib/AST/Type.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,67 @@ Type TypeBase::stripConcurrency(bool recurse, bool dropGlobalActor) {
960960
return newFnType;
961961
}
962962

963+
if (auto existentialType = getAs<ExistentialType>()) {
964+
auto newConstraintType = existentialType->getConstraintType()
965+
->stripConcurrency(recurse, dropGlobalActor);
966+
if (newConstraintType.getPointer() ==
967+
existentialType->getConstraintType().getPointer())
968+
return Type(this);
969+
970+
return ExistentialType::get(newConstraintType);
971+
}
972+
973+
if (auto protocolType = getAs<ProtocolType>()) {
974+
if (protocolType->getDecl()->isSpecificProtocol(
975+
KnownProtocolKind::Sendable))
976+
return ProtocolCompositionType::get(getASTContext(), { }, false);
977+
978+
return Type(this);
979+
}
980+
981+
if (auto protocolCompositionType = getAs<ProtocolCompositionType>()) {
982+
SmallVector<Type, 4> newMembers;
983+
auto members = protocolCompositionType->getMembers();
984+
for (unsigned i : indices(members)) {
985+
auto memberType = members[i];
986+
auto newMemberType =
987+
memberType->stripConcurrency(recurse, dropGlobalActor);
988+
if (!newMembers.empty()) {
989+
newMembers.push_back(newMemberType);
990+
continue;
991+
}
992+
993+
if (memberType.getPointer() != newMemberType.getPointer()) {
994+
newMembers.append(members.begin(), members.begin() + i);
995+
newMembers.push_back(newMemberType);
996+
continue;
997+
}
998+
}
999+
1000+
if (!newMembers.empty()) {
1001+
return ProtocolCompositionType::get(
1002+
getASTContext(), newMembers,
1003+
protocolCompositionType->hasExplicitAnyObject());
1004+
}
1005+
1006+
return Type(this);
1007+
}
1008+
1009+
if (auto existentialMetatype = getAs<ExistentialMetatypeType>()) {
1010+
auto instanceType = existentialMetatype->getExistentialInstanceType();
1011+
auto newInstanceType =
1012+
instanceType->stripConcurrency(recurse, dropGlobalActor);
1013+
if (instanceType.getPointer() != newInstanceType.getPointer()) {
1014+
Optional<MetatypeRepresentation> repr;
1015+
if (existentialMetatype->hasRepresentation())
1016+
repr = existentialMetatype->getRepresentation();
1017+
return ExistentialMetatypeType::get(
1018+
newInstanceType, repr, getASTContext());
1019+
}
1020+
1021+
return Type(this);
1022+
}
1023+
9631024
return Type(this);
9641025
}
9651026

0 commit comments

Comments
 (0)