Skip to content

Commit 18fb76f

Browse files
committed
Simplify parsing and fix astgen failures
1 parent fba3130 commit 18fb76f

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8386,7 +8386,28 @@ void GenericParamList::print(ASTPrinter &Printer,
83868386
interleave(
83878387
*this,
83888388
[&](const GenericTypeParamDecl *P) {
8389-
P->print(Printer, PO);
8389+
Printer << P->getName();
8390+
if (!P->getInherited().empty()) {
8391+
Printer << " : ";
8392+
8393+
auto loc = P->getInherited().getEntry(0);
8394+
if (willUseTypeReprPrinting(loc, nullptr, PO)) {
8395+
loc.getTypeRepr()->print(Printer, PO);
8396+
} else {
8397+
loc.getType()->print(Printer, PO);
8398+
}
8399+
8400+
if (P->hasDefaultType()) {
8401+
Printer << " = ";
8402+
8403+
auto loc = P->getDefaultTypeRepr();
8404+
if (loc && willUseTypeReprPrinting(loc, nullptr, PO)) {
8405+
loc->print(Printer, PO);
8406+
} else {
8407+
P->getDefaultType()->print(Printer, PO);
8408+
}
8409+
}
8410+
}
83908411
},
83918412
[&] { Printer << ", "; });
83928413

lib/Parse/ParseGeneric.cpp

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -130,37 +130,6 @@ Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
130130
Inherited.push_back({Ty.get()});
131131
}
132132

133-
// Parse the '=' followed by a type.
134-
TypeLoc DefaultType;
135-
136-
if (Context.LangOpts.hasFeature(Feature::DefaultGenerics)) {
137-
// TODO: Don't allow defaults for values or parameter packs at the moment.
138-
if (Tok.is(tok::equal) && !EachLoc.isValid() && !LetLoc.isValid()) {
139-
(void)consumeToken();
140-
ParserResult<TypeRepr> Ty;
141-
142-
if (Tok.isAny(tok::identifier, tok::code_complete, tok::kw_protocol,
143-
tok::kw_Any) || Tok.isTilde()) {
144-
Ty = parseType();
145-
} else if (Tok.is(tok::kw_class)) {
146-
diagnose(Tok, diag::unexpected_class_constraint);
147-
diagnose(Tok, diag::suggest_anyobject)
148-
.fixItReplace(Tok.getLoc(), "AnyObject");
149-
consumeToken();
150-
Result.setIsParseError();
151-
} else {
152-
diagnose(Tok, diag::expected_generics_type_restriction, Name);
153-
Result.setIsParseError();
154-
}
155-
156-
if (Ty.hasCodeCompletion())
157-
return makeParserCodeCompletionStatus();
158-
159-
if (Ty.isNonNull())
160-
DefaultType = Ty.get();
161-
}
162-
}
163-
164133
auto ParamKind = GenericTypeParamKind::Type;
165134
SourceLoc SpecifierLoc;
166135

@@ -172,8 +141,22 @@ Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
172141
SpecifierLoc = LetLoc;
173142
}
174143

144+
// Parse the '=' followed by a type.
145+
ParserResult<TypeRepr> DefaultType;
146+
147+
if (Context.LangOpts.hasFeature(Feature::DefaultGenerics)) {
148+
// TODO: Don't allow defaults for values or parameter packs at the moment.
149+
if (Tok.is(tok::equal) && ParamKind == GenericTypeParamKind::Type) {
150+
consumeToken(tok::equal);
151+
DefaultType = parseType(diag::expected_type);
152+
Result |= DefaultType;
153+
if (DefaultType.isNull())
154+
return Result;
155+
}
156+
}
157+
175158
auto *Param = GenericTypeParamDecl::createParsed(
176-
CurDeclContext, Name, NameLoc, SpecifierLoc, DefaultType,
159+
CurDeclContext, Name, NameLoc, SpecifierLoc, DefaultType.getPtrOrNull(),
177160
/*index*/ GenericParams.size(), ParamKind);
178161
if (!Inherited.empty())
179162
Param->setInherited(Context.AllocateCopy(Inherited));

test/ASTGen/decls.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// RUN: %diff -u %t/astgen.ast %t/cpp-parser.ast
1515

16-
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -Xfrontend -enable-experimental-concurrency -enable-experimental-feature CoroutineAccessors -enable-experimental-feature DefaultIsolationPerFile -enable-experimental-feature ParserASTGen)
16+
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -Xfrontend -enable-experimental-concurrency -enable-experimental-feature CoroutineAccessors -enable-experimental-feature DefaultIsolationPerFile -enable-experimental-feature DefaultGenerics -enable-experimental-feature ParserASTGen)
1717

1818
// REQUIRES: executable_test
1919
// REQUIRES: swift_swift_parser
@@ -370,4 +370,3 @@ struct Vec<Element, A = SystemAllocator> {}
370370
enum DefaultGenericEnum<T = Int> {}
371371
class DefaultGenericClass<T, U: Proto1, V: Proto2 = SystemAllocator> {}
372372
typealias DefaultGenericTypealias<T = Int> = Vec<T>
373-
func defaultGenericFunc<T = String>() {}

0 commit comments

Comments
 (0)