Skip to content

Commit dddf0ec

Browse files
author
Benjamin Driscoll
committed
[IDE][Parse] Address CR for new named opaque return type syntax
1 parent d2de21a commit dddf0ec

File tree

12 files changed

+84
-53
lines changed

12 files changed

+84
-53
lines changed

include/swift/AST/GenericParamList.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ class GenericParamList final :
339339
/// to the given DeclContext.
340340
GenericParamList *clone(DeclContext *dc) const;
341341

342-
void print(raw_ostream &OS) const;
343-
SWIFT_DEBUG_DUMP;
344-
345342
bool walk(ASTWalker &walker);
346343

347344
/// Finds a generic parameter declaration by name. This should only
348345
/// be used from the SIL parser.
349346
GenericTypeParamDecl *lookUpGenericParam(Identifier name) const;
347+
348+
SWIFT_DEBUG_DUMP;
349+
void print(raw_ostream &OS, const PrintOptions &PO = PrintOptions()) const;
350+
void print(ASTPrinter &Printer, const PrintOptions &PO) const;
350351
};
351352

352353
/// A trailing where clause.

include/swift/AST/TypeRepr.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,23 +1109,25 @@ class OpaqueReturnTypeRepr : public TypeRepr {
11091109
/// constraints that the concrete types satisfy:
11101110
///
11111111
/// func foo() -> <T: Collection> T { return [1] }
1112-
class OpaqueReturnParameterizedTypeRepr : public TypeRepr {
1112+
class NamedOpaqueReturnTypeRepr : public TypeRepr {
11131113
TypeRepr *Base;
11141114
GenericParamList *GenericParams;
11151115

11161116
public:
1117-
OpaqueReturnParameterizedTypeRepr(TypeRepr *Base,
1117+
NamedOpaqueReturnTypeRepr(TypeRepr *Base,
11181118
GenericParamList *GenericParams)
1119-
: TypeRepr(TypeReprKind::OpaqueReturnParameterized), Base(Base),
1120-
GenericParams(GenericParams) {}
1119+
: TypeRepr(TypeReprKind::NamedOpaqueReturn), Base(Base),
1120+
GenericParams(GenericParams) {
1121+
assert(Base && GenericParams);
1122+
}
11211123

11221124
TypeRepr *getBase() const { return Base; }
11231125
GenericParamList *getGenericParams() const { return GenericParams; }
11241126

11251127
static bool classof(const TypeRepr *T) {
1126-
return T->getKind() == TypeReprKind::OpaqueReturnParameterized;
1128+
return T->getKind() == TypeReprKind::NamedOpaqueReturn;
11271129
}
1128-
static bool classof(const OpaqueReturnParameterizedTypeRepr *T) {
1130+
static bool classof(const NamedOpaqueReturnTypeRepr *T) {
11291131
return true;
11301132
}
11311133

@@ -1263,6 +1265,7 @@ inline bool TypeRepr::isSimple() const {
12631265
case TypeReprKind::InOut:
12641266
case TypeReprKind::Composition:
12651267
case TypeReprKind::OpaqueReturn:
1268+
case TypeReprKind::NamedOpaqueReturn:
12661269
return false;
12671270
case TypeReprKind::SimpleIdent:
12681271
case TypeReprKind::GenericIdent:
@@ -1281,8 +1284,6 @@ inline bool TypeRepr::isSimple() const {
12811284
case TypeReprKind::Isolated:
12821285
case TypeReprKind::Placeholder:
12831286
return true;
1284-
case TypeReprKind::OpaqueReturnParameterized:
1285-
return cast<OpaqueReturnParameterizedTypeRepr>(this)->getBase()->isSimple();
12861287
}
12871288
llvm_unreachable("bad TypeRepr kind");
12881289
}

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ TYPEREPR(Composition, TypeRepr)
5454
TYPEREPR(Metatype, TypeRepr)
5555
TYPEREPR(Protocol, TypeRepr)
5656
TYPEREPR(OpaqueReturn, TypeRepr)
57-
TYPEREPR(OpaqueReturnParameterized, TypeRepr)
57+
TYPEREPR(NamedOpaqueReturn, TypeRepr)
5858
TYPEREPR(Placeholder, TypeRepr)
5959
ABSTRACT_TYPEREPR(Specifier, TypeRepr)
6060
TYPEREPR(InOut, SpecifierTypeRepr)

lib/AST/ASTDumper.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ void RequirementRepr::print(ASTPrinter &out) const {
168168
printImpl(out);
169169
}
170170

171+
void GenericParamList::dump() const {
172+
print(llvm::errs());
173+
llvm::errs() << '\n';
174+
}
175+
176+
void GenericParamList::print(raw_ostream &out, const PrintOptions &PO) const {
177+
StreamPrinter printer(out);
178+
print(printer, PO);
179+
}
180+
171181
static void printTrailingRequirements(ASTPrinter &Printer,
172182
ArrayRef<RequirementRepr> Reqs,
173183
bool printWhereKeyword) {
@@ -185,27 +195,21 @@ static void printTrailingRequirements(ASTPrinter &Printer,
185195
[&] { Printer << ", "; });
186196
}
187197

188-
void GenericParamList::print(llvm::raw_ostream &OS) const {
189-
OS << '<';
198+
void GenericParamList::print(ASTPrinter &Printer, const PrintOptions &PO) const {
199+
Printer << '<';
190200
interleave(*this,
191201
[&](const GenericTypeParamDecl *P) {
192-
OS << P->getName();
202+
Printer << P->getName();
193203
if (!P->getInherited().empty()) {
194-
OS << " : ";
195-
P->getInherited()[0].getType().print(OS);
204+
Printer << " : ";
205+
P->getInherited()[0].getType().print(Printer, PO);
196206
}
197207
},
198-
[&] { OS << ", "; });
208+
[&] { Printer << ", "; });
199209

200-
StreamPrinter Printer(OS);
201210
printTrailingRequirements(Printer, getRequirements(),
202211
/*printWhereKeyword*/true);
203-
OS << '>';
204-
}
205-
206-
void GenericParamList::dump() const {
207-
print(llvm::errs());
208-
llvm::errs() << '\n';
212+
Printer << '>';
209213
}
210214

211215
void TrailingWhereClause::print(llvm::raw_ostream &OS,
@@ -3140,7 +3144,7 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
31403144
}
31413145

31423146
void
3143-
visitOpaqueReturnParameterizedTypeRepr(OpaqueReturnParameterizedTypeRepr *T) {
3147+
visitNamedOpaqueReturnTypeRepr(NamedOpaqueReturnTypeRepr *T) {
31443148
printCommon("type_opaque_return_parameterized") << '\n';
31453149
printRec(T->getBase());
31463150
PrintWithColorRAII(OS, ParenthesisColor) << ')';

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,8 +1858,8 @@ bool Traversal::visitOpaqueReturnTypeRepr(OpaqueReturnTypeRepr *T) {
18581858
return doIt(T->getConstraint());
18591859
}
18601860

1861-
bool Traversal::visitOpaqueReturnParameterizedTypeRepr(
1862-
OpaqueReturnParameterizedTypeRepr *T) {
1861+
bool Traversal::visitNamedOpaqueReturnTypeRepr(
1862+
NamedOpaqueReturnTypeRepr *T) {
18631863
return doIt(T->getBase());
18641864
}
18651865

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ directReferencesForTypeRepr(Evaluator &evaluator,
22632263
return { };
22642264

22652265
case TypeReprKind::OpaqueReturn:
2266-
case TypeReprKind::OpaqueReturnParameterized:
2266+
case TypeReprKind::NamedOpaqueReturn:
22672267
return { };
22682268

22692269
case TypeReprKind::Fixed:

lib/AST/TypeRepr.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,29 +435,31 @@ void ProtocolTypeRepr::printImpl(ASTPrinter &Printer,
435435
Printer << ".Protocol";
436436
}
437437

438-
SourceLoc OpaqueReturnParameterizedTypeRepr::getStartLocImpl() const {
438+
void OpaqueReturnTypeRepr::printImpl(ASTPrinter &Printer,
439+
const PrintOptions &Opts) const {
440+
Printer.printKeyword("some", Opts, /*Suffix=*/" ");
441+
printTypeRepr(Constraint, Printer, Opts);
442+
}
443+
444+
SourceLoc NamedOpaqueReturnTypeRepr::getStartLocImpl() const {
439445
return GenericParams->getLAngleLoc();
440446
}
441447

442-
SourceLoc OpaqueReturnParameterizedTypeRepr::getEndLocImpl() const {
448+
SourceLoc NamedOpaqueReturnTypeRepr::getEndLocImpl() const {
443449
return Base->getEndLoc();
444450
}
445451

446-
SourceLoc OpaqueReturnParameterizedTypeRepr::getLocImpl() const {
452+
SourceLoc NamedOpaqueReturnTypeRepr::getLocImpl() const {
447453
return Base->getLoc();
448454
}
449455

450-
void OpaqueReturnParameterizedTypeRepr::printImpl(
456+
void NamedOpaqueReturnTypeRepr::printImpl(
451457
ASTPrinter &Printer, const PrintOptions &Opts) const {
458+
GenericParams->print(Printer, Opts);
459+
Printer << ' ';
452460
printTypeRepr(Base, Printer, Opts);
453461
}
454462

455-
void OpaqueReturnTypeRepr::printImpl(ASTPrinter &Printer,
456-
const PrintOptions &Opts) const {
457-
Printer.printKeyword("some", Opts, /*Suffix=*/" ");
458-
printTypeRepr(Constraint, Printer, Opts);
459-
}
460-
461463
void SpecifierTypeRepr::printImpl(ASTPrinter &Printer,
462464
const PrintOptions &Opts) const {
463465
switch (getKind()) {

lib/Parse/ParsePattern.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,11 @@ Parser::parseFunctionSignature(Identifier SimpleName,
869869
// parameters, and correct it.
870870
parseEffectsSpecifiers(arrowLoc, asyncLoc, &reasync, throwsLoc, &rethrows);
871871

872-
GenericParamList *GenericParams = nullptr;
872+
GenericParamList *genericParams = nullptr;
873873
if (Context.LangOpts.EnableExperimentalOpaqueReturnTypes) {
874-
auto GenericParamsResult = maybeParseGenericParams();
875-
GenericParams = GenericParamsResult.getPtrOrNull();
876-
Status |= GenericParamsResult;
874+
auto genericParamsResult = maybeParseGenericParams();
875+
genericParams = genericParamsResult.getPtrOrNull();
876+
Status |= genericParamsResult;
877877

878878
// Check for effect specifiers after the generic parameters, but before
879879
// the return type, and correct it.
@@ -888,14 +888,9 @@ Parser::parseFunctionSignature(Identifier SimpleName,
888888
if (Status.isErrorOrHasCompletion())
889889
return Status;
890890

891-
if (GenericParams != nullptr) {
892-
// The `Base` for our `OpaqueReturnParameterizedTypeRepr` should not be
893-
// `nullptr`
894-
assert(
895-
retType != nullptr &&
896-
"Expected non-null return type if `parseDeclReturnType` succeeded");
891+
if (genericParams != nullptr) {
897892
retType = new (Context)
898-
OpaqueReturnParameterizedTypeRepr(retType, GenericParams);
893+
NamedOpaqueReturnTypeRepr(retType, genericParams);
899894
}
900895

901896
// Check for effect specifiers after the type and correct it.

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,8 +2084,8 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
20842084
: ErrorType::get(getASTContext());
20852085
}
20862086

2087-
case TypeReprKind::OpaqueReturnParameterized:
2088-
return resolveType(cast<OpaqueReturnParameterizedTypeRepr>(repr)->getBase(),
2087+
case TypeReprKind::NamedOpaqueReturn:
2088+
return resolveType(cast<NamedOpaqueReturnTypeRepr>(repr)->getBase(),
20892089
options);
20902090

20912091
case TypeReprKind::Placeholder: {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func f() -> <T> () {
2+
}
3+
func g() -> <T> Int {
4+
}
5+
6+
// RUN: %target-swift-ide-test -print-ast-typechecked -enable-experimental-opaque-return-types -source-filename %s | %FileCheck %s -check-prefix=CHECK1
7+
// CHECK1: {{^}}func f() {{{$}}
8+
// CHECK1: {{^}}}{{$}}
9+
// CHECK1: {{^}}func g() -> <T> Int {{{$}}
10+
// CHECK1: {{^}}}{{$}}

test/type/opaque_experimental.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
// RUN: %target-swift-frontend -enable-experimental-opaque-return-types -disable-availability-checking -typecheck -verify %s
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-opaque-return-types -disable-availability-checking
22

33
// Tests for experimental extensions to opaque return type support.
44

55
func f0() -> <T> () { }
6+
func f1() -> <T, U, V> () { }
7+
func f2() -> <T: Collection, U: SignedInteger> () { }
8+
func f4() async -> <T> () { }
9+
10+
func g0() -> <T> { } // expected-error{{expected type for function result}}
11+
func g1() -> async <T> () { } // expected-error{{'async' may only occur before '->'}}
12+
func g2() -> <T> async () { } // expected-error{{'async' may only occur before '->'}}
13+
func g3() -> <T> () async { } // expected-error{{'async' may only occur before '->'}}

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,13 @@ DisableImplicitConcurrencyImport("disable-implicit-concurrency-module-import",
763763
llvm::cl::desc("Disable implicit import of _Concurrency module"),
764764
llvm::cl::init(false));
765765

766+
static llvm::cl::opt<bool>
767+
EnableExperimentalOpaqueReturnTypes("enable-experimental-opaque-return-types",
768+
llvm::cl::desc("Enable experimental extensions to opaque return type support"),
769+
llvm::cl::Hidden,
770+
llvm::cl::cat(Category),
771+
llvm::cl::init(false));
772+
766773
static llvm::cl::opt<bool>
767774
EnableExperimentalDistributed("enable-experimental-distributed",
768775
llvm::cl::desc("Enable experimental distributed actors and functions"),
@@ -3862,6 +3869,9 @@ int main(int argc, char *argv[]) {
38623869
if (options::DisableImplicitConcurrencyImport) {
38633870
InitInvok.getLangOptions().DisableImplicitConcurrencyModuleImport = true;
38643871
}
3872+
if (options::EnableExperimentalOpaqueReturnTypes) {
3873+
InitInvok.getLangOptions().EnableExperimentalOpaqueReturnTypes = true;
3874+
}
38653875

38663876
if (options::EnableExperimentalDistributed) {
38673877
// distributed implies concurrency features:

0 commit comments

Comments
 (0)