Skip to content

[Parser] Allow closures with custom attributes and capture lists. #37986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1217,15 +1217,25 @@ class Parser {

//===--------------------------------------------------------------------===//
// Type Parsing


enum class ParseTypeReason {
/// Any type parsing context.
Unspecified,

/// Whether the type is for a closure attribute.
CustomAttribute,
};

ParserResult<TypeRepr> parseType();
ParserResult<TypeRepr> parseType(Diag<> MessageID,
bool IsSILFuncDecl = false);
ParserResult<TypeRepr> parseType(
Diag<> MessageID,
ParseTypeReason reason = ParseTypeReason::Unspecified);

ParserResult<TypeRepr>
parseTypeSimpleOrComposition(Diag<> MessageID);
parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason);

ParserResult<TypeRepr> parseTypeSimple(Diag<> MessageID);
ParserResult<TypeRepr> parseTypeSimple(
Diag<> MessageID, ParseTypeReason reason);

/// Parse layout constraint.
LayoutConstraint parseLayoutConstraint(Identifier LayoutConstraintID);
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2821,7 +2821,7 @@ ParserResult<CustomAttr> Parser::parseCustomAttribute(
SyntaxContext->setCreateSyntax(SyntaxKind::CustomAttribute);

// Parse a custom attribute.
auto type = parseType(diag::expected_type);
auto type = parseType(diag::expected_type, ParseTypeReason::CustomAttribute);
if (type.hasCodeCompletion() || type.isNull()) {
if (Tok.is(tok::l_paren) && isCustomAttributeArgument())
skipSingle();
Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
}

if (isBareType && paramContext == ParameterContextKind::EnumElement) {
auto type = parseType(diag::expected_parameter_type, false);
auto type = parseType(diag::expected_parameter_type);
status |= type;
param.Type = type.getPtrOrNull();
param.FirstName = Identifier();
Expand All @@ -389,7 +389,7 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
// the user is about to type the parameter label and we shouldn't
// suggest types.
SourceLoc typeStartLoc = Tok.getLoc();
auto type = parseType(diag::expected_parameter_type, false);
auto type = parseType(diag::expected_parameter_type);
status |= type;
param.Type = type.getPtrOrNull();

Expand Down
17 changes: 9 additions & 8 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ LayoutConstraint Parser::parseLayoutConstraint(Identifier LayoutConstraintID) {
/// type-simple '!'
/// type-collection
/// type-array
ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID) {
ParserResult<TypeRepr> Parser::parseTypeSimple(
Diag<> MessageID, ParseTypeReason reason) {
ParserResult<TypeRepr> ty;

if (Tok.is(tok::kw_inout) ||
Expand Down Expand Up @@ -242,7 +243,7 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID) {
continue;
}
// Parse legacy array types for migration.
if (Tok.is(tok::l_square)) {
if (Tok.is(tok::l_square) && reason != ParseTypeReason::CustomAttribute) {
ty = parseTypeArray(ty);
continue;
}
Expand Down Expand Up @@ -329,8 +330,8 @@ ParserResult<TypeRepr> Parser::parseSILBoxType(GenericParamList *generics,
/// type-function:
/// type-composition 'async'? 'throws'? '->' type
///
ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
bool IsSILFuncDecl) {
ParserResult<TypeRepr> Parser::parseType(
Diag<> MessageID, ParseTypeReason reason) {
// Start a context for creating type syntax.
SyntaxParsingContext TypeParsingContext(SyntaxContext,
SyntaxContextKind::Type);
Expand Down Expand Up @@ -369,7 +370,7 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
return parseSILBoxType(generics, attrs);
}

ParserResult<TypeRepr> ty = parseTypeSimpleOrComposition(MessageID);
ParserResult<TypeRepr> ty = parseTypeSimpleOrComposition(MessageID, reason);
status |= ParserStatus(ty);
if (ty.isNull())
return status;
Expand Down Expand Up @@ -762,7 +763,7 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
/// 'some'? type-simple
/// type-composition '&' type-simple
ParserResult<TypeRepr>
Parser::parseTypeSimpleOrComposition(Diag<> MessageID) {
Parser::parseTypeSimpleOrComposition(Diag<> MessageID, ParseTypeReason reason) {
SyntaxParsingContext SomeTypeContext(SyntaxContext, SyntaxKind::SomeType);
// Check for the opaque modifier.
// This is only semantically allowed in certain contexts, but we parse it
Expand All @@ -786,7 +787,7 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID) {

SyntaxParsingContext CompositionContext(SyntaxContext, SyntaxContextKind::Type);
// Parse the first type
ParserResult<TypeRepr> FirstType = parseTypeSimple(MessageID);
ParserResult<TypeRepr> FirstType = parseTypeSimple(MessageID, reason);
if (FirstType.isNull())
return FirstType;
if (!Tok.isContextualPunctuator("&")) {
Expand Down Expand Up @@ -844,7 +845,7 @@ Parser::parseTypeSimpleOrComposition(Diag<> MessageID) {

// Parse next type.
ParserResult<TypeRepr> ty =
parseTypeSimple(diag::expected_identifier_for_type);
parseTypeSimple(diag::expected_identifier_for_type, reason);
if (ty.hasCodeCompletion())
return makeParserCodeCompletionResult<TypeRepr>();
Status |= ty;
Expand Down
3 changes: 1 addition & 2 deletions lib/SIL/Parser/ParseSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,7 @@ bool SILParser::parseSILType(SILType &Result,
TypeAttributes::Convention::makeSwiftConvention("thin");
}

ParserResult<TypeRepr> TyR = P.parseType(diag::expected_sil_type,
/*isSILFuncDecl*/ IsFuncDecl);
ParserResult<TypeRepr> TyR = P.parseType(diag::expected_sil_type);

if (TyR.isNull())
return true;
Expand Down
6 changes: 5 additions & 1 deletion test/Concurrency/global_actor_function_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func someSlowOperation() async -> Int { 5 }

func acceptOnSomeGlobalActor<T>(_: @SomeGlobalActor () -> T) { }

func testClosures() async {
func testClosures(i: Int) async {
// Global actors on synchronous closures become part of the type
let cl1 = { @SomeGlobalActor in
onSomeGlobalActor()
Expand All @@ -46,6 +46,10 @@ func testClosures() async {
}
let _: Double = cl2 // expected-error{{cannot convert value of type '() async -> Int' to specified type 'Double'}}

let cl3 = { @SomeGlobalActor [i] in
print(i + onSomeGlobalActor())
}

// okay to be explicit
acceptOnSomeGlobalActor { @SomeGlobalActor in
onSomeGlobalActor()
Expand Down