Skip to content

Commit 6586930

Browse files
Merge pull request #4420 from swiftwasm/katei/merge-main-2022-04-05
Merge main 2022-04-05
2 parents 23aa047 + ed7d40b commit 6586930

File tree

109 files changed

+1816
-1124
lines changed

Some content is hidden

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

109 files changed

+1816
-1124
lines changed

SwiftCompilerSources/Package.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ let package = Package(
2828
.target(
2929
name: "_RegexParser",
3030
dependencies: [],
31-
swiftSettings: [SwiftSetting.unsafeFlags([
31+
swiftSettings: [
32+
.unsafeFlags([
3233
"-I", "../include/swift",
33-
"-cross-module-optimization"
34+
"-cross-module-optimization",
35+
]),
36+
// Workaround until `_RegexParser` is imported as implementation-only
37+
// by `_StringProcessing`.
38+
.unsafeFlags([
39+
"-Xfrontend",
40+
"-disable-implicit-string-processing-module-import"
3441
])]),
3542
.target(
3643
name: "Optimizer",

docs/RequirementMachine/RequirementMachine.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3053,7 +3053,7 @@ \chapter{The Requirement Machine}\label{requirementmachine}
30533053

30543054
\section{Generic Parameters}\label{genericparamsym}
30553055
\index{generic parameter symbol}
3056-
So far, I've only shown you how to build rewrite rules from requirements in the requirement signature of some protocol $\proto{P}$. When lowering a type parameter, the protocol $\genericparam{Self}$ type lowers to the protocol symbol $\protosym{P}$. Once such a rewrite system is built, queries can be performed against the protocol generic signature $\gensig{\genericparam{Self}}{\genericparam{Self}\colon\proto{P}}$. When lowering parameters and requirements in an arbitary generic signature, generic parameter types instead become generic parameter symbols.
3056+
So far, I've only shown you how to build rewrite rules from requirements in the requirement signature of some protocol $\proto{P}$. When lowering a type parameter, the protocol $\genericparam{Self}$ type lowers to the protocol symbol $\protosym{P}$. Once such a rewrite system is built, queries can be performed against the protocol generic signature $\gensig{\genericparam{Self}}{\genericparam{Self}\colon\proto{P}}$. When lowering parameters and requirements in an arbitrary generic signature, generic parameter types instead become generic parameter symbols.
30573057

30583058
Generic parameter symbols should only ever appear as the initial symbol in a term. While the rewrite system would have no trouble with terms where generic parameter symbols appear elsewhere in the abstract, they don't actually make sense semantically, since they do not correspond to valid Swift type parameter types.
30593059

include/swift/AST/Decl.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -506,11 +506,6 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
506506
IsOpaqueType : 1
507507
);
508508

509-
SWIFT_INLINE_BITFIELD_FULL(AssociatedTypeDecl, AbstractTypeParamDecl, 1,
510-
/// Whether this is a primary associated type.
511-
IsPrimary : 1
512-
);
513-
514509
SWIFT_INLINE_BITFIELD_EMPTY(GenericTypeDecl, TypeDecl);
515510

516511
SWIFT_INLINE_BITFIELD(TypeAliasDecl, GenericTypeDecl, 1+1,
@@ -3272,14 +3267,6 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
32723267
LazyMemberLoader *definitionResolver,
32733268
uint64_t resolverData);
32743269

3275-
bool isPrimary() const {
3276-
return Bits.AssociatedTypeDecl.IsPrimary;
3277-
}
3278-
3279-
void setPrimary() {
3280-
Bits.AssociatedTypeDecl.IsPrimary = true;
3281-
}
3282-
32833270
/// Get the protocol in which this associated type is declared.
32843271
ProtocolDecl *getProtocol() const {
32853272
return cast<ProtocolDecl>(getDeclContext());
@@ -4358,11 +4345,14 @@ enum class KnownDerivableProtocolKind : uint8_t {
43584345
Decodable,
43594346
AdditiveArithmetic,
43604347
Differentiable,
4348+
Identifiable,
43614349
Actor,
43624350
DistributedActor,
43634351
DistributedActorSystem,
43644352
};
43654353

4354+
using PrimaryAssociatedTypeName = std::pair<Identifier, SourceLoc>;
4355+
43664356
/// ProtocolDecl - A declaration of a protocol, for example:
43674357
///
43684358
/// protocol Drawable {
@@ -4379,6 +4369,7 @@ enum class KnownDerivableProtocolKind : uint8_t {
43794369
class ProtocolDecl final : public NominalTypeDecl {
43804370
SourceLoc ProtocolLoc;
43814371

4372+
ArrayRef<PrimaryAssociatedTypeName> PrimaryAssociatedTypeNames;
43824373
ArrayRef<ProtocolDecl *> InheritedProtocols;
43834374
ArrayRef<AssociatedTypeDecl *> AssociatedTypes;
43844375

@@ -4460,7 +4451,9 @@ class ProtocolDecl final : public NominalTypeDecl {
44604451

44614452
public:
44624453
ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc, SourceLoc NameLoc,
4463-
Identifier Name, ArrayRef<InheritedEntry> Inherited,
4454+
Identifier Name,
4455+
ArrayRef<PrimaryAssociatedTypeName> PrimaryAssociatedTypeNames,
4456+
ArrayRef<InheritedEntry> Inherited,
44644457
TrailingWhereClause *TrailingWhere);
44654458

44664459
using Decl::getASTContext;
@@ -4486,6 +4479,13 @@ class ProtocolDecl final : public NominalTypeDecl {
44864479
/// a protocol having nested types (ObjC protocols).
44874480
ArrayRef<AssociatedTypeDecl *> getAssociatedTypeMembers() const;
44884481

4482+
/// Returns the list of primary associated type names. These are the associated
4483+
/// types that is parametrized with same-type requirements in a
4484+
/// parametrized protocol type of the form SomeProtocol<Arg1, Arg2...>.
4485+
ArrayRef<PrimaryAssociatedTypeName> getPrimaryAssociatedTypeNames() const {
4486+
return PrimaryAssociatedTypeNames;
4487+
}
4488+
44894489
/// Returns the list of primary associated types. These are the associated
44904490
/// types that is parametrized with same-type requirements in a
44914491
/// parametrized protocol type of the form SomeProtocol<Arg1, Arg2...>.

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3770,14 +3770,19 @@ ERROR(not_a_generic_definition,none,
37703770
"cannot specialize a non-generic definition", ())
37713771
ERROR(not_a_generic_type,none,
37723772
"cannot specialize non-generic type %0", (Type))
3773+
ERROR(protocol_declares_unknown_primary_assoc_type,none,
3774+
"an associated type named %0 must be declared in the protocol %1 or a protocol it inherits",
3775+
(Identifier, Type))
3776+
ERROR(protocol_declares_duplicate_primary_assoc_type,none,
3777+
"duplicate primary associated type name %0", (Identifier))
37733778
ERROR(parameterized_protocol_not_supported,none,
3774-
"protocol type with generic arguments can only be used as a generic constraint", ())
3779+
"protocol type with type arguments can only be used as a generic constraint", ())
37753780
ERROR(protocol_does_not_have_primary_assoc_type,none,
37763781
"cannot specialize protocol type %0", (Type))
3777-
ERROR(parameterized_protocol_too_many_type_arguments,none,
3778-
"protocol type %0 can specialized with too many type parameters "
3779-
"(got %1, but expected at most %2)",
3780-
(Type, unsigned, unsigned))
3782+
ERROR(parameterized_protocol_type_argument_count_mismatch,none,
3783+
"protocol type %0 specialized with %select{too many|too few}3 type arguments "
3784+
"(got %1, but expected %2)",
3785+
(Type, unsigned, unsigned, unsigned))
37813786
ERROR(cannot_specialize_self,none,
37823787
"cannot specialize 'Self'", ())
37833788
NOTE(specialize_explicit_type_instead,none,
@@ -6128,9 +6133,6 @@ WARNING(result_builder_missing_limited_availability, none,
61286133
"result builder %0 does not implement 'buildLimitedAvailability'; "
61296134
"this code may crash on earlier versions of the OS",
61306135
(Type))
6131-
ERROR(result_builder_static_buildblock, none,
6132-
"result builder must provide at least one static 'buildBlock' "
6133-
"method", ())
61346136
ERROR(result_builder_static_buildblock_or_buildpartialblock, none,
61356137
"result builder must provide at least one static 'buildBlock' "
61366138
"method, or both 'buildPartialBlock(first:)' and "

include/swift/AST/EducationalNotes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ EDUCATIONAL_NOTES(type_cannot_conform, "protocol-type-non-conformance.md")
7373
EDUCATIONAL_NOTES(unlabeled_trailing_closure_deprecated,
7474
"trailing-closure-matching.md")
7575

76-
EDUCATIONAL_NOTES(result_builder_static_buildblock,
76+
EDUCATIONAL_NOTES(result_builder_static_buildblock_or_buildpartialblock,
7777
"result-builder-methods.md")
7878
EDUCATIONAL_NOTES(result_builder_missing_limited_availability,
7979
"result-builder-methods.md")

include/swift/AST/KnownProtocols.def

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

6161
PROTOCOL(Actor)
6262
PROTOCOL(Sequence)
63+
PROTOCOL(Identifiable)
6364
PROTOCOL(IteratorProtocol)
6465
PROTOCOL(RawRepresentable)
6566
PROTOCOL(Equatable)

include/swift/Basic/LangOptions.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ namespace swift {
209209
/// Emit a remark after loading a module.
210210
bool EnableModuleLoadingRemarks = false;
211211

212+
/// Resolve main function as though it were called from an async context
213+
bool EnableAsyncMainResolution = false;
214+
212215
///
213216
/// Support for alternate usage modes
214217
///
@@ -346,9 +349,6 @@ namespace swift {
346349
/// Enable experimental 'move only' features.
347350
bool EnableExperimentalMoveOnly = false;
348351

349-
/// Enable experimental pairwise `buildBlock` for result builders.
350-
bool EnableExperimentalPairwiseBuildBlock = false;
351-
352352
/// Enable variadic generics.
353353
bool EnableExperimentalVariadicGenerics = false;
354354

@@ -360,6 +360,9 @@ namespace swift {
360360
bool DisableImplicitConcurrencyModuleImport =
361361
!SWIFT_IMPLICIT_CONCURRENCY_IMPORT;
362362

363+
/// Disable the implicit import of the _StringProcessing module.
364+
bool DisableImplicitStringProcessingModuleImport = false;
365+
363366
/// Should we check the target OSs of serialized modules to see that they're
364367
/// new enough?
365368
bool EnableTargetOSChecking = true;

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ def disable_implicit_distributed_module_import : Flag<["-"],
422422
"disable-implicit-distributed-module-import">,
423423
HelpText<"Disable the implicit import of the Distributed module.">;
424424

425+
def disable_implicit_string_processing_module_import : Flag<["-"],
426+
"disable-implicit-string-processing-module-import">,
427+
HelpText<"Disable the implicit import of the _StringProcessing module.">;
428+
425429
def disable_arc_opts : Flag<["-"], "disable-arc-opts">,
426430
HelpText<"Don't run SIL ARC optimization passes.">;
427431
def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ def pch_output_dir: Separate<["-"], "pch-output-dir">,
270270
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
271271
HelpText<"Directory to persist automatically created precompiled bridging headers">;
272272

273+
def async_main: Flag<["-"], "async-main">,
274+
Flags<[FrontendOption]>,
275+
HelpText<"Resolve main function as if it were called from an asynchronous context">;
276+
273277
// FIXME: Unhide this once it doesn't depend on an output file map.
274278
def incremental : Flag<["-"], "incremental">,
275279
Flags<[NoInteractiveOption, HelpHidden, DoesNotAffectIncrementalBuild]>,

include/swift/Parse/Parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,9 @@ class Parser {
12131213
parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD);
12141214

12151215
ParserStatus parsePrimaryAssociatedTypes(
1216-
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
1216+
SmallVectorImpl<PrimaryAssociatedTypeName> &AssocTypeNames);
12171217
ParserStatus parsePrimaryAssociatedTypeList(
1218-
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
1218+
SmallVectorImpl<PrimaryAssociatedTypeName> &AssocTypeNames);
12191219
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
12201220
DeclAttributes &Attributes);
12211221

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,10 @@ enum class ConstraintSystemFlags {
15151515
/// calling conventions, say due to Clang attributes such as
15161516
/// `__attribute__((ns_consumed))`.
15171517
UseClangFunctionTypes = 0x80,
1518+
1519+
/// When set, nominal typedecl contexts are asynchronous contexts.
1520+
/// This is set while searching for the main function
1521+
ConsiderNominalTypeContextsAsync = 0x100,
15181522
};
15191523

15201524
/// Options that affect the constraint system as a whole.

include/swift/Sema/IDETypeChecking.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "swift/AST/Identifier.h"
2323
#include "swift/Basic/SourceLoc.h"
24+
#include "swift/Basic/OptionSet.h"
2425
#include <memory>
2526
#include <tuple>
2627

@@ -50,6 +51,8 @@ namespace swift {
5051
class ConstraintSystem;
5152
class Solution;
5253
class SolutionApplicationTarget;
54+
enum class ConstraintSystemFlags;
55+
using ConstraintSystemOptions = OptionSet<ConstraintSystemFlags>;
5356
}
5457

5558
/// Typecheck binding initializer at \p bindingIndex.
@@ -93,8 +96,9 @@ namespace swift {
9396
/// Unlike other member lookup functions, \c swift::resolveValueMember()
9497
/// should be used when you want to look up declarations with the same name as
9598
/// one you already have.
96-
ResolvedMemberResult resolveValueMember(DeclContext &DC, Type BaseTy,
97-
DeclName Name);
99+
ResolvedMemberResult
100+
resolveValueMember(DeclContext &DC, Type BaseTy, DeclName Name,
101+
constraints::ConstraintSystemOptions Options = {});
98102

99103
/// Given a type and an extension to the original type decl of that type,
100104
/// decide if the extension has been applied, i.e. if the requirements of the

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,8 +1381,7 @@ struct RequirementPrintLocation {
13811381
/// function does: asking "where should this requirement be printed?" and then
13821382
/// callers check if the location is the ATD.
13831383
static RequirementPrintLocation
1384-
bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req,
1385-
PrintOptions opts, bool inheritanceClause) {
1384+
bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req) {
13861385
auto protoSelf = proto->getProtocolSelfType();
13871386
// Returns the most relevant decl within proto connected to outerType (or null
13881387
// if one doesn't exist), and whether the type is an "direct use",
@@ -1415,16 +1414,6 @@ bestRequirementPrintLocation(ProtocolDecl *proto, const Requirement &req,
14151414
// desired.
14161415
auto directUse = foundType && outerType->isEqual(foundType);
14171416

1418-
// Prefer to attach requirements to associated type declarations,
1419-
// unless the associated type is a primary associated type and
1420-
// we're printing primary associated types using the new syntax.
1421-
if (!directUse &&
1422-
relevantDecl &&
1423-
opts.PrintPrimaryAssociatedTypes &&
1424-
isa<AssociatedTypeDecl>(relevantDecl) &&
1425-
cast<AssociatedTypeDecl>(relevantDecl)->isPrimary())
1426-
relevantDecl = proto;
1427-
14281417
return std::make_pair(relevantDecl, directUse);
14291418
};
14301419

@@ -1495,8 +1484,7 @@ void PrintAST::printInheritedFromRequirementSignature(ProtocolDecl *proto,
14951484
return false;
14961485
}
14971486

1498-
auto location = bestRequirementPrintLocation(proto, req, Options,
1499-
/*inheritanceClause=*/true);
1487+
auto location = bestRequirementPrintLocation(proto, req);
15001488
return location.AttachedTo == attachingTo && !location.InWhereClause;
15011489
});
15021490
}
@@ -1511,8 +1499,7 @@ void PrintAST::printWhereClauseFromRequirementSignature(ProtocolDecl *proto,
15111499
proto->getRequirementSignature().getRequirements()),
15121500
flags,
15131501
[&](const Requirement &req) {
1514-
auto location = bestRequirementPrintLocation(proto, req, Options,
1515-
/*inheritanceClause=*/false);
1502+
auto location = bestRequirementPrintLocation(proto, req);
15161503
return location.AttachedTo == attachingTo && location.InWhereClause;
15171504
});
15181505
}
@@ -3557,14 +3544,6 @@ void PrintAST::printPrimaryAssociatedTypes(ProtocolDecl *decl) {
35573544
assocType);
35583545
Printer.printName(assocType->getName(),
35593546
PrintNameContext::GenericParameter);
3560-
3561-
printInheritedFromRequirementSignature(decl, assocType);
3562-
3563-
if (assocType->hasDefaultDefinitionType()) {
3564-
Printer << " = ";
3565-
assocType->getDefaultDefinitionType().print(Printer, Options);
3566-
}
3567-
35683547
Printer.printStructurePost(PrintStructureKind::GenericParameter,
35693548
assocType);
35703549
},
@@ -5092,14 +5071,6 @@ bool Decl::shouldPrintInContext(const PrintOptions &PO) const {
50925071
return PO.PrintIfConfig;
50935072
}
50945073

5095-
if (auto *ATD = dyn_cast<AssociatedTypeDecl>(this)) {
5096-
// If PO.PrintPrimaryAssociatedTypes is on, primary associated
5097-
// types are printed as part of the protocol declaration itself,
5098-
// so skip them here.
5099-
if (ATD->isPrimary() && PO.PrintPrimaryAssociatedTypes)
5100-
return false;
5101-
}
5102-
51035074
// Print everything else.
51045075
return true;
51055076
}

lib/AST/Decl.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,9 +4557,7 @@ AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
45574557
TrailingWhereClause *trailingWhere)
45584558
: AbstractTypeParamDecl(DeclKind::AssociatedType, dc, name, nameLoc),
45594559
KeywordLoc(keywordLoc), DefaultDefinition(defaultDefinition),
4560-
TrailingWhere(trailingWhere) {
4561-
Bits.AssociatedTypeDecl.IsPrimary = 0;
4562-
}
4560+
TrailingWhere(trailingWhere) {}
45634561

45644562
AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
45654563
Identifier name, SourceLoc nameLoc,
@@ -5305,11 +5303,13 @@ bool EnumDecl::hasCircularRawValue() const {
53055303

53065304
ProtocolDecl::ProtocolDecl(DeclContext *DC, SourceLoc ProtocolLoc,
53075305
SourceLoc NameLoc, Identifier Name,
5306+
ArrayRef<PrimaryAssociatedTypeName> PrimaryAssociatedTypeNames,
53085307
ArrayRef<InheritedEntry> Inherited,
53095308
TrailingWhereClause *TrailingWhere)
53105309
: NominalTypeDecl(DeclKind::Protocol, DC, Name, NameLoc, Inherited,
53115310
nullptr),
5312-
ProtocolLoc(ProtocolLoc) {
5311+
ProtocolLoc(ProtocolLoc),
5312+
PrimaryAssociatedTypeNames(PrimaryAssociatedTypeNames) {
53135313
Bits.ProtocolDecl.RequiresClassValid = false;
53145314
Bits.ProtocolDecl.RequiresClass = false;
53155315
Bits.ProtocolDecl.ExistentialConformsToSelfValid = false;
@@ -5622,6 +5622,8 @@ Optional<KnownDerivableProtocolKind>
56225622
return KnownDerivableProtocolKind::AdditiveArithmetic;
56235623
case KnownProtocolKind::Differentiable:
56245624
return KnownDerivableProtocolKind::Differentiable;
5625+
case KnownProtocolKind::Identifiable:
5626+
return KnownDerivableProtocolKind::Identifiable;
56255627
case KnownProtocolKind::Actor:
56265628
return KnownDerivableProtocolKind::Actor;
56275629
case KnownProtocolKind::DistributedActor:

lib/AST/RequirementMachine/ConcreteContraction.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,17 @@ bool ConcreteContraction::performConcreteContraction(
599599
// requirement where the left hand side is not a type parameter.
600600
SmallVector<Requirement, 4> reqs;
601601
if (req.inferred) {
602+
// Discard errors from desugaring a substituted requirement that
603+
// was inferred. For example, if we have something like
604+
//
605+
// <T, U where T == Int, U == Set<T>>
606+
//
607+
// The inferred requirement 'T : Hashable' from 'Set<>' will
608+
// be substituted with 'T == Int' to get 'Int : Hashable'.
609+
//
610+
// Desugaring will diagnose a redundant conformance requirement,
611+
// but we want to ignore that, since the user did not explicitly
612+
// write 'Int : Hashable' (or 'T : Hashable') anywhere.
602613
SmallVector<RequirementError, 4> discardErrors;
603614
desugarRequirement(substReq, SourceLoc(), reqs, discardErrors);
604615
} else {

0 commit comments

Comments
 (0)