Skip to content

Commit e3ef3d1

Browse files
authored
Merge branch 'apple:main' into nicked/getting-started
2 parents 125a2e8 + 5429100 commit e3ef3d1

File tree

15 files changed

+154
-72
lines changed

15 files changed

+154
-72
lines changed

include/swift/Basic/Compiler.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef SWIFT_BASIC_COMPILER_H
1414
#define SWIFT_BASIC_COMPILER_H
1515

16+
#include <stddef.h>
17+
1618
#if defined(_MSC_VER) && !defined(__clang__)
1719
#define SWIFT_COMPILER_IS_MSVC 1
1820
#else
@@ -190,4 +192,21 @@
190192
#define ENUM_EXTENSIBILITY_ATTR(arg)
191193
#endif
192194

195+
// The 'u8' string literal prefix creates `char` types on C++14/17 but
196+
// `char8_t` types on C++20. To support compiling in both modes
197+
// simultaneously, wrap Unicode literals in `SWIFT_UTF8("...")` to ensure
198+
// that they are interpreted by the compiler as UTF-8 but always return
199+
// `char` types.
200+
#if defined(__cplusplus)
201+
#if defined(__cpp_char8_t)
202+
inline constexpr char operator""_swift_u8(char8_t c) { return c; }
203+
inline const char *operator""_swift_u8(const char8_t *p, std::size_t) {
204+
return reinterpret_cast<const char *>(p);
205+
}
206+
#define SWIFT_UTF8(literal) u8##literal##_swift_u8
207+
#else // !defined(__cpp_char8_t)
208+
#define SWIFT_UTF8(literal) u8##literal
209+
#endif // defined(__cpp_char8_t)
210+
#endif // defined(__cplusplus)
211+
193212
#endif // SWIFT_BASIC_COMPILER_H

lib/AST/Type.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/AST/SubstitutionMap.h"
3737
#include "swift/AST/TypeLoc.h"
3838
#include "swift/AST/TypeRepr.h"
39+
#include "swift/Basic/Compiler.h"
3940
#include "clang/AST/Type.h"
4041
#include "llvm/ADT/APFloat.h"
4142
#include "llvm/ADT/SmallPtrSet.h"
@@ -2036,8 +2037,8 @@ Identifier GenericTypeParamType::getName() const {
20362037
llvm::SmallString<10> nameBuf;
20372038
llvm::raw_svector_ostream os(nameBuf);
20382039

2039-
static const char *tau = u8"\u03C4_";
2040-
2040+
static const char *tau = SWIFT_UTF8("\u03C4_");
2041+
20412042
os << tau << getDepth() << '_' << getIndex();
20422043
Identifier name = C.getIdentifier(os.str());
20432044
names.insert({depthIndex, name});

lib/Basic/Unicode.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "swift/Basic/Unicode.h"
14+
#include "swift/Basic/Compiler.h"
1415
#include "llvm/ADT/SmallString.h"
1516
#include "llvm/ADT/SmallVector.h"
1617
#include "llvm/Support/ConvertUTF.h"
@@ -135,7 +136,7 @@ std::string swift::unicode::sanitizeUTF8(StringRef Text) {
135136
Builder.reserve(Text.size());
136137
const llvm::UTF8* Data = reinterpret_cast<const llvm::UTF8*>(Text.begin());
137138
const llvm::UTF8* End = reinterpret_cast<const llvm::UTF8*>(Text.end());
138-
StringRef Replacement = u8"\ufffd";
139+
StringRef Replacement = SWIFT_UTF8("\ufffd");
139140
while (Data < End) {
140141
auto Step = llvm::getNumBytesForUTF8(*Data);
141142
if (Data + Step > End) {

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/AST/ModuleLoader.h"
2929
#include "swift/AST/Pattern.h"
3030
#include "swift/AST/TypeDifferenceVisitor.h"
31+
#include "swift/Basic/Compiler.h"
3132
#include "swift/Basic/Dwarf.h"
3233
#include "swift/Basic/SourceManager.h"
3334
#include "swift/Basic/Version.h"
@@ -3034,7 +3035,7 @@ void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
30343035
return;
30353036

30363037
llvm::SmallString<8> Buf;
3037-
static const char *Tau = u8"\u03C4";
3038+
static const char *Tau = SWIFT_UTF8("\u03C4");
30383039
llvm::raw_svector_ostream OS(Buf);
30393040
OS << '$' << Tau << '_' << Depth << '_' << Index;
30403041
uint64_t PtrWidthInBits = CI.getTargetInfo().getPointerWidth(0);

lib/Macros/Sources/SwiftMacros/OptionSetMacro.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,25 @@ public struct OptionSetMacro {
123123
}
124124
}
125125

126-
extension OptionSetMacro: ConformanceMacro {
127-
public static func expansion<
128-
Decl: DeclGroupSyntax,
129-
Context: MacroExpansionContext
130-
>(
126+
extension OptionSetMacro: ExtensionMacro {
127+
public static func expansion(
131128
of attribute: AttributeSyntax,
132-
providingConformancesOf decl: Decl,
133-
in context: Context
134-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
135-
// Decode the expansion arguments.
136-
guard let (structDecl, _, _) = decodeExpansion(of: attribute, attachedTo: decl, in: context) else {
137-
return []
138-
}
139-
129+
attachedTo decl: some DeclGroupSyntax,
130+
providingExtensionsOf type: some TypeSyntaxProtocol,
131+
conformingTo protocols: [TypeSyntax],
132+
in context: some MacroExpansionContext
133+
) throws -> [ExtensionDeclSyntax] {
140134
// If there is an explicit conformance to OptionSet already, don't add one.
141-
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypes,
142-
inheritedTypes.contains(where: { inherited in inherited.type.trimmedDescription == "OptionSet" }) {
135+
if protocols.isEmpty {
143136
return []
144137
}
145138

146-
return [("OptionSet", nil)]
139+
let ext: DeclSyntax =
140+
"""
141+
extension \(type.trimmed): OptionSet {}
142+
"""
143+
144+
return [ext.cast(ExtensionDeclSyntax.self)]
147145
}
148146
}
149147

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,10 @@ function(add_swift_target_library_single target name)
10801080
target_include_directories("${target}" SYSTEM PRIVATE
10811081
${SWIFTLIB_INCLUDE})
10821082
endif()
1083+
if(libkind STREQUAL STATIC)
1084+
set_property(TARGET "${target}" PROPERTY
1085+
PREFIX lib)
1086+
endif()
10831087
endif()
10841088

10851089
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
@@ -2665,6 +2669,10 @@ function(_add_swift_target_executable_single name)
26652669
set_target_properties(${name} PROPERTIES
26662670
BUILD_WITH_INSTALL_RPATH YES
26672671
INSTALL_RPATH "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR};@executable_path/../../../lib/swift/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
2672+
elseif(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
2673+
set_target_properties(${name} PROPERTIES
2674+
BUILD_WITH_INSTALL_RPATH YES
2675+
INSTALL_RPATH "$ORIGIN/../../../lib/swift/${SWIFT_SDK_${SWIFTEXE_SINGLE_SDK}_LIB_SUBDIR}")
26682676
endif()
26692677
set_output_directory(${name}
26702678
BINARY_DIR ${SWIFT_RUNTIME_OUTPUT_INTDIR}

stdlib/public/Backtracing/Dwarf.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ struct DwarfReader<S: DwarfSource> {
15381538

15391539
var cursor = ImageSourceCursor(source: infoSection,
15401540
offset: abstractOrigin)
1541-
let abbrev = try cursor.readULEB128()
1541+
var abbrev = try cursor.readULEB128()
15421542
if abbrev == 0 {
15431543
return
15441544
}
@@ -1553,13 +1553,39 @@ struct DwarfReader<S: DwarfSource> {
15531553
return
15541554
}
15551555

1556-
let refAttrs = try readDieAttributes(
1556+
var refAttrs = try readDieAttributes(
15571557
at: &cursor,
15581558
unit: unit,
15591559
abbrevInfo: abbrevInfo,
15601560
shouldFetchIndirect: true
15611561
)
15621562

1563+
if let specificationVal = refAttrs[.DW_AT_specification],
1564+
case let .reference(specification) = specificationVal {
1565+
cursor = ImageSourceCursor(source: infoSection,
1566+
offset: specification)
1567+
abbrev = try cursor.readULEB128()
1568+
if abbrev == 0 {
1569+
return
1570+
}
1571+
1572+
guard let abbrevInfo = unit.abbrevs[abbrev] else {
1573+
throw DwarfError.missingAbbrev(abbrev)
1574+
}
1575+
1576+
let tag = abbrevInfo.tag
1577+
if tag != .DW_TAG_subprogram {
1578+
return
1579+
}
1580+
1581+
refAttrs = try readDieAttributes(
1582+
at: &cursor,
1583+
unit: unit,
1584+
abbrevInfo: abbrevInfo,
1585+
shouldFetchIndirect: true
1586+
)
1587+
}
1588+
15631589
var name: String? = nil
15641590
var rawName: String? = nil
15651591

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
3131
${SWIFT_SDK_LINUX_CXX_OVERLAY_SWIFT_COMPILE_FLAGS}
3232

3333
LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}"
34-
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX
34+
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX WINDOWS
3535
INSTALL_IN_COMPONENT compiler
3636
INSTALL_WITH_SHARED
3737
DEPENDS libstdcxx-modulemap libcxxshim_modulemap)

test/IRGen/debug_scope_distinct.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: differentiable_programming
2+
13
// RUN: %empty-directory(%t)
24
// RUN: %target-swiftc_driver -DM -emit-module -emit-module-path %t/M.swiftmodule %s -module-name M
35
// RUN: %target-swiftc_driver -O -g -I %t -c %s -emit-ir -o - | %FileCheck %s

test/Index/index_macros.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,16 @@ public struct SomeAccessorMacro: AccessorMacro {
207207
}
208208
}
209209

210-
public struct SomeConformanceMacro: ConformanceMacro {
210+
public struct SomeConformanceMacro: ExtensionMacro {
211211
public static func expansion(
212212
of node: AttributeSyntax,
213-
providingConformancesOf decl: some DeclGroupSyntax,
213+
attachedTo: some DeclGroupSyntax,
214+
providingExtensionsOf type: some TypeSyntaxProtocol,
215+
conformingTo protocols: [TypeSyntax],
214216
in context: some MacroExpansionContext
215-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
216-
let protocolName: TypeSyntax = "TestProto"
217-
return [(protocolName, nil)]
217+
) throws -> [ExtensionDeclSyntax] {
218+
let ext: DeclSyntax = "extension \(type.trimmed): TestProto {}"
219+
return [ext.cast(ExtensionDeclSyntax.self)]
218220
}
219221
}
220222

test/Interop/Cxx/stdlib/Inputs/std-map.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using Map = std::map<int, int>;
99
using MapStrings = std::map<std::string, std::string>;
10+
using NestedMap = std::map<int, Map>;
1011
using UnorderedMap = std::unordered_map<int, int>;
1112

1213
inline Map initMap() { return {{1, 3}, {2, 2}, {3, 3}}; }

test/Interop/Cxx/stdlib/use-std-map.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ StdMapTestSuite.test("MapStrings.subscript") {
5050
expectEqual(m[std.string("abc")], std.string("qwe"))
5151
}
5252

53+
StdMapTestSuite.test("NestedMap.subscript") {
54+
var m = NestedMap()
55+
expectNil(m[0])
56+
expectNil(m[0])
57+
m[1] = Map()
58+
expectNotNil(m[1])
59+
60+
expectNil(m[1]![0])
61+
m[1]![0] = 123
62+
expectEqual(m[1]![0], 123)
63+
64+
m[1]![0] = nil
65+
expectNil(m[1]![0])
66+
67+
m[1] = nil
68+
expectNil(m[1])
69+
}
70+
5371
StdMapTestSuite.test("UnorderedMap.subscript") {
5472
// This relies on the `std::unordered_map` conformance to `CxxDictionary` protocol.
5573
var m = initUnorderedMap()

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,14 +1281,16 @@ public struct EmptyPeerMacro: PeerMacro {
12811281
}
12821282
}
12831283

1284-
public struct EquatableMacro: ConformanceMacro {
1284+
public struct EquatableMacro: ExtensionMacro {
12851285
public static func expansion(
12861286
of node: AttributeSyntax,
1287-
providingConformancesOf decl: some DeclGroupSyntax,
1287+
attachedTo: some DeclGroupSyntax,
1288+
providingExtensionsOf type: some TypeSyntaxProtocol,
1289+
conformingTo protocols: [TypeSyntax],
12881290
in context: some MacroExpansionContext
1289-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1290-
let protocolName: TypeSyntax = "Equatable"
1291-
return [(protocolName, nil)]
1291+
) throws -> [ExtensionDeclSyntax] {
1292+
let ext: DeclSyntax = "extension \(type.trimmed): Equatable {}"
1293+
return [ext.cast(ExtensionDeclSyntax.self)]
12921294
}
12931295
}
12941296

@@ -1316,34 +1318,37 @@ public struct ConformanceViaExtensionMacro: ExtensionMacro {
13161318
}
13171319
}
13181320

1319-
public struct HashableMacro: ConformanceMacro {
1321+
public struct HashableMacro: ExtensionMacro {
13201322
public static func expansion(
13211323
of node: AttributeSyntax,
1322-
providingConformancesOf decl: some DeclGroupSyntax,
1324+
attachedTo: some DeclGroupSyntax,
1325+
providingExtensionsOf type: some TypeSyntaxProtocol,
1326+
conformingTo protocols: [TypeSyntax],
13231327
in context: some MacroExpansionContext
1324-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1325-
let protocolName: TypeSyntax = "Hashable"
1326-
return [(protocolName, nil)]
1328+
) throws -> [ExtensionDeclSyntax] {
1329+
let ext: DeclSyntax = "extension \(type.trimmed): Hashable {}"
1330+
return [ext.cast(ExtensionDeclSyntax.self)]
13271331
}
13281332
}
13291333

1330-
public struct DelegatedConformanceMacro: ConformanceMacro, MemberMacro {
1334+
public struct DelegatedConformanceMacro: ExtensionMacro, MemberMacro {
13311335
public static func expansion(
13321336
of node: AttributeSyntax,
1333-
providingConformancesOf decl: some DeclGroupSyntax,
1337+
attachedTo: some DeclGroupSyntax,
1338+
providingExtensionsOf type: some TypeSyntaxProtocol,
1339+
conformingTo protocols: [TypeSyntax],
13341340
in context: some MacroExpansionContext
1335-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1336-
let protocolName: TypeSyntax = "P"
1341+
) throws -> [ExtensionDeclSyntax] {
13371342
let conformance: DeclSyntax =
13381343
"""
1339-
extension Placeholder where Element: P {}
1344+
extension \(type.trimmed): P where Element: P {}
13401345
"""
13411346

13421347
guard let extensionDecl = conformance.as(ExtensionDeclSyntax.self) else {
13431348
return []
13441349
}
13451350

1346-
return [(protocolName, extensionDecl.genericWhereClause)]
1351+
return [extensionDecl]
13471352
}
13481353

13491354
public static func expansion(
@@ -1760,26 +1765,21 @@ public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
17601765
}
17611766
}
17621767

1763-
public struct InitializableMacro: ConformanceMacro, MemberMacro {
1768+
public struct InitializableMacro: ExtensionMacro {
17641769
public static func expansion(
17651770
of node: AttributeSyntax,
1766-
providingConformancesOf decl: some DeclGroupSyntax,
1767-
in context: some MacroExpansionContext
1768-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1769-
return [("Initializable", nil)]
1770-
}
1771-
1772-
public static func expansion(
1773-
of node: AttributeSyntax,
1774-
providingMembersOf decl: some DeclGroupSyntax,
1771+
attachedTo: some DeclGroupSyntax,
1772+
providingExtensionsOf type: some TypeSyntaxProtocol,
1773+
conformingTo protocols: [TypeSyntax],
17751774
in context: some MacroExpansionContext
1776-
) throws -> [DeclSyntax] {
1777-
let requirement: DeclSyntax =
1775+
) throws -> [ExtensionDeclSyntax] {
1776+
let ext: DeclSyntax =
17781777
"""
1779-
init(value: Int) {}
1778+
extension \(type.trimmed): Initializable {
1779+
init(value: Int) {}
1780+
}
17801781
"""
1781-
1782-
return [requirement]
1782+
return [ext.cast(ExtensionDeclSyntax.self)]
17831783
}
17841784
}
17851785

test/Macros/top_level_freestanding.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ macro Empty<T>(_ closure: () -> T) = #externalMacro(module: "MacroDefinition", t
117117
S(a: 10, b: 10)
118118
}
119119

120-
@attached(extension, conformances: Initializable)
121-
@attached(member, names: named(init))
120+
@attached(extension, conformances: Initializable, names: named(init))
122121
macro Initializable() = #externalMacro(module: "MacroDefinition", type: "InitializableMacro")
123122

124123
protocol Initializable {

0 commit comments

Comments
 (0)