Skip to content

Commit 09bb27c

Browse files
hborlaangela-laar
authored andcommitted
Merge pull request swiftlang#62075 from hborla/implicit-some-type-reconstruction
[Sema] Record opaque type decls for type reconstruction after creation instead of in the parser.
1 parent cba7389 commit 09bb27c

File tree

5 files changed

+22
-44
lines changed

5 files changed

+22
-44
lines changed

include/swift/AST/SourceFile.h

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

153156
/// The set of declarations with valid runtime discoverable attributes
154157
/// located in the source file.
@@ -661,11 +664,8 @@ class SourceFile final : public FileUnit {
661664

662665
OpaqueTypeDecl *lookupOpaqueResultType(StringRef MangledName) override;
663666

664-
/// Do not call when inside an inactive clause (\c
665-
/// InInactiveClauseEnvironment)) because it will later on result in a lookup
666-
/// to something that won't be in the ASTScope tree.
667-
void addUnvalidatedDeclWithOpaqueResultType(ValueDecl *vd) {
668-
UnvalidatedDeclsWithOpaqueReturnTypes.insert(vd);
667+
void addOpaqueResultTypeDecl(OpaqueTypeDecl *decl) {
668+
UnvalidatedOpaqueReturnTypes.insert(decl);
669669
}
670670

671671
ArrayRef<OpaqueTypeDecl *> getOpaqueReturnTypeDecls();

include/swift/Parse/Parser.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ class Parser {
172172

173173
bool InPoundLineEnvironment = false;
174174
bool InPoundIfEnvironment = false;
175-
/// Do not call \c addUnvalidatedDeclWithOpaqueResultType when in an inactive
176-
/// clause because ASTScopes are not created in those contexts and lookups to
177-
/// those decls will fail.
175+
/// ASTScopes are not created in inactive clauses and lookups to decls will fail.
178176
bool InInactiveClauseEnvironment = false;
179177
bool InSwiftKeyPath = false;
180178

lib/AST/Module.cpp

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

36153615
ArrayRef<OpaqueTypeDecl *> SourceFile::getOpaqueReturnTypeDecls() {
3616-
for (auto *vd : UnvalidatedDeclsWithOpaqueReturnTypes.takeVector()) {
3617-
if (auto opaqueDecl = vd->getOpaqueResultTypeDecl()) {
3618-
auto inserted = ValidatedOpaqueReturnTypes.insert(
3619-
{opaqueDecl->getOpaqueReturnTypeIdentifier().str(),
3620-
opaqueDecl});
3621-
if (inserted.second) {
3622-
OpaqueReturnTypes.push_back(opaqueDecl);
3623-
}
3616+
for (auto *opaqueDecl : UnvalidatedOpaqueReturnTypes.takeVector()) {
3617+
auto inserted = ValidatedOpaqueReturnTypes.insert(
3618+
{opaqueDecl->getOpaqueReturnTypeIdentifier().str(),
3619+
opaqueDecl});
3620+
if (inserted.second) {
3621+
OpaqueReturnTypes.push_back(opaqueDecl);
36243622
}
36253623
}
36263624

lib/Parse/ParseDecl.cpp

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

7509-
bool hasOpaqueReturnTy = false;
7510-
if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
7511-
hasOpaqueReturnTy = typedPattern->getTypeRepr()->hasOpaque();
7512-
}
7513-
auto sf = CurDeclContext->getParentSourceFile();
7514-
75157509
// Configure all vars with attributes, 'static' and parent pattern.
75167510
pattern->forEachVariable([&](VarDecl *VD) {
75177511
VD->setStatic(StaticLoc.isValid());
@@ -7522,9 +7516,6 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
75227516
setOriginalDeclarationForDifferentiableAttributes(Attributes, VD);
75237517

75247518
Decls.push_back(VD);
7525-
if (hasOpaqueReturnTy && sf && !InInactiveClauseEnvironment) {
7526-
sf->addUnvalidatedDeclWithOpaqueResultType(VD);
7527-
}
75287519
});
75297520

75307521
// Check whether we have already established an initializer context.
@@ -7809,14 +7800,6 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
78097800
GenericParams,
78107801
BodyParams, FuncRetTy,
78117802
CurDeclContext);
7812-
7813-
// Let the source file track the opaque return type mapping, if any.
7814-
if (FuncRetTy && FuncRetTy->hasOpaque() &&
7815-
!InInactiveClauseEnvironment) {
7816-
if (auto sf = CurDeclContext->getParentSourceFile()) {
7817-
sf->addUnvalidatedDeclWithOpaqueResultType(FD);
7818-
}
7819-
}
78207803

78217804
// Parse a 'where' clause if present.
78227805
if (Tok.is(tok::kw_where)) {
@@ -8747,14 +8730,6 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
87478730
Context, name, StaticLoc, StaticSpelling, SubscriptLoc, Indices.get(),
87488731
ArrowLoc, ElementTy.get(), CurDeclContext, GenericParams);
87498732
Subscript->getAttrs() = Attributes;
8750-
8751-
// Let the source file track the opaque return type mapping, if any.
8752-
if (ElementTy.get() && ElementTy.get()->hasOpaque() &&
8753-
!InInactiveClauseEnvironment) {
8754-
if (auto sf = CurDeclContext->getParentSourceFile()) {
8755-
sf->addUnvalidatedDeclWithOpaqueResultType(Subscript);
8756-
}
8757-
}
87588733

87598734
DefaultArgs.setFunctionContext(Subscript, Subscript->getIndices());
87608735

lib/Sema/TypeCheckGeneric.cpp

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

263263
auto metatype = MetatypeType::get(interfaceType);
264264
opaqueDecl->setInterfaceType(metatype);
265+
266+
// Record the opaque return type decl in the parent source file,
267+
// which will be used in IRGen to emit all opaque type decls
268+
// in a Swift module for type reconstruction.
269+
if (auto *sourceFile = dc->getParentSourceFile())
270+
sourceFile->addOpaqueResultTypeDecl(opaqueDecl);
271+
265272
return opaqueDecl;
266273
}
267274

0 commit comments

Comments
 (0)