Skip to content

Commit d302532

Browse files
Merge pull request #4282 from swiftwasm/main
[pull] swiftwasm from main
2 parents 065e752 + 036d78f commit d302532

Some content is hidden

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

47 files changed

+464
-808
lines changed

include/swift-c/SyntaxParser/SwiftSyntaxCDataTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

include/swift/AST/Decl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4402,10 +4402,10 @@ class ProtocolDecl final : public NominalTypeDecl {
44024402
/// a protocol having nested types (ObjC protocols).
44034403
ArrayRef<AssociatedTypeDecl *> getAssociatedTypeMembers() const;
44044404

4405-
/// Returns the primary associated type, or nullptr if there isn't one. This is
4406-
/// the associated type that is parametrized with a same-type requirement in a
4407-
/// parametrized protocol type of the form SomeProtocol<SomeArgType>.
4408-
AssociatedTypeDecl *getPrimaryAssociatedType() const;
4405+
/// Returns the list of primary associated types. These are the associated
4406+
/// types that is parametrized with same-type requirements in a
4407+
/// parametrized protocol type of the form SomeProtocol<Arg1, Arg2...>.
4408+
ArrayRef<AssociatedTypeDecl *> getPrimaryAssociatedTypes() const;
44094409

44104410
/// Returns a protocol requirement with the given name, or nullptr if the
44114411
/// name has multiple overloads, or no overloads at all.

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,11 +3734,13 @@ ERROR(not_a_generic_definition,none,
37343734
ERROR(not_a_generic_type,none,
37353735
"cannot specialize non-generic type %0", (Type))
37363736
ERROR(parameterized_protocol_not_supported,none,
3737-
"protocol type with generic argument can only be used as a generic constraint", ())
3737+
"protocol type with generic arguments can only be used as a generic constraint", ())
37383738
ERROR(protocol_does_not_have_primary_assoc_type,none,
37393739
"cannot specialize protocol type %0", (Type))
3740-
ERROR(protocol_cannot_have_multiple_generic_arguments,none,
3741-
"protocol type %0 can only be specialized with exactly one argument", (Type))
3740+
ERROR(parameterized_protocol_too_many_type_arguments,none,
3741+
"protocol type %0 can specialized with too many type parameters "
3742+
"(got %1, but expected at most %2)",
3743+
(Type, unsigned, unsigned))
37423744
ERROR(cannot_specialize_self,none,
37433745
"cannot specialize 'Self'", ())
37443746
NOTE(specialize_explicit_type_instead,none,

include/swift/AST/ExistentialLayout.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ namespace swift {
2626
class ProtocolType;
2727
class ProtocolCompositionType;
2828

29-
struct PrimaryAssociatedTypeRequirement {
30-
AssociatedTypeDecl *AssocType;
31-
Type Argument;
32-
};
33-
3429
struct ExistentialLayout {
3530
enum Kind { Class, Error, Opaque };
3631

@@ -117,7 +112,7 @@ struct ExistentialLayout {
117112

118113
/// Zero or more primary associated type requirements from a
119114
/// ParameterizedProtocolType
120-
ArrayRef<PrimaryAssociatedTypeRequirement> sameTypeRequirements;
115+
ArrayRef<Type> sameTypeRequirements;
121116
};
122117

123118
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,10 @@ class ExistentialRequiresAnyRequest :
313313
void cacheResult(bool value) const;
314314
};
315315

316-
/// Find the primary associated type of the given protocol.
317-
class PrimaryAssociatedTypeRequest :
318-
public SimpleRequest<PrimaryAssociatedTypeRequest,
319-
AssociatedTypeDecl *(ProtocolDecl *),
316+
/// Find the list of primary associated types of the given protocol.
317+
class PrimaryAssociatedTypesRequest :
318+
public SimpleRequest<PrimaryAssociatedTypesRequest,
319+
ArrayRef<AssociatedTypeDecl *>(ProtocolDecl *),
320320
RequestFlags::Cached> {
321321
public:
322322
using SimpleRequest::SimpleRequest;
@@ -325,7 +325,7 @@ class PrimaryAssociatedTypeRequest :
325325
friend SimpleRequest;
326326

327327
// Evaluation.
328-
AssociatedTypeDecl *
328+
ArrayRef<AssociatedTypeDecl *>
329329
evaluate(Evaluator &evaluator, ProtocolDecl *decl) const;
330330

331331
public:

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ SWIFT_REQUEST(TypeChecker, PropertyWrapperTypeInfoRequest,
262262
NoLocationInfo)
263263
SWIFT_REQUEST(TypeChecker, ProtocolRequiresClassRequest, bool(ProtocolDecl *),
264264
SeparatelyCached, NoLocationInfo)
265-
SWIFT_REQUEST(TypeChecker, PrimaryAssociatedTypeRequest,
266-
AssociatedTypeDecl *(ProtocolDecl *),
265+
SWIFT_REQUEST(TypeChecker, PrimaryAssociatedTypesRequest,
266+
ArrayRef<AssociatedTypeDecl *>(ProtocolDecl *),
267267
Cached, NoLocationInfo)
268268
SWIFT_REQUEST(TypeChecker, RequirementRequest,
269269
Requirement(WhereClauseOwner, unsigned, TypeResolutionStage),

include/swift/AST/TypeDifferenceVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ class CanTypeDifferenceVisitor : public CanTypePairVisitor<Impl, bool> {
344344
if (asImpl().visit(type1.getBaseType(), type2.getBaseType()))
345345
return true;
346346

347-
return asImpl().visit(type1.getArgumentType(),
348-
type2.getArgumentType());
347+
return visitComponentArray(type1, type2,
348+
type1->getArgs(), type2->getArgs());
349349
}
350350

351351
bool visitExistentialType(CanExistentialType type1,

include/swift/AST/TypeMatcher.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,19 @@ class TypeMatcher {
342342
return false;
343343
}
344344

345-
return this->visit(firstParametrizedProto.getArgumentType(),
346-
secondParametrizedProto->getArgumentType(),
347-
sugaredFirstType->castTo<ParameterizedProtocolType>()
348-
->getArgumentType());
345+
auto firstArgs = firstParametrizedProto->getArgs();
346+
auto secondArgs = secondParametrizedProto->getArgs();
347+
348+
if (firstArgs.size() == secondArgs.size()) {
349+
for (unsigned i : indices(firstArgs)) {
350+
return this->visit(CanType(firstArgs[i]),
351+
secondArgs[i],
352+
sugaredFirstType->castTo<ParameterizedProtocolType>()
353+
->getArgs()[i]);
354+
}
355+
356+
return true;
357+
}
349358
}
350359

351360
return mismatch(firstParametrizedProto.getPointer(), secondType,

include/swift/AST/Types.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ class alignas(1 << TypeAlignInBits) TypeBase
413413
Count : 32
414414
);
415415

416+
SWIFT_INLINE_BITFIELD_FULL(ParameterizedProtocolType, TypeBase, 32,
417+
/// The number of type arguments.
418+
ArgCount : 32
419+
);
420+
416421
SWIFT_INLINE_BITFIELD_FULL(TupleType, TypeBase, 1+32,
417422
/// Whether an element of the tuple is inout, __shared or __owned.
418423
/// Values cannot have such tuple types in the language.
@@ -5243,15 +5248,16 @@ class ProtocolCompositionType final : public TypeBase,
52435248
BEGIN_CAN_TYPE_WRAPPER(ProtocolCompositionType, Type)
52445249
END_CAN_TYPE_WRAPPER(ProtocolCompositionType, Type)
52455250

5246-
/// ParameterizedProtocolType - A type that constrains the primary associated
5247-
/// type of a protocol to an argument type.
5251+
/// ParameterizedProtocolType - A type that constrains one or more primary
5252+
/// associated type of a protocol to a list of argument types.
52485253
///
52495254
/// Written like a bound generic type, eg Sequence<Int>.
52505255
///
52515256
/// For now, these are only supported in generic requirement-like contexts:
52525257
/// - Inheritance clauses of protocols, generic parameters, associated types
52535258
/// - Conformance requirements in where clauses
52545259
/// - Extensions
5260+
/// - Opaque result types
52555261
///
52565262
/// Assuming that the primary associated type of Sequence is Element, the
52575263
/// desugaring is that T : Sequence<Int> is equivalent to
@@ -5260,51 +5266,54 @@ END_CAN_TYPE_WRAPPER(ProtocolCompositionType, Type)
52605266
/// T : Sequence where T.Element == Int.
52615267
/// \endcode
52625268
class ParameterizedProtocolType final : public TypeBase,
5263-
public llvm::FoldingSetNode {
5269+
public llvm::FoldingSetNode,
5270+
private llvm::TrailingObjects<ParameterizedProtocolType, Type> {
52645271
friend struct ExistentialLayout;
5272+
friend TrailingObjects;
52655273

52665274
ProtocolType *Base;
5267-
AssociatedTypeDecl *AssocType;
52685275
Type Arg;
52695276

52705277
public:
52715278
/// Retrieve an instance of a protocol composition type with the
52725279
/// given set of members.
52735280
static Type get(const ASTContext &C, ProtocolType *base,
5274-
Type arg);
5281+
ArrayRef<Type> args);
52755282

52765283
ProtocolType *getBaseType() const {
52775284
return Base;
52785285
}
52795286

5280-
AssociatedTypeDecl *getAssocType() const {
5281-
return AssocType;
5287+
ArrayRef<Type> getArgs() const {
5288+
return {getTrailingObjects<Type>(),
5289+
Bits.ParameterizedProtocolType.ArgCount};
52825290
}
52835291

5284-
Type getArgumentType() const {
5285-
return Arg;
5286-
}
5292+
void getRequirements(Type baseType, SmallVectorImpl<Requirement> &reqs) const;
52875293

52885294
void Profile(llvm::FoldingSetNodeID &ID) {
5289-
Profile(ID, Base, Arg);
5295+
Profile(ID, Base, getArgs());
52905296
}
52915297
static void Profile(llvm::FoldingSetNodeID &ID,
52925298
ProtocolType *base,
5293-
Type arg);
5299+
ArrayRef<Type> args);
52945300

52955301
// Implement isa/cast/dyncast/etc.
52965302
static bool classof(const TypeBase *T) {
52975303
return T->getKind() == TypeKind::ParameterizedProtocol;
52985304
}
5299-
5305+
53005306
private:
53015307
ParameterizedProtocolType(const ASTContext *ctx,
5302-
ProtocolType *base, Type arg,
5308+
ProtocolType *base,
5309+
ArrayRef<Type> args,
53035310
RecursiveTypeProperties properties);
53045311
};
53055312
BEGIN_CAN_TYPE_WRAPPER(ParameterizedProtocolType, Type)
53065313
PROXY_CAN_TYPE_SIMPLE_GETTER(getBaseType)
5307-
PROXY_CAN_TYPE_SIMPLE_GETTER(getArgumentType)
5314+
CanTypeArrayRef getArgs() const {
5315+
return CanTypeArrayRef(getPointer()->getArgs());
5316+
}
53085317
END_CAN_TYPE_WRAPPER(ParameterizedProtocolType, Type)
53095318

53105319
/// An existential type, spelled with \c any .

lib/AST/ASTContext.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,25 +3360,31 @@ ProtocolCompositionType::build(const ASTContext &C, ArrayRef<Type> Members,
33603360

33613361
Type ParameterizedProtocolType::get(const ASTContext &C,
33623362
ProtocolType *baseTy,
3363-
Type argTy) {
3363+
ArrayRef<Type> args) {
3364+
assert(args.size() > 0);
3365+
33643366
bool isCanonical = baseTy->isCanonical();
33653367
RecursiveTypeProperties properties = baseTy->getRecursiveProperties();
3366-
properties |= argTy->getRecursiveProperties();
3367-
isCanonical &= argTy->isCanonical();
3368+
for (auto arg : args) {
3369+
properties |= arg->getRecursiveProperties();
3370+
isCanonical &= arg->isCanonical();
3371+
}
33683372

33693373
auto arena = getArena(properties);
33703374

33713375
void *InsertPos = nullptr;
33723376
llvm::FoldingSetNodeID ID;
3373-
ParameterizedProtocolType::Profile(ID, baseTy, argTy);
3377+
ParameterizedProtocolType::Profile(ID, baseTy, args);
33743378

33753379
if (auto paramTy
33763380
= C.getImpl().getArena(arena).ParameterizedProtocolTypes
33773381
.FindNodeOrInsertPos(ID, InsertPos))
33783382
return paramTy;
33793383

3380-
auto paramTy = new (C, arena) ParameterizedProtocolType(
3381-
isCanonical ? &C : nullptr, baseTy, argTy, properties);
3384+
auto size = totalSizeToAlloc<Type>(args.size());
3385+
auto mem = C.Allocate(size, alignof(ParameterizedProtocolType), arena);
3386+
auto paramTy = new (mem) ParameterizedProtocolType(
3387+
isCanonical ? &C : nullptr, baseTy, args, properties);
33823388
C.getImpl().getArena(arena).ParameterizedProtocolTypes.InsertNode(
33833389
paramTy, InsertPos);
33843390
return paramTy;

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,9 @@ namespace {
39473947
StringRef label) {
39483948
printCommon(label, "parameterized_protocol_type");
39493949
printRec("base", T->getBaseType());
3950-
printRec("arg", T->getArgumentType());
3950+
for (auto arg : T->getArgs()) {
3951+
printRec(arg);
3952+
}
39513953
PrintWithColorRAII(OS, ParenthesisColor) << ')';
39523954
}
39533955

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6105,7 +6105,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61056105
void visitParameterizedProtocolType(ParameterizedProtocolType *T) {
61066106
visit(T->getBaseType());
61076107
Printer << "<";
6108-
visit(T->getArgumentType());
6108+
interleave(T->getArgs(), [&](Type Ty) { visit(Ty); },
6109+
[&] { Printer << ", "; });
61096110
Printer << ">";
61106111
}
61116112

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5459,9 +5459,10 @@ bool ProtocolDecl::existentialRequiresAny() const {
54595459
ExistentialRequiresAnyRequest{const_cast<ProtocolDecl *>(this)}, true);
54605460
}
54615461

5462-
AssociatedTypeDecl *ProtocolDecl::getPrimaryAssociatedType() const {
5462+
ArrayRef<AssociatedTypeDecl *>
5463+
ProtocolDecl::getPrimaryAssociatedTypes() const {
54635464
return evaluateOrDefault(getASTContext().evaluator,
5464-
PrimaryAssociatedTypeRequest{const_cast<ProtocolDecl *>(this)},
5465+
PrimaryAssociatedTypesRequest{const_cast<ProtocolDecl *>(this)},
54655466
nullptr);
54665467
}
54675468

lib/AST/Expr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ bool Expr::isSemanticallyConstExpr() const {
209209
case ExprKind::BooleanLiteral:
210210
case ExprKind::FloatLiteral:
211211
case ExprKind::StringLiteral:
212+
case ExprKind::KeyPath:
212213
return true;
213214
case ExprKind::Array:
214215
case ExprKind::Dictionary: {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,14 +4523,13 @@ ConstraintResult GenericSignatureBuilder::addTypeRequirement(
45234523
source)))
45244524
anyErrors = true;
45254525

4526-
auto *assocType = paramProtoType->getAssocType();
4527-
auto depType = DependentMemberType::get(
4528-
resolvedSubject.getDependentType(*this), assocType);
4529-
if (isErrorResult(addSameTypeRequirement(Type(depType),
4530-
paramProtoType->getArgumentType(),
4531-
source,
4532-
UnresolvedHandlingKind::GenerateConstraints)))
4533-
anyErrors = true;
4526+
SmallVector<Requirement, 2> reqs;
4527+
auto baseType = resolvedSubject.getDependentType(*this);
4528+
paramProtoType->getRequirements(baseType, reqs);
4529+
for (auto req : reqs) {
4530+
if (isErrorResult(addRequirement(req, source, inferForModule)))
4531+
anyErrors = true;
4532+
}
45344533

45354534
return anyErrors ? ConstraintResult::Conflicting
45364535
: ConstraintResult::Resolved;

lib/AST/Module.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,14 @@ void ModuleDecl::getDisplayDecls(SmallVectorImpl<Decl*> &Results) const {
963963
#ifndef NDEBUG
964964
llvm::DenseSet<Decl *> visited;
965965
for (auto *D : Results) {
966+
// decls synthesized from implicit clang decls may appear multiple times;
967+
// e.g. if multiple modules with underlying clang modules are re-exported.
968+
// including duplicates of these is harmless, so skip them when counting
969+
// this assertion
970+
if (const auto *CD = D->getClangDecl()) {
971+
if (CD->isImplicit()) continue;
972+
}
973+
966974
auto inserted = visited.insert(D).second;
967975
assert(inserted && "there should be no duplicate decls");
968976
}

0 commit comments

Comments
 (0)