Skip to content

Commit 33f5c10

Browse files
committed
[Sema] Record opaque type decls for type reconstruction in OpaqueResultTypeRequest
instead of in the parser.
1 parent ee04cea commit 33f5c10

File tree

5 files changed

+22
-44
lines changed

5 files changed

+22
-44
lines changed

Diff for: include/swift/AST/SourceFile.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ 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 parsed decls with opaque return types that have not yet
153-
/// been validated.
154-
llvm::SetVector<ValueDecl *> UnvalidatedDeclsWithOpaqueReturnTypes;
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;
155158

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

644647
OpaqueTypeDecl *lookupOpaqueResultType(StringRef MangledName) override;
645648

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);
649+
void addOpaqueResultTypeDecl(OpaqueTypeDecl *decl) {
650+
UnvalidatedOpaqueReturnTypes.insert(decl);
651651
}
652652

653653
ArrayRef<OpaqueTypeDecl *> getOpaqueReturnTypeDecls();

Diff for: include/swift/Parse/Parser.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ class Parser {
177177

178178
bool InPoundLineEnvironment = false;
179179
bool InPoundIfEnvironment = false;
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.
180+
/// ASTScopes are not created in inactive clauses and lookups to decls will fail.
183181
bool InInactiveClauseEnvironment = false;
184182
bool InSwiftKeyPath = false;
185183

Diff for: lib/AST/Module.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -3484,14 +3484,12 @@ void SourceFile::setTypeRefinementContext(TypeRefinementContext *Root) {
34843484
}
34853485

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

Diff for: lib/Parse/ParseDecl.cpp

-25
Original file line numberDiff line numberDiff line change
@@ -7235,12 +7235,6 @@ 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-
72447238
// Configure all vars with attributes, 'static' and parent pattern.
72457239
pattern->forEachVariable([&](VarDecl *VD) {
72467240
VD->setStatic(StaticLoc.isValid());
@@ -7252,9 +7246,6 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
72527246
setOriginalDeclarationForDifferentiableAttributes(Attributes, VD);
72537247

72547248
Decls.push_back(VD);
7255-
if (hasOpaqueReturnTy && sf && !InInactiveClauseEnvironment) {
7256-
sf->addUnvalidatedDeclWithOpaqueResultType(VD);
7257-
}
72587249
});
72597250

72607251
// Check whether we have already established an initializer context.
@@ -7541,14 +7532,6 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
75417532
GenericParams,
75427533
BodyParams, FuncRetTy,
75437534
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-
}
75527535

75537536
// Parse a 'where' clause if present.
75547537
if (Tok.is(tok::kw_where)) {
@@ -8512,14 +8495,6 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
85128495
Context, name, StaticLoc, StaticSpelling, SubscriptLoc, Indices.get(),
85138496
ArrowLoc, ElementTy.get(), CurDeclContext, GenericParams);
85148497
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-
}
85238498

85248499
DefaultArgs.setFunctionContext(Subscript, Subscript->getIndices());
85258500

Diff for: lib/Sema/TypeCheckGeneric.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ 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+
254261
return opaqueDecl;
255262
}
256263

0 commit comments

Comments
 (0)