Skip to content

Commit acb42e3

Browse files
Merge pull request #5511 from swiftwasm/katei/merge-main-2023-06-11
Merge main 2023-06-11
2 parents a77258f + f5f4b84 commit acb42e3

File tree

60 files changed

+441
-175
lines changed

Some content is hidden

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

60 files changed

+441
-175
lines changed

docs/ContinuousIntegration.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,18 @@ macOS platform | @swift-ci Please Sourcekit Stress test | Swift Sourcekit Stress
157157
Platform | Comment | Check Status
158158
------------ | ------- | ------------
159159
macOS platform | @swift-ci Please Build Toolchain macOS Platform| Swift Build Toolchain macOS Platform
160-
Linux platform | @swift-ci Please Build Toolchain Linux Platform| Swift Build Toolchain Linux Platform
160+
Linux platform | @swift-ci Please Build Toolchain Linux Platform| Swift Build Toolchain Ubuntu 22.04 (x86_64)
161+
162+
You can also build a toolchain for a specific Linux distribution
163+
164+
Distro | Comment | Check Status
165+
-------------- | ------------------------------------------------ | ----------------------------------------------
166+
UBI9 | @swift-ci Please Build Toolchain UBI9 | Swift Build Toolchain UBI9 (x86_64)
167+
CentOS 7 | @swift-ci Please Build Toolchain CentOS 7 | Swift Build Toolchain CentOS 7 (x86_64)
168+
Ubuntu 18.04 | @swift-ci Please Build Toolchain Ubuntu 18.04 | Swift Build Toolchain Ubuntu 18.04 (x86_64)
169+
Ubuntu 20.04 | @swift-ci Please Build Toolchain Ubuntu 20.04 | Swift Build Toolchain Ubuntu 20.04 (x86_64)
170+
Ubuntu 22.04 | @swift-ci Please Build Toolchain Ubuntu 22.04 | Swift Build Toolchain Ubuntu 22.04 (x86_64)
171+
Amazon Linux 2 | @swift-ci Please Build Toolchain Amazon Linux 2 | Swift Build Toolchain Amazon Linux 2 (x86_64)
161172

162173
### Build and Test Stdlib against Snapshot Toolchain
163174

@@ -275,9 +286,6 @@ Currently, supported pull request testing triggers:
275286
Platform | Comment | Check Status
276287
------------ | ------- | ------------
277288
Windows | @swift-ci Please test Windows platform | Swift Test Windows Platform
278-
Linux | @swift-ci Please test Tensorflow Linux platform | Swift Test Linux Platform (TensorFlow)
279-
Linux (GPU) | @swift-ci Please test Tensorflow Linux GPU platform |Swift Test Linux Platform with GPU (TensorFlow)
280-
macOS | @swift-ci Please test Tensorflow macOS platform | Swift Test macOS Platform (TensorFlow)
281289

282290
## ci.swift.org bots
283291

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ ERROR(no_candidates_match_argument_type,none,
275275
(StringRef, Type, unsigned))
276276

277277
ERROR(cannot_infer_closure_parameter_type,none,
278-
"unable to infer type of a closure parameter %0 in the current context",
278+
"cannot infer type of closure parameter %0 without a type annotation",
279279
(StringRef))
280280
ERROR(cannot_infer_closure_type,none,
281-
"unable to infer closure type in the current context", ())
281+
"unable to infer closure type without a type annotation", ())
282282
ERROR(cannot_infer_empty_closure_result_type,none,
283283
"cannot infer return type of empty closure", ())
284284
ERROR(cannot_infer_closure_result_type,none,
@@ -4163,7 +4163,7 @@ ERROR(could_not_infer_placeholder,none,
41634163
"could not infer type for placeholder", ())
41644164

41654165
ERROR(type_of_expression_is_ambiguous,none,
4166-
"type of expression is ambiguous without more context", ())
4166+
"type of expression is ambiguous without a type annotation", ())
41674167

41684168
ERROR(failed_to_produce_diagnostic,Fatal,
41694169
"failed to produce diagnostic for expression; "
@@ -7146,6 +7146,8 @@ NOTE(macro_remove_result_type,none,
71467146
())
71477147
NOTE(macro_make_freestanding_expression,none,
71487148
"make this macro a freestanding expression macro", ())
7149+
ERROR(macro_multiple_freestanding_roles,none,
7150+
"macro can only have a single freestanding role", ())
71497151
ERROR(macro_expansion_missing_pound,none,
71507152
"expansion of macro %0 requires leading '#'", (DeclName))
71517153
ERROR(macro_expansion_missing_arguments,none,

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,16 +4811,14 @@ void PrintAST::visitMacroDecl(MacroDecl *decl) {
48114811
}
48124812
);
48134813

4814-
{
4814+
if (decl->resultType.getTypeRepr() ||
4815+
!decl->getResultInterfaceType()->isVoid()) {
48154816
Printer.printStructurePre(PrintStructureKind::DeclResultTypeClause);
48164817
SWIFT_DEFER {
48174818
Printer.printStructurePost(PrintStructureKind::DeclResultTypeClause);
48184819
};
48194820

4820-
if (decl->parameterList)
4821-
Printer << " -> ";
4822-
else
4823-
Printer << ": ";
4821+
Printer << " -> ";
48244822

48254823
TypeLoc resultTypeLoc(
48264824
decl->resultType.getTypeRepr(), decl->getResultInterfaceType());

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ extension MacroRole {
6767
case 0x10: self = .member
6868
case 0x20: self = .peer
6969
case 0x40: self = .conformance
70+
case 0x80: self = .codeItem
71+
7072
default: fatalError("unknown macro role")
7173
}
7274
}
@@ -414,6 +416,7 @@ func expandFreestandingMacro(
414416
macroKind: UInt8,
415417
discriminatorText: UnsafePointer<UInt8>,
416418
discriminatorTextLength: Int,
419+
rawMacroRole: UInt8,
417420
sourceFilePtr: UnsafeRawPointer,
418421
sourceLocationPtr: UnsafePointer<UInt8>?,
419422
expandedSourcePointer: UnsafeMutablePointer<UnsafePointer<UInt8>?>,
@@ -446,18 +449,21 @@ func expandFreestandingMacro(
446449
)
447450
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)
448451

452+
let macroRole = MacroRole(rawMacroRole: rawMacroRole)
449453
let expandedSource: String?
450454
switch MacroPluginKind(rawValue: macroKind)! {
451455
case .InProcess:
452456
expandedSource = expandFreestandingMacroInProcess(
453457
macroPtr: macroPtr,
458+
macroRole: macroRole,
454459
diagEnginePtr: diagEnginePtr,
455460
expansionSyntax: expansion,
456461
sourceFilePtr: sourceFilePtr,
457462
discriminator: discriminator)
458463
case .Executable:
459464
expandedSource = expandFreestandingMacroIPC(
460465
macroPtr: macroPtr,
466+
macroRole: macroRole,
461467
diagEnginePtr: diagEnginePtr,
462468
expansionSyntax: expansion,
463469
sourceFilePtr: sourceFilePtr,
@@ -485,6 +491,7 @@ func expandFreestandingMacro(
485491

486492
func expandFreestandingMacroIPC(
487493
macroPtr: UnsafeRawPointer,
494+
macroRole: MacroRole,
488495
diagEnginePtr: UnsafeMutablePointer<UInt8>,
489496
expansionSyntax: FreestandingMacroExpansionSyntax,
490497
sourceFilePtr: UnsafePointer<ExportedSourceFile>,
@@ -502,9 +509,21 @@ func expandFreestandingMacroIPC(
502509

503510
let macro = macroPtr.assumingMemoryBound(to: ExportedExecutableMacro.self).pointee
504511

512+
// Map the macro role.
513+
let pluginMacroRole: PluginMessage.MacroRole
514+
switch macroRole {
515+
case .accessor, .member, .memberAttribute, .peer, .conformance:
516+
preconditionFailure("unhandled macro role for freestanding macro")
517+
518+
case .expression: pluginMacroRole = .expression
519+
case .declaration: pluginMacroRole = .freeStandingDeclaration
520+
case .codeItem: pluginMacroRole = .codeItem
521+
}
522+
505523
// Send the message.
506524
let message = HostToPluginMessage.expandFreestandingMacro(
507525
macro: .init(moduleName: macro.moduleName, typeName: macro.typeName, name: macroName),
526+
macroRole: pluginMacroRole,
508527
discriminator: discriminator,
509528
syntax: PluginMessage.Syntax(syntax: Syntax(expansionSyntax), in: sourceFilePtr)!)
510529
do {
@@ -541,6 +560,7 @@ func expandFreestandingMacroIPC(
541560

542561
func expandFreestandingMacroInProcess(
543562
macroPtr: UnsafeRawPointer,
563+
macroRole: MacroRole,
544564
diagEnginePtr: UnsafeMutablePointer<UInt8>,
545565
expansionSyntax: FreestandingMacroExpansionSyntax,
546566
sourceFilePtr: UnsafePointer<ExportedSourceFile>,
@@ -580,6 +600,7 @@ func expandFreestandingMacroInProcess(
580600

581601
return SwiftSyntaxMacroExpansion.expandFreestandingMacro(
582602
definition: macro,
603+
macroRole: macroRole,
583604
node: node,
584605
in: context
585606
)

lib/ASTGen/Sources/ASTGen/PluginMessages.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal enum HostToPluginMessage: Codable {
2020
/// Expand a '@freestanding' macro.
2121
case expandFreestandingMacro(
2222
macro: PluginMessage.MacroReference,
23+
macroRole: PluginMessage.MacroRole? = nil,
2324
discriminator: String,
2425
syntax: PluginMessage.Syntax
2526
)
@@ -91,6 +92,7 @@ internal enum PluginToHostMessage: Codable {
9192
case member
9293
case peer
9394
case conformance
95+
case codeItem
9496
}
9597

9698
struct SourceLocation: Codable {

lib/IDETool/SyntacticMacroExpansion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void SyntacticMacroExpansionInstance::expand(
412412
SourceFile *SF, const MacroExpansionSpecifier &expansion,
413413
SourceEditConsumer &consumer) {
414414

415-
// Find the expansion at 'expantion.offset'.
415+
// Find the expansion at 'expansion.offset'.
416416
MacroExpansionFinder expansionFinder(
417417
SourceMgr,
418418
SourceMgr.getLocForOffset(*SF->getBufferID(), expansion.offset));

lib/IRGen/GenType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,9 +2897,11 @@ static bool tryEmitDeinitCall(IRGenFunction &IGF,
28972897
return false;
28982898
}
28992899

2900-
auto deinitTable = IGF.getSILModule().lookUpMoveOnlyDeinit(nominal);
2900+
auto deinitTable = IGF.getSILModule().lookUpMoveOnlyDeinit(
2901+
nominal, false /*deserialize lazily*/);
29012902

2902-
// If we do not have a deinit table, call the value witness instead.
2903+
// If we do not have a deinit table already deserialized, call the value
2904+
// witness instead.
29032905
if (!deinitTable) {
29042906
irgen::emitDestroyCall(IGF, T, indirect());
29052907
indirectCleanup();

lib/Macros/Sources/ObservationMacros/ObservableMacro.swift

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ import SwiftSyntaxMacros
1717
@_implementationOnly import SwiftSyntaxBuilder
1818

1919
public struct ObservableMacro {
20+
static let moduleName = "_Observation"
21+
22+
static let conformanceName = "Observable"
23+
static var qualifiedConformanceName: String {
24+
return "\(moduleName).\(conformanceName)"
25+
}
26+
27+
static var observableConformanceType: TypeSyntax {
28+
"\(raw: qualifiedConformanceName)"
29+
}
30+
31+
static let registrarTypeName = "ObservationRegistrar"
32+
static var qualifiedRegistrarTypeName: String {
33+
return "\(moduleName).\(registrarTypeName)"
34+
}
35+
2036
static let trackedMacroName = "ObservationTracked"
2137
static let ignoredMacroName = "ObservationIgnored"
2238

@@ -25,7 +41,7 @@ public struct ObservableMacro {
2541
static func registrarVariable(_ observableType: TokenSyntax) -> DeclSyntax {
2642
return
2743
"""
28-
@\(raw: ignoredMacroName) private let \(raw: registrarVariableName) = ObservationRegistrar()
44+
@\(raw: ignoredMacroName) private let \(raw: registrarVariableName) = \(raw: qualifiedRegistrarTypeName)()
2945
"""
3046
}
3147

@@ -202,17 +218,15 @@ extension ObservableMacro: MemberMacro {
202218
declaration.addIfNeeded(ObservableMacro.registrarVariable(observableType), to: &declarations)
203219
declaration.addIfNeeded(ObservableMacro.accessFunction(observableType), to: &declarations)
204220
declaration.addIfNeeded(ObservableMacro.withMutationFunction(observableType), to: &declarations)
205-
221+
222+
#if !OBSERVATION_SUPPORTS_PEER_MACROS
206223
let storedInstanceVariables = declaration.definedVariables.filter { $0.isValidForObservation }
207224
for property in storedInstanceVariables {
208-
if property.hasMacroApplication(ObservableMacro.ignoredMacroName) { continue }
209-
if property.initializer == nil {
210-
context.addDiagnostics(from: DiagnosticsError(syntax: property, message: "@Observable requires property '\(property.identifier?.text ?? "")' to have an initial value", id: .missingInitializer), node: property)
211-
}
212-
let storage = DeclSyntax(property.privatePrefixed("_", addingAttribute: ObservableMacro.ignoredAttribute))
213-
declaration.addIfNeeded(storage, to: &declarations)
214-
225+
if property.hasMacroApplication(ObservableMacro.ignoredMacroName) { continue }
226+
let storage = DeclSyntax(property.privatePrefixed("_", addingAttribute: ObservableMacro.ignoredAttribute))
227+
declaration.addIfNeeded(storage, to: &declarations)
215228
}
229+
#endif
216230

217231
return declarations
218232
}
@@ -264,13 +278,13 @@ extension ObservableMacro: ConformanceMacro {
264278

265279
if let inheritanceList {
266280
for inheritance in inheritanceList {
267-
if inheritance.typeName.identifier == "Observable" {
281+
if inheritance.typeName.identifier == ObservableMacro.conformanceName {
268282
return []
269283
}
270284
}
271285
}
272286

273-
return [("Observable", nil)]
287+
return [(ObservableMacro.observableConformanceType, nil)]
274288
}
275289
}
276290

@@ -293,6 +307,13 @@ public struct ObservationTrackedMacro: AccessorMacro {
293307
return []
294308
}
295309

310+
let initAccessor: AccessorDeclSyntax =
311+
"""
312+
init(initialValue) initializes(_\(identifier)) {
313+
_\(identifier) = initialValue
314+
}
315+
"""
316+
296317
let getAccessor: AccessorDeclSyntax =
297318
"""
298319
get {
@@ -310,7 +331,7 @@ public struct ObservationTrackedMacro: AccessorMacro {
310331
}
311332
"""
312333

313-
return [getAccessor, setAccessor]
334+
return [initAccessor, getAccessor, setAccessor]
314335
}
315336
}
316337

@@ -327,8 +348,9 @@ extension ObservationTrackedMacro: PeerMacro {
327348
property.isValidForObservation else {
328349
return []
329350
}
330-
331-
if property.hasMacroApplication(ObservableMacro.ignoredMacroName) {
351+
352+
if property.hasMacroApplication(ObservableMacro.ignoredMacroName) ||
353+
property.hasMacroApplication(ObservableMacro.trackedMacroName) {
332354
return []
333355
}
334356

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,17 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20172017
llvm_unreachable("should always be type-checked already");
20182018
}
20192019

2020+
/// Determine the number of bits set.
2021+
static unsigned numBitsSet(uint64_t value) {
2022+
unsigned count = 0;
2023+
for (uint64_t i : range(0, 63)) {
2024+
if (value & (uint64_t(1) << i))
2025+
++count;
2026+
}
2027+
2028+
return count;
2029+
}
2030+
20202031
void visitMacroDecl(MacroDecl *MD) {
20212032
TypeChecker::checkDeclAttributes(MD);
20222033
checkAccessControl(MD);
@@ -2061,22 +2072,39 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
20612072
}
20622073
}
20632074

2064-
// If the macro has a (non-Void) result type, it must have the freestanding
2075+
// If the macro has a result type, it must have the freestanding
20652076
// expression role. Other roles cannot have result types.
20662077
if (auto resultTypeRepr = MD->getResultTypeRepr()) {
2067-
if (!MD->getMacroRoles().contains(MacroRole::Expression) &&
2068-
!MD->getResultInterfaceType()->isEqual(Ctx.getVoidType())) {
2069-
auto resultType = MD->getResultInterfaceType();
2070-
Ctx.Diags.diagnose(
2071-
MD->arrowLoc, diag::macro_result_type_cannot_be_used, resultType)
2072-
.highlight(resultTypeRepr->getSourceRange());
2078+
if (!MD->getMacroRoles().contains(MacroRole::Expression)) {
2079+
auto resultType = MD->getResultInterfaceType(); {
2080+
auto diag = Ctx.Diags.diagnose(
2081+
MD->arrowLoc, diag::macro_result_type_cannot_be_used, resultType);
2082+
diag.highlight(resultTypeRepr->getSourceRange());
2083+
2084+
// In a .swiftinterface file, downgrade this diagnostic to a warning.
2085+
// This allows the compiler to process existing .swiftinterface
2086+
// files that contain this issue.
2087+
if (resultType->isVoid()) {
2088+
if (auto sourceFile = MD->getParentSourceFile())
2089+
if (sourceFile->Kind == SourceFileKind::Interface)
2090+
diag.limitBehavior(DiagnosticBehavior::Warning);
2091+
}
2092+
}
2093+
20732094
Ctx.Diags.diagnose(MD->arrowLoc, diag::macro_make_freestanding_expression)
20742095
.fixItInsert(MD->getAttributeInsertionLoc(false),
20752096
"@freestanding(expression)\n");
20762097
Ctx.Diags.diagnose(MD->arrowLoc, diag::macro_remove_result_type)
20772098
.fixItRemove(SourceRange(MD->arrowLoc, resultTypeRepr->getEndLoc()));
20782099
}
20792100
}
2101+
2102+
// A macro can only have a single freestanding macro role.
2103+
MacroRoles freestandingRolesInhabited =
2104+
MD->getMacroRoles() & getFreestandingMacroRoles();
2105+
if (numBitsSet(freestandingRolesInhabited.toRaw()) > 1) {
2106+
MD->diagnose(diag::macro_multiple_freestanding_roles);
2107+
}
20802108
}
20812109

20822110
void visitMacroExpansionDecl(MacroExpansionDecl *MED) {

0 commit comments

Comments
 (0)