Skip to content

Commit 38b7fa5

Browse files
committed
Assign fallback discriminators within top-level closures.
Fixes rdar://142425569.
1 parent 2f7b5e4 commit 38b7fa5

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/AST/Expr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,11 +1952,13 @@ unsigned AbstractClosureExpr::getDiscriminator() const {
19521952
// If we don't have a discriminator, and either
19531953
// 1. We have ill-formed code and we're able to assign a discriminator, or
19541954
// 2. We are in a macro expansion buffer
1955+
// 3. We are within top-level code where there's nothing to anchor to
19551956
//
19561957
// then assign the next discriminator now.
19571958
if (getRawDiscriminator() == InvalidDiscriminator &&
19581959
(ctx.Diags.hadAnyError() ||
1959-
getParentSourceFile()->getFulfilledMacroRole() != std::nullopt)) {
1960+
getParentSourceFile()->getFulfilledMacroRole() != std::nullopt ||
1961+
getParent()->isModuleScopeContext())) {
19601962
auto discriminator = ctx.getNextDiscriminator(getParent());
19611963
ctx.setMaxAssignedDiscriminator(getParent(), discriminator + 1);
19621964
const_cast<AbstractClosureExpr *>(this)->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// REQUIRES: swift_swift_parser, executable_test
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift
5+
6+
// RUN: %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -parse-as-library -emit-reference-dependencies-path %t/MacroUser.swiftdeps -primary-file %s
7+
8+
// Note: this test ensures that we don't crash when trying to mangle a symbol
9+
// within a closure passed to a macro.
10+
11+
@freestanding(declaration)
12+
macro Empty<T>(_ x: T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
13+
14+
#Empty {
15+
struct S {
16+
static var foo: Int { 0 }
17+
}
18+
_ = S.foo
19+
}

0 commit comments

Comments
 (0)