Skip to content

Commit 7c73a28

Browse files
authored
Merge pull request #58725 from CodaFi/catalytic-converter
Improve Over-Availability Diagnostics
2 parents 2193c25 + 4837853 commit 7c73a28

File tree

9 files changed

+64
-14
lines changed

9 files changed

+64
-14
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5575,10 +5575,16 @@ NOTE(note_deprecated_rename, none,
55755575
"use '%0' instead", (StringRef))
55765576

55775577
ERROR(availability_decl_more_than_enclosing, none,
5578-
"declaration cannot be more available than enclosing scope", ())
5578+
"%0 cannot be more available than enclosing scope",
5579+
(DescriptiveDeclKind))
5580+
5581+
NOTE(availability_implicit_decl_here, none,
5582+
"%0 implicitly declared here with availability of %1 %2 or newer",
5583+
(DescriptiveDeclKind, StringRef, llvm::VersionTuple))
55795584

55805585
NOTE(availability_decl_more_than_enclosing_enclosing_here, none,
5581-
"enclosing scope here", ())
5586+
"enclosing scope requires availability of %0 %1 or newer",
5587+
(StringRef, llvm::VersionTuple))
55825588

55835589
ERROR(availability_decl_only_version_newer, none,
55845590
"%0 is only available in %1 %2 or newer",

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,9 +2177,10 @@ bool PlatformAvailability::isPlatformRelevant(StringRef name) const {
21772177
return name == "ios" || name == "ios_app_extension";
21782178

21792179
case PlatformKind::macCatalyst:
2180+
return name == "ios" || name == "maccatalyst";
21802181
case PlatformKind::macCatalystApplicationExtension:
2181-
// ClangImporter does not yet support macCatalyst.
2182-
return false;
2182+
return name == "ios" || name == "ios_app_extension" ||
2183+
name == "maccatalyst" || name == "maccatalyst_app_extension";
21832184

21842185
case PlatformKind::tvOS:
21852186
return name == "tvos";

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9083,9 +9083,12 @@ void ClangImporter::Implementation::importAttributes(
90839083
llvm::StringSwitch<Optional<PlatformKind>>(Platform)
90849084
.Case("ios", PlatformKind::iOS)
90859085
.Case("macos", PlatformKind::macOS)
9086+
.Case("maccatalyst", PlatformKind::macCatalyst)
90869087
.Case("tvos", PlatformKind::tvOS)
90879088
.Case("watchos", PlatformKind::watchOS)
90889089
.Case("ios_app_extension", PlatformKind::iOSApplicationExtension)
9090+
.Case("maccatalyst_app_extension",
9091+
PlatformKind::macCatalystApplicationExtension)
90899092
.Case("macos_app_extension",
90909093
PlatformKind::macOSApplicationExtension)
90919094
.Case("tvos_app_extension",

lib/Sema/TypeCheckAttr.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,9 +1710,19 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *attr) {
17101710

17111711
if (EnclosingDecl) {
17121712
if (!AttrRange.isContainedIn(EnclosingAnnotatedRange.getValue())) {
1713-
diagnose(attr->getLocation(), diag::availability_decl_more_than_enclosing);
1713+
diagnose(D->isImplicit() ? EnclosingDecl->getLoc() : attr->getLocation(),
1714+
diag::availability_decl_more_than_enclosing,
1715+
D->getDescriptiveKind());
1716+
if (D->isImplicit())
1717+
diagnose(EnclosingDecl->getLoc(),
1718+
diag::availability_implicit_decl_here,
1719+
D->getDescriptiveKind(),
1720+
prettyPlatformString(targetPlatform(Ctx.LangOpts)),
1721+
AttrRange.getOSVersion().getLowerEndpoint());
17141722
diagnose(EnclosingDecl->getLoc(),
1715-
diag::availability_decl_more_than_enclosing_enclosing_here);
1723+
diag::availability_decl_more_than_enclosing_enclosing_here,
1724+
prettyPlatformString(targetPlatform(Ctx.LangOpts)),
1725+
EnclosingAnnotatedRange->getOSVersion().getLowerEndpoint());
17161726
}
17171727
}
17181728

test/Sema/availability_versions.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,19 +615,19 @@ class ClassAvailableOn10_9 {
615615
}
616616

617617
@available(OSX, introduced: 10.51)
618-
class ClassAvailableOn10_51 { // expected-note {{enclosing scope here}}
618+
class ClassAvailableOn10_51 { // expected-note {{enclosing scope requires availability of macOS 10.51 or newer}}
619619
func someMethod() {}
620620
class func someClassMethod() {
621621
let _ = ClassAvailableOn10_51()
622622
}
623623
var someProp : Int = 22
624624

625-
@available(OSX, introduced: 10.9) // expected-error {{declaration cannot be more available than enclosing scope}}
625+
@available(OSX, introduced: 10.9) // expected-error {{instance method cannot be more available than enclosing scope}}
626626
func someMethodAvailableOn10_9() { }
627627

628628
@available(OSX, introduced: 10.52)
629-
var propWithGetter: Int { // expected-note{{enclosing scope here}}
630-
@available(OSX, introduced: 10.51) // expected-error {{declaration cannot be more available than enclosing scope}}
629+
var propWithGetter: Int { // expected-note{{enclosing scope requires availability of macOS 10.52 or newer}}
630+
@available(OSX, introduced: 10.51) // expected-error {{getter cannot be more available than enclosing scope}}
631631
get { return 0 }
632632
}
633633
}
@@ -1022,8 +1022,8 @@ extension ClassToExtend : ProtocolAvailableOn10_51 {
10221022
}
10231023

10241024
@available(OSX, introduced: 10.51)
1025-
extension ClassToExtend { // expected-note {{enclosing scope here}}
1026-
@available(OSX, introduced: 10.9) // expected-error {{declaration cannot be more available than enclosing scope}}
1025+
extension ClassToExtend { // expected-note {{enclosing scope requires availability of macOS 10.51 or newer}}
1026+
@available(OSX, introduced: 10.9) // expected-error {{instance method cannot be more available than enclosing scope}}
10271027
func extensionMethod10_9() { }
10281028
}
10291029

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@import Foundation;
2+
3+
__attribute__((availability(macosx,introduced=10.0)))
4+
__attribute__((availability(ios,introduced=2.0)))
5+
__attribute__((availability(tvos,introduced=1.0)))
6+
__attribute__((availability(watchos,introduced=2.0)))
7+
__attribute__((availability(maccatalyst,introduced=13.1)))
8+
@interface NSBaseClass : NSObject
9+
- (instancetype) init
10+
__attribute__((objc_designated_initializer))
11+
__attribute__((availability(macosx,introduced=10.0)))
12+
__attribute__((availability(ios,introduced=2.0)))
13+
__attribute__((availability(tvos,introduced=1.0)))
14+
__attribute__((availability(watchos,introduced=2.0)))
15+
__attribute__((availability(maccatalyst,introduced=13.1)));
16+
@end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module AttrObjc_FooClangModule { header "attr_objc_foo_clang_module.h" }
22
module Testable_ClangModule { header "testable_clang.h" }
33
module ObjcAsync { header "objc_async.h" }
4+
module Available_NSObject { header "available_nsobject.h" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-swift-frontend -I %t -I %S/Inputs/custom-modules -parse-stdlib -parse-as-library -typecheck -verify -target x86_64-apple-ios15.4-macabi %s
2+
3+
// REQUIRES: objc_interop
4+
// REQUIRES: maccatalyst_support
5+
6+
import Available_NSObject
7+
8+
@available(iOS 15.0, *)
9+
open class OverAvailableClass: NSBaseClass {}
10+
// expected-error@-1 {{initializer cannot be more available than enclosing scope}}
11+
// expected-note@-2 {{initializer implicitly declared here with availability of Mac Catalyst 13.1 or newer}}
12+
// expected-note@-3 {{enclosing scope requires availability of Mac Catalyst 15.0 or newer}}
13+

test/attr/attr_availability_osx.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ doSomethingDeprecatedOniOS() // okay
8484
struct TestStruct {}
8585

8686
@available(macOS 10.10, *)
87-
extension TestStruct { // expected-note {{enclosing scope here}}
87+
extension TestStruct { // expected-note {{enclosing scope requires availability of macOS 10.10 or newer}}
8888
@available(swift 400)
8989
func doTheThing() {} // expected-note {{'doTheThing()' was introduced in Swift 400}}
9090

91-
@available(macOS 10.9, *) // expected-error {{declaration cannot be more available than enclosing scope}}
91+
@available(macOS 10.9, *) // expected-error {{instance method cannot be more available than enclosing scope}}
9292
@available(swift 400)
9393
func doAnotherThing() {} // expected-note {{'doAnotherThing()' was introduced in Swift 400}}
9494

0 commit comments

Comments
 (0)