Skip to content

Commit 3e4e354

Browse files
author
Nathan Hawes
committed
[migrator] Only enable Swift 3 ObjC inference warnings when migrating from Swift 3
They were always enabled, meaning migrating from Swift 4 -> 4.2 would pick up the associated fixits and add @objc unnecessarily in many places. Resolves rdar://problem/39951671
1 parent ac38f00 commit 3e4e354

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

lib/Migrator/Migrator.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,22 @@ Migrator::performAFixItMigration(version::Version SwiftLanguageVersion) {
133133
LLVMArgs.erase(aarch64_use_tbi);
134134
}
135135

136-
// SE-0160: When migrating, always use the Swift 3 @objc inference rules,
137-
// which drives warnings with the "@objc" Fix-Its.
138-
Invocation.getLangOptions().EnableSwift3ObjCInference = true;
139-
140-
// The default behavior of the migrator, referred to as "minimal" migration
141-
// in SE-0160, only adds @objc Fix-Its to those cases where the Objective-C
142-
// entry point is explicitly used somewhere in the source code. The user
143-
// may also select a workflow that adds @objc for every declaration that
144-
// would infer @objc under the Swift 3 rules but would no longer infer
145-
// @objc in Swift 4.
146-
Invocation.getLangOptions().WarnSwift3ObjCInference =
147-
getMigratorOptions().KeepObjcVisibility
148-
? Swift3ObjCInferenceWarnings::Complete
149-
: Swift3ObjCInferenceWarnings::Minimal;
136+
if (StartInvocation.getLangOptions().EffectiveLanguageVersion.isVersion3()) {
137+
// SE-0160: When migrating, always use the Swift 3 @objc inference rules,
138+
// which drives warnings with the "@objc" Fix-Its.
139+
Invocation.getLangOptions().EnableSwift3ObjCInference = true;
140+
141+
// The default behavior of the migrator, referred to as "minimal" migration
142+
// in SE-0160, only adds @objc Fix-Its to those cases where the Objective-C
143+
// entry point is explicitly used somewhere in the source code. The user
144+
// may also select a workflow that adds @objc for every declaration that
145+
// would infer @objc under the Swift 3 rules but would no longer infer
146+
// @objc in Swift 4.
147+
Invocation.getLangOptions().WarnSwift3ObjCInference =
148+
getMigratorOptions().KeepObjcVisibility
149+
? Swift3ObjCInferenceWarnings::Complete
150+
: Swift3ObjCInferenceWarnings::Minimal;
151+
}
150152

151153
const auto &OrigFrontendOpts = StartInvocation.getFrontendOptions();
152154

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: objc_interop
22
// RUN: %target-swift-frontend -typecheck -swift-version 3 -enable-swift3-objc-inference %s
3-
// RUN: %empty-directory(%t) && %target-swift-frontend -c -primary-file %S/Inputs/objc_inference.swift -emit-migrated-file-path %t/complete_objc_inference.swift.result -emit-remap-file-path %t/complete_objc_inference.swift.remap -migrate-keep-objc-visibility -o /dev/null
4-
// RUN: diff -u %S/complete_objc_inference.swift.expected %t/complete_objc_inference.swift.result
3+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -primary-file %S/Inputs/objc_inference.swift -swift-version 3 -emit-migrated-file-path %t/complete_objc_inference.swift.result -emit-remap-file-path %t/complete_objc_inference.swift.remap -migrate-keep-objc-visibility -o /dev/null
4+
// RUN: diff -u %S/complete_objc_inference.swift.expected %t/complete_objc_inference.swift.result
55
// RUN: %target-swift-frontend -typecheck -swift-version 4 -enable-swift3-objc-inference %t/complete_objc_inference.swift.result
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// REQUIRES: objc_interop
22
// RUN: %target-swift-frontend -typecheck -swift-version 3 %s
3-
// RUN: %empty-directory(%t) && %target-swift-frontend -c -primary-file %S/Inputs/objc_inference.swift -emit-migrated-file-path %t/minimal_objc_inference.swift.result -emit-remap-file-path %t/minimal_objc_inference.swift.remap -o /dev/null
3+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -primary-file %S/Inputs/objc_inference.swift -swift-version 3 -emit-migrated-file-path %t/minimal_objc_inference.swift.result -emit-remap-file-path %t/minimal_objc_inference.swift.remap -o /dev/null
44
// RUN: diff -u %S/minimal_objc_inference.swift.expected %t/minimal_objc_inference.swift.result
55
// RUN: %target-swift-frontend -typecheck -swift-version 4 %t/minimal_objc_inference.swift.result
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: objc_interop
2+
// RUN: %target-swift-frontend -typecheck -swift-version 4 %s
3+
// RUN: %empty-directory(%t) && %target-swift-frontend -c -primary-file %s -swift-version 4 -emit-migrated-file-path %t/objc_inference_noop.swift.result -emit-remap-file-path %t/objc_inference_noop.swift.remap -migrate-keep-objc-visibility -o /dev/null
4+
// RUN: diff -u %s %t/objc_inference_noop.swift.result
5+
6+
import Foundation
7+
8+
@objc class Foo : NSObject {
9+
var foo: Int = 10
10+
}

0 commit comments

Comments
 (0)