Skip to content

Commit 113bf4e

Browse files
Merge pull request #5525 from swiftwasm/main
[pull] swiftwasm from main
2 parents 49ef8a7 + 056ac83 commit 113bf4e

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

+1571
-179
lines changed

include/swift/AST/DeclContext.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace swift {
8282
class SerializedDefaultArgumentInitializer;
8383
class SerializedTopLevelCodeDecl;
8484
class StructDecl;
85+
class AccessorDecl;
8586

8687
namespace serialization {
8788
using DeclID = llvm::PointerEmbeddedInt<unsigned, 31>;
@@ -457,6 +458,18 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
457458
return const_cast<DeclContext*>(this)->getInnermostMethodContext();
458459
}
459460

461+
/// Returns the innermost accessor context that belongs to a property.
462+
///
463+
/// This routine looks through closure, initializer, and local function
464+
/// contexts to find the innermost accessor declaration.
465+
///
466+
/// \returns the innermost accessor, or null if there is no such context.
467+
LLVM_READONLY
468+
AccessorDecl *getInnermostPropertyAccessorContext();
469+
const AccessorDecl *getInnermostPropertyAccessorContext() const {
470+
return const_cast<DeclContext*>(this)->getInnermostPropertyAccessorContext();
471+
}
472+
460473
/// Returns the innermost type context.
461474
///
462475
/// This routine looks through closure, initializer, and local function

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ NOTE(sdk_version_pbm_version,none,
437437
NOTE(compiled_module_ignored_reason,none,
438438
"compiled module '%0' was ignored because %select{%error"
439439
"|it belongs to a framework in the SDK"
440-
"|loading from module interfaces is preferred}1",
440+
"|loading from module interfaces is preferred"
441+
"|it's a compiler host module}1",
441442
(StringRef, unsigned))
442443
NOTE(out_of_date_module_here,none,
443444
"%select{compiled|cached|forwarding|prebuilt}0 module is out of date: '%1'",

include/swift/AST/DiagnosticsSema.def

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,11 @@ ERROR(attr_objc_implementation_category_not_found,none,
16761676
NOTE(attr_objc_implementation_fixit_remove_category_name,none,
16771677
"remove arguments to implement the main '@interface' for this class",
16781678
())
1679+
ERROR(attr_objc_implementation_no_conformance,none,
1680+
"'@_objcImplementation' extension cannot add conformance to %0; "
1681+
"add this conformance %select{with an ordinary extension|"
1682+
"in the Objective-C header}1",
1683+
(Type, bool))
16791684

16801685
ERROR(member_of_objc_implementation_not_objc_or_final,none,
16811686
"%0 %1 does not match any %0 declared in the headers for %2; did you use "
@@ -7364,7 +7369,22 @@ ERROR(init_accessor_accesses_attribute_on_other_declaration,none,
73647369
ERROR(init_accessor_property_both_init_and_accessed,none,
73657370
"property %0 cannot be both initialized and accessed",
73667371
(DeclName))
7367-
7372+
ERROR(invalid_use_of_self_in_init_accessor,none,
7373+
"'self' within init accessors can only be used to reference "
7374+
"properties listed in 'initializes' and 'accesses'; "
7375+
"init accessors are run before 'self' is fully available", ())
7376+
ERROR(init_accessor_invalid_member_ref,none,
7377+
"cannot reference instance member %0; init accessors can only "
7378+
"refer to instance properties listed in 'initializes' and "
7379+
"'accesses' attributes",
7380+
(DeclNameRef))
7381+
ERROR(cannot_synthesize_memberwise_due_to_property_init_order,none,
7382+
"cannot synthesize memberwise initializer",
7383+
())
7384+
NOTE(out_of_order_access_in_init_accessor,none,
7385+
"init accessor for %0 cannot access stored property %1 because it "
7386+
"is called before %1 is initialized",
7387+
(Identifier, Identifier))
73687388

73697389
#define UNDEFINE_DIAGNOSTIC_MACROS
73707390
#include "DefineDiagnosticMacros.h"

include/swift/AST/TypeMatcher.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,12 @@ class TypeMatcher {
438438

439439
if (firstArgs.size() == secondArgs.size()) {
440440
for (unsigned i : indices(firstArgs)) {
441-
return this->visit(CanType(firstArgs[i]),
442-
secondArgs[i],
443-
sugaredFirstType->castTo<ParameterizedProtocolType>()
444-
->getArgs()[i]);
441+
if (!this->visit(CanType(firstArgs[i]),
442+
secondArgs[i],
443+
sugaredFirstType->castTo<ParameterizedProtocolType>()
444+
->getArgs()[i])) {
445+
return false;
446+
}
445447
}
446448

447449
return true;

include/swift/Basic/StringExtras.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ class NullTerminatedStringRef {
519519
/// where escaped Unicode characters lead to malformed/invalid JSON.
520520
void writeEscaped(llvm::StringRef Str, llvm::raw_ostream &OS);
521521

522+
/// Whether the path components of `path` begin with those from `prefix`.
523+
bool pathStartsWith(StringRef prefix, StringRef path);
524+
522525
} // end namespace swift
523526

524527
#endif // SWIFT_BASIC_STRINGEXTRAS_H
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===--- GenericMetadataCacheEntry.h ----------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Declares a struct that mirrors the layout of GenericCacheEntry in
14+
// Metadata.cpp and use a static assert to check that the offset of
15+
// the member Value match between the two.
16+
//
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_REFLECTION_GENERICMETADATACACHEENTRY_H
20+
#define SWIFT_REFLECTION_GENERICMETADATACACHEENTRY_H
21+
22+
#include <cstdint>
23+
24+
namespace swift {
25+
26+
template<typename StoredPointer>
27+
struct GenericMetadataCacheEntry {
28+
StoredPointer TrackingInfo;
29+
uint16_t NumKeyParameters;
30+
uint16_t NumWitnessTables;
31+
uint16_t NumPacks;
32+
uint16_t NumShapeClasses;
33+
StoredPointer PackShapeDescriptors;
34+
uint32_t Hash;
35+
StoredPointer Value;
36+
};
37+
38+
} // namespace swift
39+
40+
#endif // SWIFT_REFLECTION_GENERICMETADATACACHEENTRY_H

include/swift/RemoteInspection/ReflectionContext.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/Concurrency/Actor.h"
3131
#include "swift/Remote/MemoryReader.h"
3232
#include "swift/Remote/MetadataReader.h"
33+
#include "swift/RemoteInspection/GenericMetadataCacheEntry.h"
3334
#include "swift/RemoteInspection/Records.h"
3435
#include "swift/RemoteInspection/RuntimeInternals.h"
3536
#include "swift/RemoteInspection/TypeLowering.h"
@@ -1289,22 +1290,14 @@ class ReflectionContext
12891290
StoredPointer allocationMetadataPointer(
12901291
MetadataAllocation<Runtime> Allocation) {
12911292
if (Allocation.Tag == GenericMetadataCacheTag) {
1292-
struct GenericMetadataCacheEntry {
1293-
StoredPointer LockedStorage;
1294-
uint8_t LockedStorageKind;
1295-
uint8_t TrackingInfo;
1296-
uint16_t NumKeyParameters;
1297-
uint16_t NumWitnessTables;
1298-
uint32_t Hash;
1299-
StoredPointer Value;
1300-
};
13011293
auto AllocationBytes =
13021294
getReader().readBytes(RemoteAddress(Allocation.Ptr),
13031295
Allocation.Size);
13041296
if (!AllocationBytes)
13051297
return 0;
1306-
auto Entry = reinterpret_cast<const GenericMetadataCacheEntry *>(
1307-
AllocationBytes.get());
1298+
auto Entry =
1299+
reinterpret_cast<const GenericMetadataCacheEntry<StoredPointer> *>(
1300+
AllocationBytes.get());
13081301
return Entry->Value;
13091302
}
13101303
return 0;

include/swift/Runtime/Enum.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ void swift_initEnumMetadataMultiPayload(EnumMetadata *enumType,
111111
unsigned numPayloads,
112112
const TypeLayout * const *payloadTypes);
113113

114+
SWIFT_RUNTIME_EXPORT
115+
void swift_initEnumMetadataMultiPayloadWithLayoutString(EnumMetadata *enumType,
116+
EnumLayoutFlags flags,
117+
unsigned numPayloads,
118+
const Metadata * const *payloadTypes);
119+
114120
/// Return an integer value representing which case of a multi-payload
115121
/// enum is inhabited.
116122
///

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,8 @@ FUNCTION(InitStructMetadata,
12761276
// void swift_initStructMetadataWithLayoutString(Metadata *structType,
12771277
// StructLayoutFlags flags,
12781278
// size_t numFields,
1279-
// Metadata * const *fieldTypes,
1279+
// uint8_t * const *fieldTypes,
1280+
// const uint8_t *fieldTags
12801281
// uint32_t *fieldOffsets);
12811282
FUNCTION(InitStructMetadataWithLayoutString,
12821283
swift_initStructMetadataWithLayoutString, C_CC, AlwaysAvailable,
@@ -1312,6 +1313,7 @@ FUNCTION(InitEnumMetadataSinglePayload,
13121313
EFFECT(MetaData))
13131314

13141315
// void swift_initEnumMetadataMultiPayload(Metadata *enumType,
1316+
// EnumLayoutFlags layoutFlags,
13151317
// size_t numPayloads,
13161318
// TypeLayout * const *payloadTypes);
13171319
FUNCTION(InitEnumMetadataMultiPayload,
@@ -1322,6 +1324,19 @@ FUNCTION(InitEnumMetadataMultiPayload,
13221324
ATTRS(NoUnwind, WillReturn),
13231325
EFFECT(MetaData))
13241326

1327+
// void
1328+
// swift_initEnumMetadataMultiPayloadWithLayoutString(Metadata *enumType,
1329+
// EnumLayoutFlags layoutFlags,
1330+
// size_t numPayloads,
1331+
// Metadata * const *payloadTypes);
1332+
FUNCTION(InitEnumMetadataMultiPayloadWithLayoutString,
1333+
swift_initEnumMetadataMultiPayloadWithLayoutString,
1334+
C_CC, AlwaysAvailable,
1335+
RETURNS(VoidTy),
1336+
ARGS(TypeMetadataPtrTy, SizeTy, SizeTy, TypeMetadataPtrPtrTy),
1337+
ATTRS(NoUnwind, WillReturn),
1338+
EFFECT(MetaData))
1339+
13251340
// int swift_getEnumCaseMultiPayload(opaque_t *obj, Metadata *enumTy);
13261341
FUNCTION(GetEnumCaseMultiPayload,
13271342
swift_getEnumCaseMultiPayload,

include/swift/Sema/CSFix.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ enum class FixKind : uint8_t {
447447

448448
/// Ignore missing 'each' keyword before value pack reference.
449449
IgnoreMissingEachKeyword,
450+
451+
/// Ignore the fact that member couldn't be referenced within init accessor
452+
/// because its name doesn't appear in 'initializes' or 'accesses' attributes.
453+
AllowInvalidMemberReferenceInInitAccessor,
450454
};
451455

452456
class ConstraintFix {
@@ -3536,6 +3540,39 @@ class IgnoreMissingEachKeyword final : public ConstraintFix {
35363540
}
35373541
};
35383542

3543+
class AllowInvalidMemberReferenceInInitAccessor final : public ConstraintFix {
3544+
DeclNameRef MemberName;
3545+
3546+
AllowInvalidMemberReferenceInInitAccessor(ConstraintSystem &cs,
3547+
DeclNameRef memberName,
3548+
ConstraintLocator *locator)
3549+
: ConstraintFix(cs, FixKind::AllowInvalidMemberReferenceInInitAccessor,
3550+
locator),
3551+
MemberName(memberName) {}
3552+
3553+
public:
3554+
std::string getName() const override {
3555+
llvm::SmallVector<char, 16> scratch;
3556+
auto memberName = MemberName.getString(scratch);
3557+
return "allow reference to member '" + memberName.str() +
3558+
"' in init accessor";
3559+
}
3560+
3561+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3562+
3563+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
3564+
return diagnose(*commonFixes.front().first);
3565+
}
3566+
3567+
static AllowInvalidMemberReferenceInInitAccessor *
3568+
create(ConstraintSystem &cs, DeclNameRef memberName,
3569+
ConstraintLocator *locator);
3570+
3571+
static bool classof(const ConstraintFix *fix) {
3572+
return fix->getKind() == FixKind::AllowInvalidMemberReferenceInInitAccessor;
3573+
}
3574+
};
3575+
35393576
} // end namespace constraints
35403577
} // end namespace swift
35413578

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,11 @@ struct MemberLookupResult {
18791879
/// This is a static member being access through a protocol metatype
18801880
/// but its result type doesn't conform to this protocol.
18811881
UR_InvalidStaticMemberOnProtocolMetatype,
1882+
1883+
/// This is a member that doesn't appear in 'initializes' and/or
1884+
/// 'accesses' attributes of the init accessor and therefore canno
1885+
/// t be referenced in its body.
1886+
UR_UnavailableWithinInitAccessor,
18821887
};
18831888

18841889
/// This is a list of considered (but rejected) candidates, along with a

lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7168,6 +7168,12 @@ bool VarDecl::isMemberwiseInitialized(bool preferDeclaredProperties) const {
71687168
isBackingStorageForDeclaredProperty(this))
71697169
return false;
71707170

7171+
// If this is a computed property with `init` accessor, it's
7172+
// memberwise initializable when it could be used to initialize
7173+
// other stored properties.
7174+
if (auto *init = getAccessor(AccessorKind::Init))
7175+
return init->getAttrs().hasAttribute<InitializesAttr>();
7176+
71717177
// If this is a computed property, it's not memberwise initialized unless
71727178
// the caller has asked for the declared properties and it is either a
71737179
// `lazy` property or a property with an attached wrapper.

lib/AST/DeclContext.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,24 @@ AbstractFunctionDecl *DeclContext::getInnermostMethodContext() {
211211
return nullptr;
212212
}
213213

214+
AccessorDecl *DeclContext::getInnermostPropertyAccessorContext() {
215+
auto dc = this;
216+
do {
217+
if (auto decl = dc->getAsDecl()) {
218+
auto accessor = dyn_cast<AccessorDecl>(decl);
219+
// If we found a non-accessor decl, we're done.
220+
if (accessor == nullptr)
221+
return nullptr;
222+
223+
auto *storage = accessor->getStorage();
224+
if (isa<VarDecl>(storage) && storage->getDeclContext()->isTypeContext())
225+
return accessor;
226+
}
227+
} while ((dc = dc->getParent()));
228+
229+
return nullptr;
230+
}
231+
214232
bool DeclContext::isTypeContext() const {
215233
if (auto decl = getAsDecl())
216234
return isa<NominalTypeDecl>(decl) || isa<ExtensionDecl>(decl);

lib/AST/Module.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "swift/Basic/Compiler.h"
4444
#include "swift/Basic/SourceManager.h"
4545
#include "swift/Basic/Statistic.h"
46+
#include "swift/Basic/StringExtras.h"
4647
#include "swift/Demangling/ManglingMacros.h"
4748
#include "swift/Parse/Token.h"
4849
#include "swift/Strings.h"
@@ -4235,18 +4236,6 @@ FrontendStatsTracer::getTraceFormatter<const SourceFile *>() {
42354236
return &TF;
42364237
}
42374238

4238-
static bool prefixMatches(StringRef prefix, StringRef path) {
4239-
auto prefixIt = llvm::sys::path::begin(prefix),
4240-
prefixEnd = llvm::sys::path::end(prefix);
4241-
for (auto pathIt = llvm::sys::path::begin(path),
4242-
pathEnd = llvm::sys::path::end(path);
4243-
prefixIt != prefixEnd && pathIt != pathEnd; ++prefixIt, ++pathIt) {
4244-
if (*prefixIt != *pathIt)
4245-
return false;
4246-
}
4247-
return prefixIt == prefixEnd;
4248-
}
4249-
42504239
bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) const {
42514240
// stdlib is non-user by definition
42524241
if (mod->isStdlibModule())
@@ -4274,5 +4263,6 @@ bool IsNonUserModuleRequest::evaluate(Evaluator &evaluator, ModuleDecl *mod) con
42744263
return false;
42754264

42764265
StringRef runtimePath = searchPathOpts.RuntimeResourcePath;
4277-
return (!runtimePath.empty() && prefixMatches(runtimePath, modulePath)) || (!sdkPath.empty() && prefixMatches(sdkPath, modulePath));
4266+
return (!runtimePath.empty() && pathStartsWith(runtimePath, modulePath)) ||
4267+
(!sdkPath.empty() && pathStartsWith(sdkPath, modulePath));
42784268
}

lib/Basic/StringExtras.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/SmallVector.h"
2424
#include "llvm/ADT/StringSwitch.h"
2525
#include "llvm/Support/Compiler.h"
26+
#include "llvm/Support/Path.h"
2627
#include "llvm/Support/raw_ostream.h"
2728
#include <algorithm>
2829

@@ -1417,3 +1418,15 @@ void swift::writeEscaped(llvm::StringRef Str, llvm::raw_ostream &OS) {
14171418
}
14181419
}
14191420
}
1421+
1422+
bool swift::pathStartsWith(StringRef prefix, StringRef path) {
1423+
auto prefixIt = llvm::sys::path::begin(prefix),
1424+
prefixEnd = llvm::sys::path::end(prefix);
1425+
for (auto pathIt = llvm::sys::path::begin(path),
1426+
pathEnd = llvm::sys::path::end(path);
1427+
prefixIt != prefixEnd && pathIt != pathEnd; ++prefixIt, ++pathIt) {
1428+
if (*prefixIt != *pathIt)
1429+
return false;
1430+
}
1431+
return prefixIt == prefixEnd;
1432+
}

0 commit comments

Comments
 (0)