Skip to content

Commit a5f1351

Browse files
committed
Deprecate Application-Specific Main Entrypoint Attributes
Issue a deprecation warning in Swift 5 and an error in Swift 6 when we encounter @UIApplicationMain and @NSApplicationMain. These attributes are unnecessary now that @main works with UIApplicationDelegate and NSApplicationDelegate. As these conformances are required when working with the corresponding attributes, we can migrate users off of them by replacing them with @main.
1 parent 1a9a15f commit a5f1351

16 files changed

+44
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,6 +3399,9 @@ ERROR(attr_ApplicationMain_with_script,none,
33993399
"%" SELECT_APPLICATION_MAIN "0 attribute cannot be used in a module that contains "
34003400
"top-level code",
34013401
(unsigned))
3402+
ERROR(attr_ApplicationMain_deprecated,none,
3403+
"%" SELECT_APPLICATION_MAIN "0 is deprecated and will be removed in a future release; use '@main' instead",
3404+
(unsigned))
34023405

34033406
NOTE(attr_ApplicationMain_parse_as_library,none,
34043407
"pass '-parse-as-library' to compiler invocation if this is intentional",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,6 +2249,13 @@ void AttributeChecker::checkApplicationMainAttribute(DeclAttribute *attr,
22492249
attr->setInvalid();
22502250
}
22512251

2252+
diagnose(attr->getLocation(),
2253+
diag::attr_ApplicationMain_deprecated,
2254+
applicationMainKind)
2255+
.warnUntilSwiftVersion(6)
2256+
.fixItReplace(attr->getRange(), "@main");
2257+
2258+
22522259
if (attr->isInvalid())
22532260
return;
22542261

test/attr/ApplicationMain/attr_NSApplicationMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
import AppKit
66

7-
@NSApplicationMain
7+
@NSApplicationMain // expected-warning {{'NSApplicationMain' is deprecated and will be removed in a future release}}
88
class MyDelegate: NSObject, NSApplicationDelegate {
99
}

test/attr/ApplicationMain/attr_NSApplicationMain_inherited.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import AppKit
66

77
class DelegateBase : NSObject, NSApplicationDelegate { }
88

9-
@NSApplicationMain
9+
@NSApplicationMain // expected-warning {{'NSApplicationMain' is deprecated and will be removed in a future release}}
1010
class MyDelegate : DelegateBase { }
1111

test/attr/ApplicationMain/attr_NSApplicationMain_multi_file/another_delegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import AppKit
1010

1111
@NSApplicationMain // expected-error{{'NSApplicationMain' attribute can only apply to one class in a module}}
12+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
1213
class EvilDelegate: NSObject, NSApplicationDelegate {
1314
}
1415

test/attr/ApplicationMain/attr_NSApplicationMain_multi_file/delegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import AppKit
99

1010
@NSApplicationMain // expected-error{{'NSApplicationMain' attribute can only apply to one class in a module}}
11+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
1112
class MyDelegate: NSObject, NSApplicationDelegate {
1213
}
1314

test/attr/ApplicationMain/attr_NSApplicationMain_multiple.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import AppKit
66

77
@NSApplicationMain // expected-error{{'NSApplicationMain' attribute can only apply to one class in a module}}
8+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
89
class MyDelegate1: NSObject, NSApplicationDelegate {
910
}
1011

1112
@NSApplicationMain // expected-error{{'NSApplicationMain' attribute can only apply to one class in a module}}
13+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
1214
class MyDelegate2: NSObject, NSApplicationDelegate {
1315
}
1416

1517
@NSApplicationMain // expected-error{{'NSApplicationMain' attribute can only apply to one class in a module}}
18+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
1619
class MyDelegate3: NSObject, NSApplicationDelegate {
1720
}

test/attr/ApplicationMain/attr_NSApplicationMain_not_NSApplicationDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
import AppKit
66

77
@NSApplicationMain // expected-error{{'NSApplicationMain' class must conform to the 'NSApplicationDelegate' protocol}}
8+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
89
class MyNonDelegate {
910
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -swift-version 6 -typecheck -parse-as-library -verify %s
2+
3+
// REQUIRES: objc_interop
4+
5+
import AppKit
6+
7+
@NSApplicationMain // expected-error {{'NSApplicationMain' is deprecated and will be removed in a future release}}
8+
class MyDelegate: NSObject, NSApplicationDelegate {
9+
}

test/attr/ApplicationMain/attr_NSApplicationMain_with_main/delegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import AppKit
99

1010
@NSApplicationMain // expected-error{{'NSApplicationMain' attribute cannot be used in a module that contains top-level code}}
11+
// expected-warning@-1 {{'NSApplicationMain' is deprecated and will be removed in a future release}}
1112
class MyDelegate: NSObject, NSApplicationDelegate {
1213
}
1314

test/attr/ApplicationMain/attr_UIApplicationMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
import UIKit
66

7-
@UIApplicationMain
7+
@UIApplicationMain // expected-warning {{'UIApplicationMain' is deprecated and will be removed in a future release}}
88
class MyDelegate: NSObject, UIApplicationDelegate {
99
}

test/attr/ApplicationMain/attr_UIApplicationMain_inherited.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import UIKit
66

77
class DelegateBase : NSObject, UIApplicationDelegate { }
88

9-
@UIApplicationMain
9+
@UIApplicationMain // expected-warning {{'UIApplicationMain' is deprecated and will be removed in a future release}}
1010
class MyDelegate : DelegateBase { }
1111

test/attr/ApplicationMain/attr_UIApplicationMain_multiple.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import UIKit
66

77
@UIApplicationMain // expected-error{{'UIApplicationMain' attribute can only apply to one class in a module}}
8+
// expected-warning@-1 {{'UIApplicationMain' is deprecated and will be removed in a future release}}
89
class MyDelegate1: NSObject, UIApplicationDelegate {
910
}
1011

1112
@UIApplicationMain // expected-error{{'UIApplicationMain' attribute can only apply to one class in a module}}
13+
// expected-warning@-1 {{'UIApplicationMain' is deprecated and will be removed in a future release}}
1214
class MyDelegate2: NSObject, UIApplicationDelegate {
1315
}
1416

1517
@UIApplicationMain // expected-error{{'UIApplicationMain' attribute can only apply to one class in a module}}
18+
// expected-warning@-1 {{'UIApplicationMain' is deprecated and will be removed in a future release}}
1619
class MyDelegate3: NSObject, UIApplicationDelegate {
1720
}

test/attr/ApplicationMain/attr_UIApplicationMain_not_UIApplicationDelegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
import UIKit
66

77
@UIApplicationMain // expected-error{{'UIApplicationMain' class must conform to the 'UIApplicationDelegate' protocol}}
8+
// expected-warning@-1 {{'UIApplicationMain' is deprecated and will be removed in a future release}}
89
class MyNonDelegate {
910
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -swift-version 6 -typecheck -parse-as-library -verify %s
2+
3+
// REQUIRES: objc_interop
4+
5+
import UIKit
6+
7+
@UIApplicationMain // expected-error {{'UIApplicationMain' is deprecated and will be removed in a future release}}
8+
class MyDelegate: NSObject, UIApplicationDelegate {
9+
}

test/attr/ApplicationMain/attr_UIApplicationMain_with_main/delegate.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import UIKit
99

1010
@UIApplicationMain // expected-error{{'UIApplicationMain' attribute cannot be used in a module that contains top-level code}}
11+
// expected-warning@-1 {{'UIApplicationMain' is deprecated and will be removed in a future release}}
1112
class MyDelegate: NSObject, UIApplicationDelegate {
1213
}
1314

0 commit comments

Comments
 (0)