Skip to content

Commit deb0457

Browse files
author
Benjamin Driscoll
committed
[Parse] Allow named opaque types in more places
- Allow named opaque types in typed patterns and subscripts - Fix inheritance clause printing for `GenericParamList` - clang-format changes from previous commit on this branch
1 parent dddf0ec commit deb0457

File tree

12 files changed

+164
-153
lines changed

12 files changed

+164
-153
lines changed

include/swift/AST/GenericParamList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ class RequirementRepr {
8181
: SeparatorLoc(SeparatorLoc), Kind(Kind), Invalid(false),
8282
FirstType(FirstType), SecondLayout(SecondLayout) { }
8383

84-
void printImpl(ASTPrinter &OS) const;
85-
8684
public:
8785
/// Construct a new type-constraint requirement.
8886
///

include/swift/AST/TypeRepr.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,8 +1114,7 @@ class NamedOpaqueReturnTypeRepr : public TypeRepr {
11141114
GenericParamList *GenericParams;
11151115

11161116
public:
1117-
NamedOpaqueReturnTypeRepr(TypeRepr *Base,
1118-
GenericParamList *GenericParams)
1117+
NamedOpaqueReturnTypeRepr(TypeRepr *Base, GenericParamList *GenericParams)
11191118
: TypeRepr(TypeReprKind::NamedOpaqueReturn), Base(Base),
11201119
GenericParams(GenericParams) {
11211120
assert(Base && GenericParams);
@@ -1127,9 +1126,7 @@ class NamedOpaqueReturnTypeRepr : public TypeRepr {
11271126
static bool classof(const TypeRepr *T) {
11281127
return T->getKind() == TypeReprKind::NamedOpaqueReturn;
11291128
}
1130-
static bool classof(const NamedOpaqueReturnTypeRepr *T) {
1131-
return true;
1132-
}
1129+
static bool classof(const NamedOpaqueReturnTypeRepr *T) { return true; }
11331130

11341131
private:
11351132
SourceLoc getStartLocImpl() const;

include/swift/Parse/Parser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,13 @@ class Parser {
12231223
ParserResult<TypeRepr> parseType(Diag<> MessageID,
12241224
bool IsSILFuncDecl = false);
12251225

1226+
/// Parse a type optionally prefixed by a list of named opaque parameters. If
1227+
/// no params present, return 'type'. Otherwise, return 'type-named-opaque'.
1228+
///
1229+
/// type-named-opaque:
1230+
/// generic-params type
1231+
ParserResult<TypeRepr> parseTypeWithOpaqueParams(Diag<> MessageID);
1232+
12261233
ParserResult<TypeRepr>
12271234
parseTypeSimpleOrComposition(Diag<> MessageID);
12281235

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "swift/AST/ForeignAsyncConvention.h"
2222
#include "swift/AST/ForeignErrorConvention.h"
2323
#include "swift/AST/GenericEnvironment.h"
24-
#include "swift/AST/GenericParamList.h"
2524
#include "swift/AST/Initializer.h"
2625
#include "swift/AST/ParameterList.h"
2726
#include "swift/AST/ProtocolConformance.h"
@@ -114,111 +113,6 @@ namespace {
114113
};
115114
} // end anonymous namespace
116115

117-
//===----------------------------------------------------------------------===//
118-
// Generic param list printing.
119-
//===----------------------------------------------------------------------===//
120-
121-
void RequirementRepr::dump() const {
122-
print(llvm::errs());
123-
llvm::errs() << "\n";
124-
}
125-
126-
void RequirementRepr::printImpl(ASTPrinter &out) const {
127-
auto printLayoutConstraint =
128-
[&](const LayoutConstraintLoc &LayoutConstraintLoc) {
129-
LayoutConstraintLoc.getLayoutConstraint()->print(out, PrintOptions());
130-
};
131-
132-
switch (getKind()) {
133-
case RequirementReprKind::LayoutConstraint:
134-
if (auto *repr = getSubjectRepr()) {
135-
repr->print(out, PrintOptions());
136-
}
137-
out << " : ";
138-
printLayoutConstraint(getLayoutConstraintLoc());
139-
break;
140-
141-
case RequirementReprKind::TypeConstraint:
142-
if (auto *repr = getSubjectRepr()) {
143-
repr->print(out, PrintOptions());
144-
}
145-
out << " : ";
146-
if (auto *repr = getConstraintRepr()) {
147-
repr->print(out, PrintOptions());
148-
}
149-
break;
150-
151-
case RequirementReprKind::SameType:
152-
if (auto *repr = getFirstTypeRepr()) {
153-
repr->print(out, PrintOptions());
154-
}
155-
out << " == ";
156-
if (auto *repr = getSecondTypeRepr()) {
157-
repr->print(out, PrintOptions());
158-
}
159-
break;
160-
}
161-
}
162-
163-
void RequirementRepr::print(raw_ostream &out) const {
164-
StreamPrinter printer(out);
165-
printImpl(printer);
166-
}
167-
void RequirementRepr::print(ASTPrinter &out) const {
168-
printImpl(out);
169-
}
170-
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-
181-
static void printTrailingRequirements(ASTPrinter &Printer,
182-
ArrayRef<RequirementRepr> Reqs,
183-
bool printWhereKeyword) {
184-
if (Reqs.empty()) return;
185-
186-
if (printWhereKeyword)
187-
Printer << " where ";
188-
interleave(
189-
Reqs,
190-
[&](const RequirementRepr &req) {
191-
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
192-
req.print(Printer);
193-
Printer.printStructurePost(PrintStructureKind::GenericRequirement);
194-
},
195-
[&] { Printer << ", "; });
196-
}
197-
198-
void GenericParamList::print(ASTPrinter &Printer, const PrintOptions &PO) const {
199-
Printer << '<';
200-
interleave(*this,
201-
[&](const GenericTypeParamDecl *P) {
202-
Printer << P->getName();
203-
if (!P->getInherited().empty()) {
204-
Printer << " : ";
205-
P->getInherited()[0].getType().print(Printer, PO);
206-
}
207-
},
208-
[&] { Printer << ", "; });
209-
210-
printTrailingRequirements(Printer, getRequirements(),
211-
/*printWhereKeyword*/true);
212-
Printer << '>';
213-
}
214-
215-
void TrailingWhereClause::print(llvm::raw_ostream &OS,
216-
bool printWhereKeyword) const {
217-
StreamPrinter Printer(OS);
218-
printTrailingRequirements(Printer, getRequirements(),
219-
printWhereKeyword);
220-
}
221-
222116
static void printGenericParameters(raw_ostream &OS, GenericParamList *Params) {
223117
if (!Params)
224118
return;
@@ -3143,9 +3037,8 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr> {
31433037
PrintWithColorRAII(OS, ParenthesisColor) << ')';
31443038
}
31453039

3146-
void
3147-
visitNamedOpaqueReturnTypeRepr(NamedOpaqueReturnTypeRepr *T) {
3148-
printCommon("type_opaque_return_parameterized") << '\n';
3040+
void visitNamedOpaqueReturnTypeRepr(NamedOpaqueReturnTypeRepr *T) {
3041+
printCommon("type_named_opaque_return") << '\n';
31493042
printRec(T->getBase());
31503043
PrintWithColorRAII(OS, ParenthesisColor) << ')';
31513044
}

lib/AST/ASTPrinter.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/ExistentialLayout.h"
2727
#include "swift/AST/Expr.h"
2828
#include "swift/AST/FileUnit.h"
29+
#include "swift/AST/GenericParamList.h"
2930
#include "swift/AST/GenericSignature.h"
3031
#include "swift/AST/Module.h"
3132
#include "swift/AST/NameLookup.h"
@@ -5851,3 +5852,113 @@ swift::getInheritedForPrinting(const Decl *decl, const PrintOptions &options,
58515852
}
58525853
}
58535854
}
5855+
5856+
//===----------------------------------------------------------------------===//
5857+
// Generic param list printing.
5858+
//===----------------------------------------------------------------------===//
5859+
5860+
void RequirementRepr::dump() const {
5861+
print(llvm::errs());
5862+
llvm::errs() << "\n";
5863+
}
5864+
5865+
void RequirementRepr::print(raw_ostream &out) const {
5866+
StreamPrinter printer(out);
5867+
print(printer);
5868+
}
5869+
5870+
void RequirementRepr::print(ASTPrinter &out) const {
5871+
auto printLayoutConstraint =
5872+
[&](const LayoutConstraintLoc &LayoutConstraintLoc) {
5873+
LayoutConstraintLoc.getLayoutConstraint()->print(out, PrintOptions());
5874+
};
5875+
5876+
switch (getKind()) {
5877+
case RequirementReprKind::LayoutConstraint:
5878+
if (auto *repr = getSubjectRepr()) {
5879+
repr->print(out, PrintOptions());
5880+
}
5881+
out << " : ";
5882+
printLayoutConstraint(getLayoutConstraintLoc());
5883+
break;
5884+
5885+
case RequirementReprKind::TypeConstraint:
5886+
if (auto *repr = getSubjectRepr()) {
5887+
repr->print(out, PrintOptions());
5888+
}
5889+
out << " : ";
5890+
if (auto *repr = getConstraintRepr()) {
5891+
repr->print(out, PrintOptions());
5892+
}
5893+
break;
5894+
5895+
case RequirementReprKind::SameType:
5896+
if (auto *repr = getFirstTypeRepr()) {
5897+
repr->print(out, PrintOptions());
5898+
}
5899+
out << " == ";
5900+
if (auto *repr = getSecondTypeRepr()) {
5901+
repr->print(out, PrintOptions());
5902+
}
5903+
break;
5904+
}
5905+
}
5906+
5907+
void GenericParamList::dump() const {
5908+
print(llvm::errs());
5909+
llvm::errs() << '\n';
5910+
}
5911+
5912+
void GenericParamList::print(raw_ostream &out, const PrintOptions &PO) const {
5913+
StreamPrinter printer(out);
5914+
print(printer, PO);
5915+
}
5916+
5917+
static void printTrailingRequirements(ASTPrinter &Printer,
5918+
ArrayRef<RequirementRepr> Reqs,
5919+
bool printWhereKeyword) {
5920+
if (Reqs.empty())
5921+
return;
5922+
5923+
if (printWhereKeyword)
5924+
Printer << " where ";
5925+
interleave(
5926+
Reqs,
5927+
[&](const RequirementRepr &req) {
5928+
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
5929+
req.print(Printer);
5930+
Printer.printStructurePost(PrintStructureKind::GenericRequirement);
5931+
},
5932+
[&] { Printer << ", "; });
5933+
}
5934+
5935+
void GenericParamList::print(ASTPrinter &Printer,
5936+
const PrintOptions &PO) const {
5937+
Printer << '<';
5938+
interleave(
5939+
*this,
5940+
[&](const GenericTypeParamDecl *P) {
5941+
Printer << P->getName();
5942+
if (!P->getInherited().empty()) {
5943+
Printer << " : ";
5944+
5945+
auto loc = P->getInherited()[0];
5946+
if (willUseTypeReprPrinting(loc, nullptr, PO)) {
5947+
loc.getTypeRepr()->print(Printer, PO);
5948+
} else {
5949+
loc.getType()->print(Printer, PO);
5950+
}
5951+
}
5952+
},
5953+
[&] { Printer << ", "; });
5954+
5955+
printTrailingRequirements(Printer, getRequirements(),
5956+
/*printWhereKeyword*/ true);
5957+
Printer << '>';
5958+
}
5959+
5960+
void TrailingWhereClause::print(llvm::raw_ostream &OS,
5961+
bool printWhereKeyword) const {
5962+
StreamPrinter Printer(OS);
5963+
printTrailingRequirements(Printer, getRequirements(), printWhereKeyword);
5964+
}

lib/AST/ASTWalker.cpp

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

1861-
bool Traversal::visitNamedOpaqueReturnTypeRepr(
1862-
NamedOpaqueReturnTypeRepr *T) {
1861+
bool Traversal::visitNamedOpaqueReturnTypeRepr(NamedOpaqueReturnTypeRepr *T) {
18631862
return doIt(T->getBase());
18641863
}
18651864

lib/AST/TypeRepr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ SourceLoc NamedOpaqueReturnTypeRepr::getLocImpl() const {
453453
return Base->getLoc();
454454
}
455455

456-
void NamedOpaqueReturnTypeRepr::printImpl(
457-
ASTPrinter &Printer, const PrintOptions &Opts) const {
456+
void NamedOpaqueReturnTypeRepr::printImpl(ASTPrinter &Printer,
457+
const PrintOptions &Opts) const {
458458
GenericParams->print(Printer, Opts);
459459
Printer << ' ';
460460
printTypeRepr(Base, Printer, Opts);

lib/Parse/ParsePattern.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ Parser::parseFunctionArguments(SmallVectorImpl<Identifier> &NamePieces,
824824
/// func-signature:
825825
/// func-arguments ('async'|'reasync')? func-throws? func-signature-result?
826826
/// func-signature-result:
827-
/// '->' generic-params? type
827+
/// '->' type
828828
///
829829
/// Note that this leaves retType as null if unspecified.
830830
ParserStatus
@@ -865,34 +865,17 @@ Parser::parseFunctionSignature(Identifier SimpleName,
865865
arrowLoc = consumeToken(tok::colon);
866866
}
867867

868-
// Check for effect specifiers after the arrow, but before the generic
869-
// parameters, and correct it.
868+
// Check for effect specifiers after the arrow, but before the return type,
869+
// and correct it.
870870
parseEffectsSpecifiers(arrowLoc, asyncLoc, &reasync, throwsLoc, &rethrows);
871871

872-
GenericParamList *genericParams = nullptr;
873-
if (Context.LangOpts.EnableExperimentalOpaqueReturnTypes) {
874-
auto genericParamsResult = maybeParseGenericParams();
875-
genericParams = genericParamsResult.getPtrOrNull();
876-
Status |= genericParamsResult;
877-
878-
// Check for effect specifiers after the generic parameters, but before
879-
// the return type, and correct it.
880-
parseEffectsSpecifiers(arrowLoc, asyncLoc, &reasync, throwsLoc,
881-
&rethrows);
882-
}
883-
884872
ParserResult<TypeRepr> ResultType =
885873
parseDeclResultType(diag::expected_type_function_result);
886874
retType = ResultType.getPtrOrNull();
887875
Status |= ResultType;
888876
if (Status.isErrorOrHasCompletion())
889877
return Status;
890878

891-
if (genericParams != nullptr) {
892-
retType = new (Context)
893-
NamedOpaqueReturnTypeRepr(retType, genericParams);
894-
}
895-
896879
// Check for effect specifiers after the type and correct it.
897880
parseEffectsSpecifiers(arrowLoc, asyncLoc, &reasync, throwsLoc, &rethrows);
898881
} else {

0 commit comments

Comments
 (0)