Skip to content

Commit dfae629

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 d553d04 commit dfae629

17 files changed

+45
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3408,6 +3408,9 @@ ERROR(attr_ApplicationMain_with_script,none,
34083408
"%" SELECT_APPLICATION_MAIN "0 attribute cannot be used in a module that contains "
34093409
"top-level code",
34103410
(unsigned))
3411+
ERROR(attr_ApplicationMain_deprecated,none,
3412+
"%" SELECT_APPLICATION_MAIN "0 is deprecated and will be removed in a future release; use '@main' instead",
3413+
(unsigned))
34113414

34123415
NOTE(attr_ApplicationMain_parse_as_library,none,
34133416
"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
@@ -2260,6 +2260,13 @@ void AttributeChecker::checkApplicationMainAttribute(DeclAttribute *attr,
22602260
attr->setInvalid();
22612261
}
22622262

2263+
diagnose(attr->getLocation(),
2264+
diag::attr_ApplicationMain_deprecated,
2265+
applicationMainKind)
2266+
.warnUntilSwiftVersion(6)
2267+
.fixItReplace(attr->getRange(), "@main");
2268+
2269+
22632270
if (attr->isInvalid())
22642271
return;
22652272

test/TBD/objc-entry-point.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ import AppKit
99
// present in the TBD.
1010
let globalConstantWithLazyInitializer: String = "hello, world"
1111

12-
@NSApplicationMain
12+
@NSApplicationMain // expected-warning {{'NSApplicationMain' is deprecated and will be removed in a future release; use '@main' instead}}
1313
class MyDelegate: NSObject, NSApplicationDelegate {
1414
}

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)