Skip to content

Commit 23053c8

Browse files
authored
Merge pull request #4073 from swiftwasm/main
[pull] swiftwasm from main
2 parents eaa781e + fdda6f2 commit 23053c8

Some content is hidden

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

52 files changed

+889
-283
lines changed

benchmark/utils/ArgParse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ArgumentParser<U> {
7979
private var optionalArgsMap = [String : String]()
8080

8181
/// Argument holds the name of the command line parameter, its help
82-
/// desciption and a rule that's applied to process it.
82+
/// description and a rule that's applied to process it.
8383
///
8484
/// The the rule is typically a value processing closure used to convert it
8585
/// into given type and storing it in the parsing result.

include/swift/AST/Decl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ namespace swift {
106106

107107
namespace ast_scope {
108108
class AbstractPatternEntryScope;
109+
class GenericParamScope;
109110
class PatternEntryDeclScope;
110111
class PatternEntryInitializerScope;
111112
} // namespace ast_scope
@@ -1565,6 +1566,7 @@ class PatternBindingEntry {
15651566
friend class PatternBindingInitializer;
15661567
friend class PatternBindingDecl;
15671568
friend class ast_scope::AbstractPatternEntryScope;
1569+
friend class ast_scope::GenericParamScope;
15681570
friend class ast_scope::PatternEntryDeclScope;
15691571
friend class ast_scope::PatternEntryInitializerScope;
15701572

@@ -2779,6 +2781,16 @@ class OpaqueTypeDecl final :
27792781
return OpaqueInterfaceGenericSignature.getInnermostGenericParams();
27802782
}
27812783

2784+
/// Whether the generic parameters of this opaque type declaration were
2785+
/// explicit, i.e., for named opaque result types.
2786+
bool hasExplicitGenericParams() const;
2787+
2788+
/// When the generic parameters were explicit, returns the generic parameter
2789+
/// corresponding to the given ordinal.
2790+
///
2791+
/// Otherwise, returns \c nullptr.
2792+
GenericTypeParamDecl *getExplicitGenericParam(unsigned ordinal) const;
2793+
27822794
/// Retrieve the buffer containing the opaque return type
27832795
/// representations that correspond to the opaque generic parameters.
27842796
ArrayRef<OpaqueReturnTypeRepr *> getOpaqueReturnTypeReprs() const {

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4134,6 +4134,9 @@ ERROR(opaque_type_no_underlying_type_candidates,none,
41344134
ERROR(opaque_type_mismatched_underlying_type_candidates,none,
41354135
"function declares an opaque return type %0, but the return statements "
41364136
"in its body do not have matching underlying types", (TypeRepr *))
4137+
ERROR(opaque_type_mismatched_underlying_type_candidates_named,none,
4138+
"function declares an opaque return type %0, but the return statements "
4139+
"in its body do not have matching underlying types", (Identifier))
41374140
NOTE(opaque_type_underlying_type_candidate_here,none,
41384141
"return statement has underlying type %0", (Type))
41394142
ERROR(opaque_type_self_referential_underlying_type,none,

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
368368
SILInstructionResultArray getResultsImpl() const;
369369

370370
protected:
371-
friend class LibswiftPassInvocation;
371+
friend class SwiftPassInvocation;
372372

373373
SILInstruction() {
374374
NumCreatedInstructions++;

include/swift/SIL/SILInstructionWorklist.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,13 @@ template <typename VectorT = std::vector<SILInstruction *>,
6767
class SILInstructionWorklist : SILInstructionWorklistBase {
6868
BlotSetVector<SILInstruction *, VectorT, MapT> worklist;
6969

70-
/// For invoking Swift instruction passes in Swift.
71-
LibswiftPassInvocation *libswiftPassInvocation = nullptr;
72-
7370
void operator=(const SILInstructionWorklist &rhs) = delete;
7471
SILInstructionWorklist(const SILInstructionWorklist &worklist) = delete;
7572

7673
public:
7774
SILInstructionWorklist(const char *loggingName = "InstructionWorklist")
7875
: SILInstructionWorklistBase(loggingName) {}
7976

80-
void setLibswiftPassInvocation(LibswiftPassInvocation *invocation) {
81-
libswiftPassInvocation = invocation;
82-
}
83-
8477
/// Returns true if the worklist is empty.
8578
bool isEmpty() const { return worklist.empty(); }
8679

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void executePassPipelinePlan(SILModule *SM, const SILPassPipelinePlan &plan,
4545
irgen::IRGenModule *IRMod = nullptr);
4646

4747
/// Utility class to invoke Swift passes.
48-
class LibswiftPassInvocation {
48+
class SwiftPassInvocation {
4949
/// Backlink to the pass manager.
5050
SILPassManager *passManager;
5151

@@ -58,12 +58,14 @@ class LibswiftPassInvocation {
5858
/// All slabs, allocated by the pass.
5959
SILModule::SlabList allocatedSlabs;
6060

61+
void endPassRunChecks();
62+
6163
public:
62-
LibswiftPassInvocation(SILPassManager *passManager, SILFunction *function,
64+
SwiftPassInvocation(SILPassManager *passManager, SILFunction *function,
6365
SILCombiner *silCombiner) :
6466
passManager(passManager), function(function), silCombiner(silCombiner) {}
6567

66-
LibswiftPassInvocation(SILPassManager *passManager) :
68+
SwiftPassInvocation(SILPassManager *passManager) :
6769
passManager(passManager) {}
6870

6971
SILPassManager *getPassManager() const { return passManager; }
@@ -81,10 +83,16 @@ class LibswiftPassInvocation {
8183
void notifyChanges(SILAnalysis::InvalidationKind invalidationKind);
8284

8385
/// Called by the pass manager before the pass starts running.
84-
void startPassRun(SILFunction *function);
86+
void startFunctionPassRun(SILFunction *function);
87+
88+
/// Called by the SILCombiner before the instruction pass starts running.
89+
void startInstructionPassRun(SILInstruction *inst);
8590

8691
/// Called by the pass manager when the pass has finished.
87-
void finishedPassRun();
92+
void finishedFunctionPassRun();
93+
94+
/// Called by the SILCombiner when the instruction pass has finished.
95+
void finishedInstructionPassRun();
8896
};
8997

9098
/// The SIL pass manager.
@@ -126,7 +134,7 @@ class SILPassManager {
126134
unsigned NumPassesRun = 0;
127135

128136
/// For invoking Swift passes.
129-
LibswiftPassInvocation libswiftPassInvocation;
137+
SwiftPassInvocation swiftPassInvocation;
130138

131139
/// Change notifications, collected during a bridged pass run.
132140
SILAnalysis::InvalidationKind changeNotifications =
@@ -201,8 +209,8 @@ class SILPassManager {
201209
/// pass manager.
202210
irgen::IRGenModule *getIRGenModule() { return IRMod; }
203211

204-
LibswiftPassInvocation *getLibswiftPassInvocation() {
205-
return &libswiftPassInvocation;
212+
SwiftPassInvocation *getSwiftPassInvocation() {
213+
return &swiftPassInvocation;
206214
}
207215

208216
/// Restart the function pass pipeline on the same function
@@ -377,7 +385,7 @@ class SILPassManager {
377385
void viewCallGraph();
378386
};
379387

380-
inline void LibswiftPassInvocation::
388+
inline void SwiftPassInvocation::
381389
notifyChanges(SILAnalysis::InvalidationKind invalidationKind) {
382390
passManager->notifyPassChanges(invalidationKind);
383391
}

lib/AST/ASTMangler.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,17 @@ unsigned ASTMangler::appendBoundGenericArgs(DeclContext *dc,
15581558

15591559
// If we are generic at this level, emit all of the replacements at
15601560
// this level.
1561-
if (genericContext->isGeneric()) {
1561+
bool treatAsGeneric;
1562+
if (auto opaque = dyn_cast<OpaqueTypeDecl>(decl)) {
1563+
// For opaque type declarations, the generic parameters of the opaque
1564+
// type declaration are not part of the mangling, so check whether the
1565+
// naming declaration has generic parameters.
1566+
auto namedGenericContext = opaque->getNamingDecl()->getAsGenericContext();
1567+
treatAsGeneric = namedGenericContext && namedGenericContext->isGeneric();
1568+
} else {
1569+
treatAsGeneric = genericContext->isGeneric();
1570+
}
1571+
if (treatAsGeneric) {
15621572
auto genericParams = subs.getGenericSignature().getGenericParams();
15631573
unsigned depth = genericParams[currentGenericParamIdx]->getDepth();
15641574
auto replacements = subs.getReplacementTypes();

lib/AST/ASTPrinter.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4338,9 +4338,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43384338
case PrintOptions::OpaqueReturnTypePrintingMode::Description:
43394339
return true;
43404340
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
4341-
return false;
4341+
return opaque->getDecl()->hasExplicitGenericParams();
43424342
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword:
4343-
return isSimpleUnderPrintOptions(opaque->getExistentialType());
4343+
return opaque->getDecl()->hasExplicitGenericParams() ||
4344+
isSimpleUnderPrintOptions(opaque->getExistentialType());
43444345
}
43454346
llvm_unreachable("bad opaque-return-type printing mode");
43464347
}
@@ -5410,11 +5411,28 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
54105411
}
54115412

54125413
void visitOpaqueTypeArchetypeType(OpaqueTypeArchetypeType *T) {
5414+
// Try to print a named opaque type.
5415+
auto printNamedOpaque = [&] {
5416+
if (auto genericParam =
5417+
T->getDecl()->getExplicitGenericParam(T->getOrdinal())) {
5418+
visit(genericParam->getDeclaredInterfaceType());
5419+
return true;
5420+
}
5421+
5422+
return false;
5423+
};
5424+
54135425
switch (Options.OpaqueReturnTypePrinting) {
54145426
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
5427+
if (printNamedOpaque())
5428+
return;
5429+
54155430
Printer.printKeyword("some", Options, /*Suffix=*/" ");
54165431
LLVM_FALLTHROUGH;
54175432
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
5433+
if (printNamedOpaque())
5434+
return;
5435+
54185436
visit(T->getExistentialType());
54195437
return;
54205438
}

lib/AST/ASTScopeCreation.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,26 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
708708
// Initializers come before VarDecls, e.g. PCMacro/didSet.swift 19
709709
auto patternEntry = getPatternEntry();
710710

711+
// If the pattern type is for a named opaque result type, introduce the
712+
// generic type parameters based on the first variable we find.
713+
ASTScopeImpl *leaf = this;
714+
auto pattern = patternEntry.getPattern();
715+
if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
716+
if (auto namedOpaque =
717+
dyn_cast_or_null<NamedOpaqueReturnTypeRepr>(
718+
typedPattern->getTypeRepr())) {
719+
bool addedOpaqueResultTypeScope = false;
720+
pattern->forEachVariable([&](VarDecl *var) {
721+
if (addedOpaqueResultTypeScope)
722+
return;
723+
724+
leaf = scopeCreator.addNestedGenericParamScopesToTree(
725+
var, namedOpaque->getGenericParams(), leaf);
726+
addedOpaqueResultTypeScope = true;
727+
});
728+
}
729+
}
730+
711731
// Create a child for the initializer, if present.
712732
// Cannot trust the source range given in the ASTScopeImpl for the end of the
713733
// initializer (because of InterpolatedLiteralStrings and EditorPlaceHolders),
@@ -724,7 +744,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
724744
"Original inits are always after the '='");
725745
scopeCreator
726746
.constructExpandAndInsert<PatternEntryInitializerScope>(
727-
this, decl, patternEntryIndex);
747+
leaf, decl, patternEntryIndex);
728748
}
729749

730750
// If this pattern binding entry was created by the debugger, it will always
@@ -741,12 +761,12 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
741761
"inits are always after the '='");
742762
scopeCreator
743763
.constructExpandAndInsert<PatternEntryInitializerScope>(
744-
this, decl, patternEntryIndex);
764+
leaf, decl, patternEntryIndex);
745765
}
746766

747767
// Add accessors for the variables in this pattern.
748-
patternEntry.getPattern()->forEachVariable([&](VarDecl *var) {
749-
scopeCreator.addChildrenForParsedAccessors(var, this);
768+
pattern->forEachVariable([&](VarDecl *var) {
769+
scopeCreator.addChildrenForParsedAccessors(var, leaf);
750770
});
751771

752772
// In local context, the PatternEntryDeclScope becomes the insertion point, so
@@ -849,6 +869,20 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
849869

850870
// Create child scopes for every declaration in a body.
851871

872+
namespace {
873+
/// Retrieve the opaque generic parameter list if present, otherwise the normal generic parameter list.
874+
template<typename T>
875+
GenericParamList *getPotentiallyOpaqueGenericParams(T *decl) {
876+
if (auto opaqueRepr = decl->getOpaqueResultTypeRepr()) {
877+
if (auto namedOpaque = dyn_cast<NamedOpaqueReturnTypeRepr>(opaqueRepr)) {
878+
return namedOpaque->getGenericParams();
879+
}
880+
}
881+
882+
return decl->getGenericParams();
883+
}
884+
}
885+
852886
void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
853887
ScopeCreator &scopeCreator) {
854888
scopeCreator.addChildrenForKnownAttributes(decl, this);
@@ -860,7 +894,7 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
860894

861895
if (!isa<AccessorDecl>(decl)) {
862896
leaf = scopeCreator.addNestedGenericParamScopesToTree(
863-
decl, decl->getGenericParams(), leaf);
897+
decl, getPotentiallyOpaqueGenericParams(decl), leaf);
864898

865899
auto *params = decl->getParameters();
866900
if (params->size() > 0) {
@@ -1008,7 +1042,7 @@ void SubscriptDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
10081042
ScopeCreator &scopeCreator) {
10091043
scopeCreator.addChildrenForKnownAttributes(decl, this);
10101044
auto *leaf = scopeCreator.addNestedGenericParamScopesToTree(
1011-
decl, decl->getGenericParams(), this);
1045+
decl, getPotentiallyOpaqueGenericParams(decl), this);
10121046
scopeCreator.constructExpandAndInsert<ParameterListScope>(
10131047
leaf, decl->getIndices(), decl->getAccessor(AccessorKind::Get));
10141048
scopeCreator.addChildrenForParsedAccessors(decl, leaf);

lib/AST/ASTScopeLookup.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ bool GenericTypeOrExtensionScope::areMembersVisibleFromWhereClause() const {
266266
NullablePtr<const ASTScopeImpl>
267267
PatternEntryInitializerScope::getLookupParent() const {
268268
auto parent = getParent().get();
269+
270+
// Skip generic parameter scopes, which occur here due to named opaque
271+
// result types.
272+
// FIXME: Proper isa/dyn_cast support would be better than a string
273+
// comparison here.
274+
while (parent->getClassName() == "GenericParamScope")
275+
parent = parent->getLookupParent().get();
276+
269277
ASTScopeAssert(parent->getClassName() == "PatternEntryDeclScope",
270278
"PatternEntryInitializerScope in unexpected place");
271279

lib/AST/ASTScopeSourceRange.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ SourceRange GenericParamScope::getSourceRangeOfThisASTNode(
161161
return SourceRange(getLocAfterExtendedNominal(ext), ext->getEndLoc());
162162
}
163163

164+
// For a variable, the generic parameter is visible throughout the pattern
165+
// binding entry.
166+
if (auto var = dyn_cast<VarDecl>(holder)) {
167+
if (auto patternBinding = var->getParentPatternBinding()) {
168+
unsigned index = patternBinding->getPatternEntryIndexForVarDecl(var);
169+
return patternBinding->getPatternList()[index].getSourceRange();
170+
}
171+
}
172+
164173
// For all other declarations, generic parameters are visible everywhere.
165174
return holder->getSourceRange();
166175
}

lib/AST/Decl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7829,6 +7829,19 @@ bool OpaqueTypeDecl::isOpaqueReturnTypeOfFunction(
78297829
return false;
78307830
}
78317831

7832+
bool OpaqueTypeDecl::hasExplicitGenericParams() const {
7833+
return getExplicitGenericParam(0) != nullptr;
7834+
}
7835+
7836+
GenericTypeParamDecl *OpaqueTypeDecl::getExplicitGenericParam(
7837+
unsigned ordinal) const {
7838+
if (ordinal >= getOpaqueGenericParams().size())
7839+
return nullptr;
7840+
7841+
auto genericParamType = getOpaqueGenericParams()[ordinal];
7842+
return genericParamType->getDecl();
7843+
}
7844+
78327845
unsigned OpaqueTypeDecl::getAnonymousOpaqueParamOrdinal(
78337846
OpaqueReturnTypeRepr *repr) const {
78347847
assert(NamingDeclAndHasOpaqueReturnTypeRepr.getInt() &&

0 commit comments

Comments
 (0)