Skip to content

Commit 48849dc

Browse files
committed
Shop Over-Availability Diagnostics
Adopt descriptive language and mention the actual availability bounds that are at issue when a declaration is more available than its enclosing scope.
1 parent 77bf3af commit 48849dc

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5575,10 +5575,12 @@ 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))
55795580

55805581
NOTE(availability_decl_more_than_enclosing_enclosing_here, none,
5581-
"enclosing scope here", ())
5582+
"enclosing scope requires availability of %0 %1 or newer",
5583+
(StringRef, llvm::VersionTuple))
55825584

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

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,9 +1710,13 @@ 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());
17141716
diagnose(EnclosingDecl->getLoc(),
1715-
diag::availability_decl_more_than_enclosing_enclosing_here);
1717+
diag::availability_decl_more_than_enclosing_enclosing_here,
1718+
prettyPlatformString(targetPlatform(Ctx.LangOpts)),
1719+
EnclosingAnnotatedRange->getOSVersion().getLowerEndpoint());
17161720
}
17171721
}
17181722

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

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)