Skip to content

Commit 2213a02

Browse files
authored
Revert "[Sema] Record opaque type decls for type reconstruction after creation instead of in the parser."
1 parent 5e208ff commit 2213a02

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

include/swift/AST/SourceFile.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,9 @@ class SourceFile final : public FileUnit {
149149
/// The set of validated opaque return type decls in the source file.
150150
llvm::SmallVector<OpaqueTypeDecl *, 4> OpaqueReturnTypes;
151151
llvm::StringMap<OpaqueTypeDecl *> ValidatedOpaqueReturnTypes;
152-
/// The set of opaque type decls that have not yet been validated.
153-
///
154-
/// \note This is populated as opaque type decls are created. Validation
155-
/// requires mangling the naming decl, which would lead to circularity
156-
/// if it were done from OpaqueResultTypeRequest.
157-
llvm::SetVector<OpaqueTypeDecl *> UnvalidatedOpaqueReturnTypes;
152+
/// The set of parsed decls with opaque return types that have not yet
153+
/// been validated.
154+
llvm::SetVector<ValueDecl *> UnvalidatedDeclsWithOpaqueReturnTypes;
158155

159156
/// The list of top-level items in the source file. This is \c None if
160157
/// they have not yet been parsed.
@@ -646,8 +643,11 @@ class SourceFile final : public FileUnit {
646643

647644
OpaqueTypeDecl *lookupOpaqueResultType(StringRef MangledName) override;
648645

649-
void addOpaqueResultTypeDecl(OpaqueTypeDecl *decl) {
650-
UnvalidatedOpaqueReturnTypes.insert(decl);
646+
/// Do not call when inside an inactive clause (\c
647+
/// InInactiveClauseEnvironment)) because it will later on result in a lookup
648+
/// to something that won't be in the ASTScope tree.
649+
void addUnvalidatedDeclWithOpaqueResultType(ValueDecl *vd) {
650+
UnvalidatedDeclsWithOpaqueReturnTypes.insert(vd);
651651
}
652652

653653
ArrayRef<OpaqueTypeDecl *> getOpaqueReturnTypeDecls();

include/swift/Parse/Parser.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ class Parser {
177177

178178
bool InPoundLineEnvironment = false;
179179
bool InPoundIfEnvironment = false;
180-
/// ASTScopes are not created in inactive clauses and lookups to decls will fail.
180+
/// Do not call \c addUnvalidatedDeclWithOpaqueResultType when in an inactive
181+
/// clause because ASTScopes are not created in those contexts and lookups to
182+
/// those decls will fail.
181183
bool InInactiveClauseEnvironment = false;
182184
bool InSwiftKeyPath = false;
183185

lib/AST/Module.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,12 +3485,14 @@ void SourceFile::setTypeRefinementContext(TypeRefinementContext *Root) {
34853485
}
34863486

34873487
ArrayRef<OpaqueTypeDecl *> SourceFile::getOpaqueReturnTypeDecls() {
3488-
for (auto *opaqueDecl : UnvalidatedOpaqueReturnTypes.takeVector()) {
3489-
auto inserted = ValidatedOpaqueReturnTypes.insert(
3490-
{opaqueDecl->getOpaqueReturnTypeIdentifier().str(),
3491-
opaqueDecl});
3492-
if (inserted.second) {
3493-
OpaqueReturnTypes.push_back(opaqueDecl);
3488+
for (auto *vd : UnvalidatedDeclsWithOpaqueReturnTypes.takeVector()) {
3489+
if (auto opaqueDecl = vd->getOpaqueResultTypeDecl()) {
3490+
auto inserted = ValidatedOpaqueReturnTypes.insert(
3491+
{opaqueDecl->getOpaqueReturnTypeIdentifier().str(),
3492+
opaqueDecl});
3493+
if (inserted.second) {
3494+
OpaqueReturnTypes.push_back(opaqueDecl);
3495+
}
34943496
}
34953497
}
34963498

lib/Parse/ParseDecl.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7235,6 +7235,12 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
72357235
pattern = patternRes.get();
72367236
}
72377237

7238+
bool hasOpaqueReturnTy = false;
7239+
if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
7240+
hasOpaqueReturnTy = typedPattern->getTypeRepr()->hasOpaque();
7241+
}
7242+
auto sf = CurDeclContext->getParentSourceFile();
7243+
72387244
// Configure all vars with attributes, 'static' and parent pattern.
72397245
pattern->forEachVariable([&](VarDecl *VD) {
72407246
VD->setStatic(StaticLoc.isValid());
@@ -7246,6 +7252,9 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
72467252
setOriginalDeclarationForDifferentiableAttributes(Attributes, VD);
72477253

72487254
Decls.push_back(VD);
7255+
if (hasOpaqueReturnTy && sf && !InInactiveClauseEnvironment) {
7256+
sf->addUnvalidatedDeclWithOpaqueResultType(VD);
7257+
}
72497258
});
72507259

72517260
// Check whether we have already established an initializer context.
@@ -7532,6 +7541,14 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
75327541
GenericParams,
75337542
BodyParams, FuncRetTy,
75347543
CurDeclContext);
7544+
7545+
// Let the source file track the opaque return type mapping, if any.
7546+
if (FuncRetTy && FuncRetTy->hasOpaque() &&
7547+
!InInactiveClauseEnvironment) {
7548+
if (auto sf = CurDeclContext->getParentSourceFile()) {
7549+
sf->addUnvalidatedDeclWithOpaqueResultType(FD);
7550+
}
7551+
}
75357552

75367553
// Parse a 'where' clause if present.
75377554
if (Tok.is(tok::kw_where)) {
@@ -8495,6 +8512,14 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
84958512
Context, name, StaticLoc, StaticSpelling, SubscriptLoc, Indices.get(),
84968513
ArrowLoc, ElementTy.get(), CurDeclContext, GenericParams);
84978514
Subscript->getAttrs() = Attributes;
8515+
8516+
// Let the source file track the opaque return type mapping, if any.
8517+
if (ElementTy.get() && ElementTy.get()->hasOpaque() &&
8518+
!InInactiveClauseEnvironment) {
8519+
if (auto sf = CurDeclContext->getParentSourceFile()) {
8520+
sf->addUnvalidatedDeclWithOpaqueResultType(Subscript);
8521+
}
8522+
}
84988523

84998524
DefaultArgs.setFunctionContext(Subscript, Subscript->getIndices());
85008525

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,6 @@ OpaqueResultTypeRequest::evaluate(Evaluator &evaluator,
251251

252252
auto metatype = MetatypeType::get(interfaceType);
253253
opaqueDecl->setInterfaceType(metatype);
254-
255-
// Record the opaque return type decl in the parent source file,
256-
// which will be used in IRGen to emit all opaque type decls
257-
// in a Swift module for type reconstruction.
258-
if (auto *sourceFile = dc->getParentSourceFile())
259-
sourceFile->addOpaqueResultTypeDecl(opaqueDecl);
260-
261254
return opaqueDecl;
262255
}
263256

0 commit comments

Comments
 (0)