Assign fallback discriminators within top-level closures.

Fixes rdar://142425569.
This commit is contained in:
Doug Gregor
2025-01-10 14:59:42 -08:00
parent 471d179bb5
commit 4ed008cf06
2 changed files with 22 additions and 1 deletions

View File

@@ -1934,11 +1934,13 @@ unsigned AbstractClosureExpr::getDiscriminator() const {
// If we don't have a discriminator, and either
// 1. We have ill-formed code and we're able to assign a discriminator, or
// 2. We are in a macro expansion buffer
// 3. We are within top-level code where there's nothing to anchor to
//
// then assign the next discriminator now.
if (getRawDiscriminator() == InvalidDiscriminator &&
(ctx.Diags.hadAnyError() ||
getParentSourceFile()->getFulfilledMacroRole() != std::nullopt)) {
getParentSourceFile()->getFulfilledMacroRole() != std::nullopt ||
getParent()->isModuleScopeContext())) {
auto discriminator = ctx.getNextDiscriminator(getParent());
ctx.setMaxAssignedDiscriminator(getParent(), discriminator + 1);
const_cast<AbstractClosureExpr *>(this)->

View File

@@ -0,0 +1,19 @@
// REQUIRES: swift_swift_parser, executable_test
// 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
// 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
// Note: this test ensures that we don't crash when trying to mangle a symbol
// within a closure passed to a macro.
@freestanding(declaration)
macro Empty<T>(_ x: T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
#Empty {
struct S {
static var foo: Int { 0 }
}
_ = S.foo
}