ASTPrinter: Fix printing of let properties inside @objc @implementation extensions

A `let` here is really just a `var` with a `{ get }`.

Fixes rdar://problem/127120469.
This commit is contained in:
Slava Pestov
2025-06-05 22:52:46 -04:00
parent c61a4fe333
commit 0b3fd9eede
3 changed files with 15 additions and 6 deletions

View File

@@ -3508,12 +3508,14 @@ void PrintAST::visitPatternBindingDecl(PatternBindingDecl *decl) {
if (decl->isStatic())
printStaticKeyword(decl->getCorrectStaticSpelling());
bool printAsVar = false;
if (anyVar) {
Printer << (anyVar->isSettable(anyVar->getDeclContext()) ? "var " : "let ");
} else {
Printer << "let ";
printAsVar = (anyVar->isSettable(anyVar->getDeclContext()) ||
isInObjCImpl(anyVar));
}
Printer << (printAsVar ? "var " : "let ");
bool isFirst = true;
for (auto idx : range(decl->getNumPatternEntries())) {
auto *pattern = decl->getPattern(idx);
@@ -3958,9 +3960,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
// Map all non-let specifiers to 'var'. This is not correct, but
// SourceKit relies on this for info about parameter decls.
Printer.printIntroducerKeyword(
introducer == VarDecl::Introducer::Let ? "let" : "var",
Options, " ");
bool printAsVar = (introducer != VarDecl::Introducer::Let ||
isInObjCImpl(decl));
Printer.printIntroducerKeyword(printAsVar ? "var" : "let", Options, " ");
}
printContextIfNeeded(decl);
recordDeclLoc(decl,

View File

@@ -4,6 +4,7 @@
- (nonnull instancetype)init;
@property (readonly) int letProperty1;
@property (assign) int implProperty;
- (void)mainMethod:(int)param;

View File

@@ -42,6 +42,12 @@ import Foundation
didSet { print(implProperty) }
}
// CHECK-NOT: var letProperty1:
@objc public let letProperty1: Int32
// CHECK-DAG: @nonobjc public var letProperty2: Swift.Int32 { get }
@nonobjc public let letProperty2: Int32
// CHECK-DAG: final public var implProperty2: ObjectiveC.NSObject? { get set }
public final var implProperty2: NSObject?