Skip to content

Commit 24bf623

Browse files
authored
Merge pull request #78565 from DougGregor/top-level-closure-macro-discriminators
Assign fallback discriminators within top-level closures.
2 parents 18dd091 + 4ed008c commit 24bf623

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
@@ -1934,11 +1934,13 @@ unsigned AbstractClosureExpr::getDiscriminator() const {
19341934
// If we don't have a discriminator, and either
19351935
// 1. We have ill-formed code and we're able to assign a discriminator, or
19361936
// 2. We are in a macro expansion buffer
1937+
// 3. We are within top-level code where there's nothing to anchor to
19371938
//
19381939
// then assign the next discriminator now.
19391940
if (getRawDiscriminator() == InvalidDiscriminator &&
19401941
(ctx.Diags.hadAnyError() ||
1941-
getParentSourceFile()->getFulfilledMacroRole() != std::nullopt)) {
1942+
getParentSourceFile()->getFulfilledMacroRole() != std::nullopt ||
1943+
getParent()->isModuleScopeContext())) {
19421944
auto discriminator = ctx.getNextDiscriminator(getParent());
19431945
ctx.setMaxAssignedDiscriminator(getParent(), discriminator + 1);
19441946
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)