Skip to content

Commit 59d62be

Browse files
committed
Only add explicit opaque types to module interfaces
1 parent 09bb27c commit 59d62be

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

include/swift/AST/SourceFile.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ 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;
@@ -664,6 +668,13 @@ class SourceFile final : public FileUnit {
664668

665669
OpaqueTypeDecl *lookupOpaqueResultType(StringRef MangledName) override;
666670

671+
/// Do not call when inside an inactive clause (\c
672+
/// InInactiveClauseEnvironment)) because it will later on result in a lookup
673+
/// to something that won't be in the ASTScope tree.
674+
void addUnvalidatedDeclWithOpaqueResultType(ValueDecl *vd) {
675+
UnvalidatedDeclsWithOpaqueReturnTypes.insert(vd);
676+
}
677+
667678
void addOpaqueResultTypeDecl(OpaqueTypeDecl *decl) {
668679
UnvalidatedOpaqueReturnTypes.insert(decl);
669680
}

lib/AST/Module.cpp

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

36153615
ArrayRef<OpaqueTypeDecl *> SourceFile::getOpaqueReturnTypeDecls() {
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);
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+
}
36223624
}
36233625
}
36243626

lib/Parse/ParseDecl.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7506,6 +7506,12 @@ 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+
75097515
// Configure all vars with attributes, 'static' and parent pattern.
75107516
pattern->forEachVariable([&](VarDecl *VD) {
75117517
VD->setStatic(StaticLoc.isValid());
@@ -7516,6 +7522,9 @@ Parser::parseDeclVar(ParseDeclOptions Flags,
75167522
setOriginalDeclarationForDifferentiableAttributes(Attributes, VD);
75177523

75187524
Decls.push_back(VD);
7525+
if (hasOpaqueReturnTy && sf && !InInactiveClauseEnvironment) {
7526+
sf->addUnvalidatedDeclWithOpaqueResultType(VD);
7527+
}
75197528
});
75207529

75217530
// Check whether we have already established an initializer context.
@@ -7800,6 +7809,14 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
78007809
GenericParams,
78017810
BodyParams, FuncRetTy,
78027811
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+
}
78037820

78047821
// Parse a 'where' clause if present.
78057822
if (Tok.is(tok::kw_where)) {
@@ -8730,6 +8747,14 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
87308747
Context, name, StaticLoc, StaticSpelling, SubscriptLoc, Indices.get(),
87318748
ArrowLoc, ElementTy.get(), CurDeclContext, GenericParams);
87328749
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+
}
87338758

87348759
DefaultArgs.setFunctionContext(Subscript, Subscript->getIndices());
87358760

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 -primary-file %s %S/Inputs/implicit_some_b.swift -enable-experimental-feature ImplicitSome -emit-ir
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)