Skip to content

Commit 2e7e33d

Browse files
committed
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.
1 parent 6075de1 commit 2e7e33d

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,12 +2193,20 @@ unsigned ASTContext::getNextMacroDiscriminator(
21932193

21942194
/// Get the next discriminator within the given declaration context.
21952195
unsigned ASTContext::getNextDiscriminator(const DeclContext *dc) {
2196+
// Top-level code declarations don't have their own discriminators.
2197+
if (auto tlcd = dyn_cast<TopLevelCodeDecl>(dc))
2198+
dc = tlcd->getParent();
2199+
21962200
return getImpl().NextDiscriminator[dc];
21972201
}
21982202

21992203
/// Set the maximum assigned discriminator within the given declaration context.
22002204
void ASTContext::setMaxAssignedDiscriminator(
22012205
const DeclContext *dc, unsigned discriminator) {
2206+
// Top-level code declarations don't have their own discriminators.
2207+
if (auto tlcd = dyn_cast<TopLevelCodeDecl>(dc))
2208+
dc = tlcd->getParent();
2209+
22022210
assert(discriminator >= getImpl().NextDiscriminator[dc]);
22032211
getImpl().NextDiscriminator[dc] = discriminator;
22042212
}

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,11 +1747,6 @@ void swift::printContext(raw_ostream &os, DeclContext *dc) {
17471747
<< "autoclosure discriminator=";
17481748
}
17491749

1750-
// If we aren't printing to standard error or the debugger output stream,
1751-
// this client expects to see the computed discriminator. Compute it now.
1752-
if (&os != &llvm::errs() && &os != &llvm::dbgs())
1753-
(void)ACE->getDiscriminator();
1754-
17551750
PrintWithColorRAII(os, DiscriminatorColor) << ACE->getRawDiscriminator();
17561751
break;
17571752
}
@@ -2782,12 +2777,6 @@ class PrintExpr : public ExprVisitor<PrintExpr, void, StringRef>,
27822777
void printClosure(AbstractClosureExpr *E, char const *name,
27832778
StringRef label) {
27842779
printCommon(E, name, label);
2785-
2786-
// If we aren't printing to standard error or the debugger output stream,
2787-
// this client expects to see the computed discriminator. Compute it now.
2788-
if (hasNonStandardOutput())
2789-
(void)E->getDiscriminator();
2790-
27912780
printField(E->getRawDiscriminator(), "discriminator", DiscriminatorColor);
27922781

27932782
switch (auto isolation = E->getActorIsolation()) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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-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
7+
// RUN: %target-codesign %t/main
8+
// RUN: %target-run %t/main | %FileCheck %s
9+
10+
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
11+
12+
// CHECK: 3
13+
// CHECK-NEXT: 7
14+
print(#stringify({ 1 + 2 }()))
15+
print(#stringify({ 3 + 4 }()))

0 commit comments

Comments
 (0)