Skip to content

Commit 020281a

Browse files
authored
Merge pull request #63468 from angela-laar/implicit-some-reconstruction
[Sema] Record opaque type decls for type reconstruction after creation instead of in the parser
2 parents 75f53a9 + 800a966 commit 020281a

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

include/swift/AST/SourceFile.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,19 @@ class SourceFile final : public FileUnit {
143143
/// The scope map that describes this source file.
144144
NullablePtr<ASTScope> Scope = nullptr;
145145

146+
/// The set of parsed decls with opaque return types that have not yet
147+
/// been validated.
148+
llvm::SetVector<ValueDecl *> UnvalidatedDeclsWithOpaqueReturnTypes;
149+
146150
/// The set of validated opaque return type decls in the source file.
147151
llvm::SmallVector<OpaqueTypeDecl *, 4> OpaqueReturnTypes;
148152
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;
153+
/// The set of opaque type decls that have not yet been validated.
154+
///
155+
/// \note This is populated as opaque type decls are created. Validation
156+
/// requires mangling the naming decl, which would lead to circularity
157+
/// if it were done from OpaqueResultTypeRequest.
158+
llvm::SetVector<OpaqueTypeDecl *> UnvalidatedOpaqueReturnTypes;
152159

153160
/// The set of declarations with valid runtime discoverable attributes
154161
/// located in the source file.
@@ -668,6 +675,10 @@ class SourceFile final : public FileUnit {
668675
UnvalidatedDeclsWithOpaqueReturnTypes.insert(vd);
669676
}
670677

678+
void addOpaqueResultTypeDecl(OpaqueTypeDecl *decl) {
679+
UnvalidatedOpaqueReturnTypes.insert(decl);
680+
}
681+
671682
ArrayRef<OpaqueTypeDecl *> getOpaqueReturnTypeDecls();
672683

673684
/// Returns true if the source file contains concurrency in the top-level

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/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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func bar() {
2+
let x = foo()
3+
}

test/IRGen/implicit_some_a.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-frontend -emit-ir -disable-availability-checking -primary-file %s %S/Inputs/implicit_some_b.swift -enable-experimental-feature ImplicitSome
2+
3+
protocol P {}
4+
struct S: P {}
5+
6+
func foo() -> P { return S() }

test/type/implicit_some/opaque_parameters.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func consumingB(fn: FnType<P>) { } // expected-error{{'some' cannot appear in pa
6464

6565
// TO-DO Handle plain generic opaque parameters
6666
func takePrimaryCollections(
67-
_ strings:some Collection<String>,
67+
_ strings:some Collection<String>,
6868
_ ints : some Collection<Int>
6969
) {
7070
for s in strings {

0 commit comments

Comments
 (0)