Skip to content

Commit 229275a

Browse files
committed
[AST] Don’t print internal function labels if they start with '$'
We might infer internal function labels as `$0` from a closure with which a variable is initialised. But we don’t want to print the function signature as `(_ $0: Int) -> Int` because `$0` is not a valid variable name to declare. So, in the case described above, only print the type. Fixes rdar://77462547
1 parent b244fa6 commit 229275a

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4878,11 +4878,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
48784878
PrintNameContext::FunctionParameterExternal);
48794879
Printer << ": ";
48804880
} else if (Options.AlwaysTryPrintParameterLabels &&
4881-
Param.hasInternalLabel()) {
4881+
Param.hasInternalLabel() &&
4882+
!Param.getInternalLabel().hasDollarPrefix()) {
48824883
// We didn't have an external parameter label but were requested to
4883-
// always try and print parameter labels. Print The internal label.
4884-
// If we have neither an external nor an internal label, only print the
4885-
// type.
4884+
// always try and print parameter labels.
4885+
// If the internal label is a valid internal parameter label (does not
4886+
// start with '$'), print the internal label. If we have neither an
4887+
// external nor a printable internal label, only print the type.
48864888
Printer << "_ ";
48874889
Printer.printName(Param.getInternalLabel(),
48884890
PrintNameContext::FunctionParameterLocal);

test/ModuleInterface/closure.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -typecheck %s -emit-module-interface-path %t/main.swiftinterface -enable-library-evolution
4+
// RUN: %FileCheck %s < %t/main.swiftinterface
5+
6+
// CHECK: import Swift
7+
8+
// CHECK: public let MyClosureVar: (Swift.Int) -> Swift.Int
9+
public let MyClosureVar: (Int) -> Int = { $0 }
10+
11+
// CHECK: public var MyOtherClosureVar: (_ x: Swift.Int) -> Swift.Int
12+
public let MyOtherClosureVar: (_ x: Int) -> Int

0 commit comments

Comments
 (0)