Skip to content

Commit 330b0a2

Browse files
authored
Merge pull request #2534 from swiftwasm/release/5.4
[pull] swiftwasm-release/5.4 from release/5.4
2 parents 1ddedf9 + 0d0f08c commit 330b0a2

20 files changed

+248
-50
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class ASTMangler : public Mangler {
222222
GenericSignature signature,
223223
ResilienceExpansion expansion);
224224

225-
std::string mangleTypeForDebugger(Type decl, const DeclContext *DC);
225+
std::string mangleTypeForDebugger(Type decl, GenericSignature sig);
226226

227227
/// Create a mangled name to be used for _typeName constant propagation.
228228
std::string mangleTypeForTypeName(Type type);
@@ -280,9 +280,7 @@ class ASTMangler : public Mangler {
280280
void appendOpWithGenericParamIndex(StringRef,
281281
const GenericTypeParamType *paramTy);
282282

283-
void bindGenericParameters(const DeclContext *DC);
284-
285-
void bindGenericParameters(CanGenericSignature sig);
283+
void bindGenericParameters(GenericSignature sig);
286284

287285
/// Mangles a sugared type iff we are mangling for the debugger.
288286
template <class T> void appendSugaredType(Type type,

include/swift/AST/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
626626
/// This assumes that \p module was imported.
627627
bool isImportedImplementationOnly(const ModuleDecl *module) const;
628628

629+
/// Returns true if a function, which is using \p nominal, can be serialized
630+
/// by cross-module-optimization.
631+
bool canBeUsedForCrossModuleOptimization(NominalTypeDecl *nominal) const;
632+
629633
/// Finds all top-level decls of this module.
630634
///
631635
/// This does a simple local lookup, not recursively looking through imports.

lib/AST/ASTMangler.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ static Type getTypeForDWARFMangling(Type t) {
538538
SubstFlags::AllowLoweredTypes);
539539
}
540540

541-
std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
541+
std::string ASTMangler::mangleTypeForDebugger(Type Ty, GenericSignature sig) {
542542
PrettyStackTraceType prettyStackTrace(Ty->getASTContext(),
543543
"mangling type for debugger", Ty);
544544

@@ -548,9 +548,7 @@ std::string ASTMangler::mangleTypeForDebugger(Type Ty, const DeclContext *DC) {
548548

549549
Ty = getTypeForDWARFMangling(Ty);
550550

551-
if (DC)
552-
bindGenericParameters(DC);
553-
551+
bindGenericParameters(sig);
554552
appendType(Ty);
555553
appendOperator("D");
556554
return finalize();
@@ -676,7 +674,9 @@ std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
676674
beginManglingWithoutPrefix();
677675
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
678676
Buffer << USRPrefix;
679-
bindGenericParameters(Decl->getDeclContext());
677+
678+
auto Sig = Decl->getInnermostDeclContext()->getGenericSignatureOfContext();
679+
bindGenericParameters(Sig);
680680

681681
if (auto Ctor = dyn_cast<ConstructorDecl>(Decl)) {
682682
appendConstructorEntity(Ctor, /*isAllocating=*/false);
@@ -1369,15 +1369,9 @@ void ASTMangler::appendOpWithGenericParamIndex(StringRef Op,
13691369

13701370

13711371
/// Bind the generic parameters from the given signature.
1372-
void ASTMangler::bindGenericParameters(CanGenericSignature sig) {
1372+
void ASTMangler::bindGenericParameters(GenericSignature sig) {
13731373
if (sig)
1374-
CurGenericSignature = sig;
1375-
}
1376-
1377-
/// Bind the generic parameters from the given context and its parents.
1378-
void ASTMangler::bindGenericParameters(const DeclContext *DC) {
1379-
if (auto sig = DC->getGenericSignatureOfContext())
1380-
bindGenericParameters(sig.getCanonicalSignature());
1374+
CurGenericSignature = sig.getCanonicalSignature();
13811375
}
13821376

13831377
void ASTMangler::appendFlatGenericArgs(SubstitutionMap subs) {
@@ -2853,12 +2847,13 @@ void ASTMangler::appendAccessorEntity(StringRef accessorKindCode,
28532847
const AbstractStorageDecl *decl,
28542848
bool isStatic) {
28552849
appendContextOf(decl);
2856-
bindGenericParameters(decl->getDeclContext());
2857-
if (isa<VarDecl>(decl)) {
2850+
if (auto *varDecl = dyn_cast<VarDecl>(decl)) {
2851+
bindGenericParameters(varDecl->getDeclContext()->getGenericSignatureOfContext());
28582852
appendDeclName(decl);
28592853
appendDeclType(decl);
28602854
appendOperator("v", accessorKindCode);
2861-
} else if (isa<SubscriptDecl>(decl)) {
2855+
} else if (auto *subscriptDecl = dyn_cast<SubscriptDecl>(decl)) {
2856+
bindGenericParameters(subscriptDecl->getGenericSignature());
28622857
appendDeclType(decl);
28632858

28642859
StringRef privateDiscriminator = getPrivateDiscriminatorIfNecessary(decl);

lib/AST/Module.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,30 @@ bool ModuleDecl::isImportedImplementationOnly(const ModuleDecl *module) const {
20122012
return true;
20132013
}
20142014

2015+
bool ModuleDecl::
2016+
canBeUsedForCrossModuleOptimization(NominalTypeDecl *nominal) const {
2017+
ModuleDecl *moduleOfNominal = nominal->getParentModule();
2018+
2019+
// If the nominal is defined in the same module, it's fine.
2020+
if (moduleOfNominal == this)
2021+
return true;
2022+
2023+
// See if nominal is imported in a "regular" way, i.e. not with
2024+
// @_implementationOnly or @_spi.
2025+
ModuleDecl::ImportFilter filter = {
2026+
ModuleDecl::ImportFilterKind::Exported,
2027+
ModuleDecl::ImportFilterKind::Default};
2028+
SmallVector<ImportedModule, 4> results;
2029+
getImportedModules(results, filter);
2030+
2031+
auto &imports = getASTContext().getImportCache();
2032+
for (auto &desc : results) {
2033+
if (imports.isImportedBy(moduleOfNominal, desc.importedModule))
2034+
return true;
2035+
}
2036+
return false;
2037+
}
2038+
20152039
void SourceFile::lookupImportedSPIGroups(
20162040
const ModuleDecl *importedModule,
20172041
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
816816
->getKey();
817817

818818
Type Ty = DbgTy.getType();
819-
if (!Ty->hasTypeParameter())
819+
if (Ty->hasArchetype())
820820
Ty = Ty->mapTypeOutOfContext();
821821

822822
// Strip off top level of type sugar (except for type aliases).
@@ -842,7 +842,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
842842
IGM.getSILModule());
843843

844844
Mangle::ASTMangler Mangler;
845-
std::string Result = Mangler.mangleTypeForDebugger(Ty, nullptr);
845+
GenericSignature Sig = IGM.getCurGenericContext();
846+
std::string Result = Mangler.mangleTypeForDebugger(Ty, Sig);
846847

847848
if (!Opts.DisableRoundTripDebugTypes) {
848849
// Make sure we can reconstruct mangled types for the debugger.

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2765,7 +2765,8 @@ bool LoadableByAddress::recreateTupleInstr(
27652765
for (auto elem : tupleInstr->getElements()) {
27662766
elems.push_back(elem);
27672767
}
2768-
auto *newTuple = tupleBuilder.createTuple(tupleInstr->getLoc(), elems);
2768+
auto *newTuple = tupleBuilder.createTuple(tupleInstr->getLoc(), newResultTy,
2769+
elems);
27692770
tupleInstr->replaceAllUsesWith(newTuple);
27702771
Delete.push_back(tupleInstr);
27712772
return true;

lib/SILOptimizer/IPO/CrossModuleSerializationSetup.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class CrossModuleSerializationSetup {
4545
llvm::SmallVector<SILFunction *, 16> workList;
4646
llvm::SmallPtrSet<SILFunction *, 16> functionsHandled;
4747

48+
llvm::DenseMap<SILType, bool> typesChecked;
4849
llvm::SmallPtrSet<TypeBase *, 16> typesHandled;
4950

5051
SILModule &M;
@@ -62,6 +63,8 @@ class CrossModuleSerializationSetup {
6263

6364
bool canSerialize(SILInstruction *inst, bool lookIntoThunks);
6465

66+
bool canSerialize(SILType type);
67+
6568
void setUpForSerialization(SILFunction *F);
6669

6770
void prepareInstructionForSerialization(SILInstruction *inst);
@@ -320,6 +323,12 @@ bool CrossModuleSerializationSetup::canSerialize(SILFunction *F,
320323

321324
bool CrossModuleSerializationSetup::canSerialize(SILInstruction *inst,
322325
bool lookIntoThunks) {
326+
327+
for (SILValue result : inst->getResults()) {
328+
if (!canSerialize(result->getType()))
329+
return false;
330+
}
331+
323332
if (auto *FRI = dyn_cast<FunctionRefBaseInst>(inst)) {
324333
SILFunction *callee = FRI->getReferencedFunctionOrNull();
325334
return canUseFromInline(callee, lookIntoThunks);
@@ -344,6 +353,31 @@ bool CrossModuleSerializationSetup::canSerialize(SILInstruction *inst,
344353
return true;
345354
}
346355

356+
bool CrossModuleSerializationSetup::canSerialize(SILType type) {
357+
auto iter = typesChecked.find(type);
358+
if (iter != typesChecked.end())
359+
return iter->getSecond();
360+
361+
ModuleDecl *mod = M.getSwiftModule();
362+
bool success = !type.getASTType().findIf(
363+
[mod](Type rawSubType) {
364+
CanType subType = rawSubType->getCanonicalType();
365+
if (NominalTypeDecl *subNT = subType->getNominalOrBoundGenericNominal()) {
366+
367+
// Exclude types which are defined in an @_implementationOnly imported
368+
// module. Such modules are not transitively available.
369+
if (!mod->canBeUsedForCrossModuleOptimization(subNT)) {
370+
llvm::dbgs() << " === " << mod->getName() << ", " <<
371+
subNT->getParentModule()->getName() << ", " << subNT->getName() << '\n';
372+
return true;
373+
}
374+
}
375+
return false;
376+
});
377+
typesChecked[type] = success;
378+
return success;
379+
}
380+
347381
/// Returns true if the function \p func can be used from a serialized function.
348382
///
349383
/// If \p lookIntoThunks is true, serializable shared thunks are also accepted.

lib/Sema/CSDiagnostics.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4812,13 +4812,27 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() {
48124812
auto text = SM.extractText(
48134813
Lexer::getCharSourceRangeFromSourceRange(SM, firstRange));
48144814

4815-
auto removalRange =
4816-
SourceRange(Lexer::getLocForEndOfToken(
4817-
SM, tuple->getElement(ArgIdx - 1)->getEndLoc()),
4818-
firstRange.End);
4815+
SourceLoc removalStartLoc;
4816+
// For the first argument, start is always next token after `(`.
4817+
if (ArgIdx == 0) {
4818+
removalStartLoc = tuple->getLParenLoc();
4819+
} else {
4820+
// For all other arguments, start is the next token past
4821+
// the previous argument.
4822+
removalStartLoc = tuple->getElement(ArgIdx - 1)->getEndLoc();
4823+
}
4824+
4825+
SourceRange removalRange{Lexer::getLocForEndOfToken(SM, removalStartLoc),
4826+
firstRange.End};
4827+
4828+
// Move requires postfix comma only if argument is moved in-between
4829+
// other arguments.
4830+
bool requiresComma = !isExpr<BinaryExpr>(anchor) &&
4831+
PrevArgIdx != tuple->getNumElements() - 1;
4832+
48194833
diag.fixItRemove(removalRange);
48204834
diag.fixItInsert(secondRange.Start,
4821-
text.str() + (isExpr<BinaryExpr>(anchor) ? "" : ", "));
4835+
text.str() + (requiresComma ? ", " : ""));
48224836
};
48234837

48244838
// There are 4 diagnostic messages variations depending on

lib/Sema/CodeSynthesis.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Expr *swift::buildSelfReference(VarDecl *selfDecl,
6262
selfTy = metaTy->getInstanceType();
6363
}
6464
selfTy = selfTy->getSuperclass();
65+
if (!selfTy) {
66+
// Error recovery path. We end up here if getSuperclassDecl() succeeds
67+
// but getSuperclass() fails (because, for instance, a generic parameter
68+
// of a generic nominal type cannot be resolved).
69+
selfTy = ErrorType::get(ctx);
70+
}
6571
if (isMetatype)
6672
selfTy = MetatypeType::get(selfTy);
6773

@@ -70,7 +76,7 @@ Expr *swift::buildSelfReference(VarDecl *selfDecl,
7076

7177
// If no conversion type was specified, or we're already at that type, we're
7278
// done.
73-
if (!convertTy || convertTy->isEqual(selfTy))
79+
if (!convertTy || convertTy->isEqual(selfTy) || selfTy->is<ErrorType>())
7480
return superRef;
7581

7682
// Insert the appropriate expr to handle the upcast.

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ namespace {
151151
auto conformance = DC->getParentModule()->lookupConformance(
152152
conformingType, foundProto);
153153
if (conformance.isInvalid()) {
154-
// If there's no conformance, we have an existential
155-
// and we found a member from one of the protocols, and
156-
// not a class constraint if any.
157-
assert(foundInType->isExistentialType() || foundInType->hasError());
158-
if (foundInType->isExistentialType())
154+
if (foundInType->isExistentialType()) {
155+
// If there's no conformance, we have an existential
156+
// and we found a member from one of the protocols, and
157+
// not a class constraint if any.
159158
addResult(found);
159+
}
160160
return;
161161
}
162162

0 commit comments

Comments
 (0)