Ensure that we assign discriminators for closures from top-level macros

In top-level code, we were incorrectly pulling closure discriminators
from TopLevelCodeDecls, not from the enclosing source file, which could
lead to the same discriminators being assigned to different closures that
come from macro expansions at the top level. Hilarity ensures, yet I am
not amused.

Adjust the DeclContext appropriately when computing discriminators.

Fixes rdar://123836908.
This commit is contained in:
Doug Gregor
2024-03-01 17:09:09 -08:00
parent 6075de1b62
commit 2e7e33d196
3 changed files with 23 additions and 11 deletions

View File

@@ -2193,12 +2193,20 @@ unsigned ASTContext::getNextMacroDiscriminator(
/// Get the next discriminator within the given declaration context.
unsigned ASTContext::getNextDiscriminator(const DeclContext *dc) {
// Top-level code declarations don't have their own discriminators.
if (auto tlcd = dyn_cast<TopLevelCodeDecl>(dc))
dc = tlcd->getParent();
return getImpl().NextDiscriminator[dc];
}
/// Set the maximum assigned discriminator within the given declaration context.
void ASTContext::setMaxAssignedDiscriminator(
const DeclContext *dc, unsigned discriminator) {
// Top-level code declarations don't have their own discriminators.
if (auto tlcd = dyn_cast<TopLevelCodeDecl>(dc))
dc = tlcd->getParent();
assert(discriminator >= getImpl().NextDiscriminator[dc]);
getImpl().NextDiscriminator[dc] = discriminator;
}

View File

@@ -1747,11 +1747,6 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
<< "autoclosure discriminator=";
}
// If we aren't printing to standard error or the debugger output stream,
// this client expects to see the computed discriminator. Compute it now.
if (&os != &llvm::errs() && &os != &llvm::dbgs())
(void)ACE->getDiscriminator();
PrintWithColorRAII(os, DiscriminatorColor) << ACE->getRawDiscriminator();
break;
}
@@ -2782,12 +2777,6 @@ public:
void printClosure(AbstractClosureExpr *E, char const *name,
StringRef label) {
printCommon(E, name, label);
// If we aren't printing to standard error or the debugger output stream,
// this client expects to see the computed discriminator. Compute it now.
if (hasNonStandardOutput())
(void)E->getDiscriminator();
printField(E->getRawDiscriminator(), "discriminator", DiscriminatorColor);
switch (auto isolation = E->getActorIsolation()) {

View File

@@ -0,0 +1,15 @@
// 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-build-swift -swift-version 5 -g -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -Xfrontend -emit-dependencies-path -Xfrontend %t/main.d -Xfrontend -emit-reference-dependencies-path -Xfrontend %t/main.swiftdeps
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
// CHECK: 3
// CHECK-NEXT: 7
print(#stringify({ 1 + 2 }()))
print(#stringify({ 3 + 4 }()))