Skip to content
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
1 change: 0 additions & 1 deletion lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2897,7 +2897,6 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
diagnose(Tok, diag::expected_closure_in)
.fixItInsert(Tok.getLoc(), "in ");
}
inLoc = Tok.getLoc();
}
}

Expand Down
14 changes: 7 additions & 7 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,14 +977,14 @@ static CharSourceRange getExpansionInsertionRange(MacroRole role,
if (auto *expr = target.dyn_cast<Expr *>()) {
ASSERT(isa<ClosureExpr>(expr));

auto *closure = cast<ClosureExpr>(expr);
// A closure body macro expansion replaces the full source
// range of the closure body starting from `in` and ending right
// before the closing brace.
return Lexer::getCharSourceRangeFromSourceRange(
sourceMgr, SourceRange(Lexer::getLocForEndOfToken(
sourceMgr, closure->getInLoc()),
closure->getEndLoc()));
// range of the closure body's contents.
auto *closure = cast<ClosureExpr>(expr);
if (auto range = closure->getBody()->getContentRange())
return Lexer::getCharSourceRangeFromSourceRange(sourceMgr, range);

// If we have an empty body, just use the end loc.
return CharSourceRange(closure->getEndLoc(), 0);
}

// If the function has a body, that's what's being replaced.
Expand Down
18 changes: 18 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,24 @@ struct ThrowCancellationMacro: BodyMacro {
}
}

struct EmptyBodyMacro: BodyMacro {
static func expansion(
of node: AttributeSyntax,
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
[]
}

static func expansion(
of node: AttributeSyntax,
providingBodyFor closure: ClosureExprSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
[]
}
}

@_spi(ExperimentalLanguageFeature)
public struct TracedPreambleMacro: PreambleMacro {
public static func expansion(
Expand Down
17 changes: 17 additions & 0 deletions test/Macros/macro_expand_body_closure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// REQUIRES: swift_swift_parser
// REQUIRES: swift_feature_ClosureBodyMacro

// RUN: %empty-directory(%t)
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath -swift-version 5

// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -enable-experimental-feature ClosureBodyMacro

@attached(body)
macro Empty() = #externalMacro(module: "MacroDefinition", type: "EmptyBodyMacro")

// Make sure we can handle the lack of 'in' here.
_ = { @Empty x -> // expected-error {{cannot infer type of closure parameter 'x' without a type annotation}}
0 // expected-error {{expected closure result type after '->'}} expected-error {{expected 'in' after the closure signature}}
}

_ = { @Empty in }
2 changes: 2 additions & 0 deletions validation-test/IDE/crashers_fixed/pr-84278.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// RUN: %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
{ k -> #^^#
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// {"kind":"typecheck","signature":"swift::ast_scope::ASTScopeImpl::checkSourceRangeBeforeAddingChild(swift::ast_scope::ASTScopeImpl*, swift::ASTContext const&) const","signatureAssert":"Assertion failed: ((SM.isBefore(range.Start, range.End) || range.Start == range.End) && \"scope source range ends before start\"), function getCharSourceRangeOfScope"}
// RUN: not --crash %target-swift-frontend -typecheck %s
// RUN: not %target-swift-frontend -typecheck %s
{
@in