Skip to content

Commit f372825

Browse files
committed
Progress... I think
1 parent e0e3f05 commit f372825

14 files changed

+40
-34
lines changed

include/swift/AST/Decl.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -5952,6 +5952,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
59525952
/// Returns true if the function has a typed throw.
59535953
bool hasTypedThrows() const { return (Bits.AbstractFunctionDecl.Throws && ThrowsType != nullptr); }
59545954

5955+
TypeRepr *typedThrow() const { return ThrowsType; }
5956+
59555957
// FIXME: Hack that provides names with keyword arguments for accessors.
59565958
DeclName getEffectiveFullName() const;
59575959

@@ -6263,7 +6265,7 @@ class FuncDecl : public AbstractFunctionDecl {
62636265
/// Factory function only for use by deserialization.
62646266
static FuncDecl *createDeserialized(ASTContext &Context,
62656267
StaticSpellingKind StaticSpelling,
6266-
DeclName Name, bool Async, bool Throws,
6268+
DeclName Name, bool Async, bool Throws, TypeRepr *ThrowsType,
62676269
GenericParamList *GenericParams,
62686270
Type FnRetType, DeclContext *Parent);
62696271

@@ -6278,12 +6280,12 @@ class FuncDecl : public AbstractFunctionDecl {
62786280
static FuncDecl *createImplicit(ASTContext &Context,
62796281
StaticSpellingKind StaticSpelling,
62806282
DeclName Name, SourceLoc NameLoc, bool Async,
6281-
bool Throws, GenericParamList *GenericParams,
6283+
bool Throws, TypeRepr *ThrowsType, GenericParamList *GenericParams,
62826284
ParameterList *BodyParams, Type FnRetType,
62836285
DeclContext *Parent);
62846286

62856287
static FuncDecl *createImported(ASTContext &Context, SourceLoc FuncLoc,
6286-
DeclName Name, SourceLoc NameLoc, bool Throws,
6288+
DeclName Name, SourceLoc NameLoc, bool Throws, TypeRepr *ThrowsType,
62876289
ParameterList *BodyParams, Type FnRetType,
62886290
DeclContext *Parent, ClangNode ClangN);
62896291

lib/AST/Builtins.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType) {
170170
DeclName Name(Context, Id, paramList);
171171
auto *const FD = FuncDecl::createImplicit(
172172
Context, StaticSpellingKind::None, Name, /*NameLoc=*/SourceLoc(),
173-
/*Async=*/false, /*Throws=*/false,
173+
/*Async=*/false, /*Throws=*/false, /*ThrowsType=*/nullptr,
174174
/*GenericParams=*/nullptr, paramList, ResType, DC);
175175
FD->setAccess(AccessLevel::Public);
176176
return FD;
@@ -211,7 +211,7 @@ getBuiltinGenericFunction(Identifier Id,
211211
auto *const func = FuncDecl::createImplicit(
212212
Context, StaticSpellingKind::None, Name, /*NameLoc=*/SourceLoc(),
213213
/*Async=*/false,
214-
/*Throws=*/Rethrows, GenericParams, paramList, ResType, DC);
214+
/*Throws=*/Rethrows, /*ThrowsType=*/nullptr, GenericParams, paramList, ResType, DC);
215215

216216
func->setAccess(AccessLevel::Public);
217217
func->setGenericSignature(Sig);

lib/AST/Decl.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -7210,14 +7210,14 @@ FuncDecl *FuncDecl::createImpl(ASTContext &Context,
72107210

72117211
FuncDecl *FuncDecl::createDeserialized(ASTContext &Context,
72127212
StaticSpellingKind StaticSpelling,
7213-
DeclName Name, bool Async, bool Throws,
7213+
DeclName Name, bool Async, bool Throws, TypeRepr *ThrowsType,
72147214
GenericParamList *GenericParams,
72157215
Type FnRetType, DeclContext *Parent) {
72167216
assert(FnRetType && "Deserialized result type must not be null");
72177217
auto *const FD =
72187218
FuncDecl::createImpl(Context, SourceLoc(), StaticSpelling, SourceLoc(),
72197219
Name, SourceLoc(), Async, SourceLoc(), Throws,
7220-
SourceLoc(), nullptr, GenericParams, Parent, ClangNode());
7220+
SourceLoc(), ThrowsType, GenericParams, Parent, ClangNode());
72217221
FD->setResultInterfaceType(FnRetType);
72227222
return FD;
72237223
}
@@ -7240,13 +7240,13 @@ FuncDecl *FuncDecl::create(ASTContext &Context, SourceLoc StaticLoc,
72407240
FuncDecl *FuncDecl::createImplicit(ASTContext &Context,
72417241
StaticSpellingKind StaticSpelling,
72427242
DeclName Name, SourceLoc NameLoc, bool Async,
7243-
bool Throws, GenericParamList *GenericParams,
7243+
bool Throws, TypeRepr *ThrowsType, GenericParamList *GenericParams,
72447244
ParameterList *BodyParams, Type FnRetType,
72457245
DeclContext *Parent) {
72467246
assert(FnRetType);
72477247
auto *const FD = FuncDecl::createImpl(
72487248
Context, SourceLoc(), StaticSpelling, SourceLoc(), Name, NameLoc, Async,
7249-
SourceLoc(), Throws, SourceLoc(), nullptr, GenericParams, Parent, ClangNode());
7249+
SourceLoc(), Throws, SourceLoc(), ThrowsType, GenericParams, Parent, ClangNode());
72507250
FD->setImplicit();
72517251
FD->setParameters(BodyParams);
72527252
FD->setResultInterfaceType(FnRetType);
@@ -7255,13 +7255,13 @@ FuncDecl *FuncDecl::createImplicit(ASTContext &Context,
72557255

72567256
FuncDecl *FuncDecl::createImported(ASTContext &Context, SourceLoc FuncLoc,
72577257
DeclName Name, SourceLoc NameLoc,
7258-
bool Throws, ParameterList *BodyParams,
7258+
bool Throws, TypeRepr *ThrowsType, ParameterList *BodyParams,
72597259
Type FnRetType, DeclContext *Parent,
72607260
ClangNode ClangN) {
72617261
assert(ClangN && FnRetType);
72627262
auto *const FD = FuncDecl::createImpl(
72637263
Context, SourceLoc(), StaticSpellingKind::None, FuncLoc, Name, NameLoc,
7264-
/*Async=*/false, SourceLoc(), Throws, SourceLoc(), nullptr,
7264+
/*Async=*/false, SourceLoc(), Throws, SourceLoc(), ThrowsType,
72657265
/*GenericParams=*/nullptr, Parent, ClangN);
72667266
FD->setParameters(BodyParams);
72677267
FD->setResultInterfaceType(FnRetType);

lib/ClangImporter/ImportDecl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static FuncDecl *createFuncOrAccessor(ASTContext &ctx, SourceLoc funcLoc,
177177
bodyParams,
178178
resultTy, dc, clangNode);
179179
} else {
180-
return FuncDecl::createImported(ctx, funcLoc, name, nameLoc, throws,
180+
return FuncDecl::createImported(ctx, funcLoc, name, nameLoc, throws, nullptr,
181181
bodyParams, resultTy, dc, clangNode);
182182
}
183183
}

lib/Parse/ParsePattern.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,9 @@ void Parser::parseAsyncThrows(
863863
ParserResult<TypeRepr> result = parseType();
864864
throwsType = result.getPtrOrNull();
865865
}
866+
} else {
867+
keyword = Tok.getText();
868+
throwsLoc = consumeToken();
866869
}
867870

868871
if (existingArrowLoc.isValid()) {

lib/Parse/ParseStmt.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ ParserResult<Stmt> Parser::parseStmtDefer() {
947947
Context, StaticSpellingKind::None, name, /*NameLoc=*/PreviousLoc,
948948
/*Async=*/false,
949949
/*Throws=*/false,
950+
/*ThrowsType=*/nullptr,
950951
/*GenericParams*/ nullptr, params, TupleType::getEmpty(Context),
951952
CurDeclContext);
952953
setLocalDiscriminator(tempDecl);

lib/Parse/ParseType.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ ParserResult<TypeRepr> Parser::parseSILBoxType(GenericParamList *generics,
355355
/// attribute-list type-function
356356
///
357357
/// type-function:
358-
/// type-composition 'async'? 'throws'? '->' type
358+
/// type-composition 'async'? 'throws'? type? '->' type
359359
///
360360
ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
361361
bool HandleCodeCompletion,
@@ -436,18 +436,17 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
436436
.fixItReplace(Tok.getLoc(), "throws");
437437
}
438438
throwsLoc = consumeToken();
439-
// The next token is not a keyword
440-
if (!peekToken().isKeyword()) {
441-
BacktrackingScope backtrackingScope(*this);
442-
if (peekToken().is(tok::kw_throws)) {
443-
ASTContext &Ctx = SF.getASTContext();
444-
DiagnosticSuppression SuppressedDiags(Ctx.Diags);
445-
backtrackingScope.cancelBacktrack();
446-
if (canParseType()) {
447-
ParserResult<TypeRepr> result = parseType();
448-
throwsType = result.getPtrOrNull();
449-
}
450-
}
439+
440+
ASTContext &Ctx = SF.getASTContext();
441+
DiagnosticSuppression SuppressedDiags(Ctx.Diags);
442+
bool hasType = false;
443+
{
444+
BacktrackingScope backtrack(*this);
445+
hasType = canParseType();
446+
}
447+
if (hasType) {
448+
ParserResult<TypeRepr> result = parseType();
449+
throwsType = result.getPtrOrNull();
451450
}
452451

453452
// 'async' must preceed 'throws'; accept this but complain.

lib/Sema/DerivedConformanceAdditiveArithmetic.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static ValueDecl *deriveMathOperator(DerivedConformance &derived,
194194
C, StaticSpellingKind::KeywordStatic, operatorDeclName,
195195
/*NameLoc=*/SourceLoc(),
196196
/*Async=*/false,
197-
/*Throws=*/false,
197+
/*Throws=*/false, /*ThrowsType=*/nullptr,
198198
/*GenericParams=*/nullptr, params, selfInterfaceType, parentDC);
199199
auto bodySynthesizer = [](AbstractFunctionDecl *funcDecl,
200200
void *ctx) -> std::pair<BraceStmt *, bool> {

lib/Sema/DerivedConformanceCodable.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,12 @@ static FuncDecl *deriveEncodable_encode(DerivedConformance &derived) {
624624

625625
ParameterList *params = ParameterList::createWithoutLoc(encoderParam);
626626

627-
// Func name: encode(to: Encoder)
627+
// Func name: encode(to: Encoder) throws
628628
DeclName name(C, C.Id_encode, params);
629629
auto *const encodeDecl = FuncDecl::createImplicit(
630630
C, StaticSpellingKind::None, name, /*NameLoc=*/SourceLoc(),
631631
/*Async=*/false,
632-
/*Throws=*/true, /*GenericParams=*/nullptr, params, returnType,
632+
/*Throws=*/true, /*ThrowsType=*/nullptr, /*GenericParams=*/nullptr, params, returnType,
633633
conformanceDC);
634634
encodeDecl->setSynthesized();
635635
encodeDecl->setBodySynthesizer(deriveBodyEncodable_encode);

lib/Sema/DerivedConformanceComparable.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ deriveComparable_lt(
258258
auto *const comparableDecl = FuncDecl::createImplicit(
259259
C, StaticSpellingKind::KeywordStatic, name, /*NameLoc=*/SourceLoc(),
260260
/*Async=*/false,
261-
/*Throws=*/false,
261+
/*Throws=*/false, /*ThrowsType=*/nullptr,
262262
/*GenericParams=*/nullptr, params, boolTy, parentDC);
263263
comparableDecl->setUserAccessible(false);
264264

lib/Sema/DerivedConformanceDifferentiable.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static ValueDecl *deriveDifferentiable_method(
547547
auto *const funcDecl = FuncDecl::createImplicit(
548548
C, StaticSpellingKind::None, declName, /*NameLoc=*/SourceLoc(),
549549
/*Async=*/false,
550-
/*Throws=*/false,
550+
/*Throws=*/false, /*ThrowsType=*/nullptr,
551551
/*GenericParams=*/nullptr, params, returnType, parentDC);
552552
if (!nominal->getSelfClassDecl())
553553
funcDecl->setSelfAccessKind(SelfAccessKind::Mutating);

lib/Sema/DerivedConformanceEquatableHashable.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ deriveEquatable_eq(
409409
auto *const eqDecl = FuncDecl::createImplicit(
410410
C, StaticSpellingKind::KeywordStatic, name, /*NameLoc=*/SourceLoc(),
411411
/*Async=*/false,
412-
/*Throws=*/false,
412+
/*Throws=*/false, /*ThrowsType=*/nullptr,
413413
/*GenericParams=*/nullptr, params, boolTy, parentDC);
414414
eqDecl->setUserAccessible(false);
415415

@@ -546,7 +546,7 @@ deriveHashable_hashInto(
546546
auto *const hashDecl = FuncDecl::createImplicit(
547547
C, StaticSpellingKind::None, name, /*NameLoc=*/SourceLoc(),
548548
/*Async=*/false,
549-
/*Throws=*/false,
549+
/*Throws=*/false, /*ThrowsType=*/nullptr,
550550
/*GenericParams=*/nullptr, params, returnType, parentDC);
551551
hashDecl->setBodySynthesizer(bodySynthesizer);
552552

lib/Sema/TypeCheckAttr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,7 @@ SynthesizeMainFunctionRequest::evaluate(Evaluator &evaluator,
19181918
ParameterList::createEmpty(context)),
19191919
/*NameLoc=*/SourceLoc(),
19201920
/*Async=*/false,
1921-
/*Throws=*/mainFunction->hasThrows(),
1921+
/*Throws=*/mainFunction->hasThrows(), /*ThrowsType=*/mainFunction->typedThrow(),
19221922
/*GenericParams=*/nullptr, ParameterList::createEmpty(context),
19231923
/*FnRetType=*/TupleType::getEmpty(context), declContext);
19241924
func->setSynthesized(true);

lib/Serialization/Deserialization.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3135,13 +3135,14 @@ class DeclDeserializer {
31353135
return declOrOffset;
31363136

31373137
const auto resultType = MF.getType(resultInterfaceTypeID);
3138+
// const auto throwsType = MF.getType(throwsTypeID); // How to retrieve the TypeRepr
31383139
if (declOrOffset.isComplete())
31393140
return declOrOffset;
31403141

31413142
FuncDecl *fn;
31423143
if (!isAccessor) {
31433144
fn = FuncDecl::createDeserialized(ctx, staticSpelling.getValue(), name,
3144-
async, throws, genericParams,
3145+
async, throws, nullptr, genericParams,
31453146
resultType, DC);
31463147
} else {
31473148
auto *accessor = AccessorDecl::createDeserialized(

0 commit comments

Comments
 (0)