Skip to content

[Diagnostics] Modify diagnostics to suggest updating existing available attribute #38887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5327,6 +5327,8 @@ NOTE(availability_guard_with_version_check, none,

NOTE(availability_add_attribute, none,
"add @available attribute to enclosing %0", (DescriptiveDeclKind))
NOTE(availability_update_attribute, none,
"update existing @available attribute", ())
FIXIT(insert_available_attr,
"@available(%0 %1, *)\n%2",
(StringRef, StringRef, StringRef))
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,8 +1453,10 @@ static void fixAvailabilityForDecl(SourceRange ReferenceRange, const Decl *D,
if (TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(D).hasValue())
return;

if (getActiveAvailableAttribute(D, Context)) {
// For QoI, in future should emit a fixit to update the existing attribute.
if (auto *attr = getActiveAvailableAttribute(D, Context)) {
D->diagnose(diag::availability_update_attribute)
.fixItReplace(attr->IntroducedRange,
RequiredRange.getLowerEndpoint().getAsString());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// REQUIRES: OS=macosx

@main // expected-error {{'main()' is only available in macOS 10.99 or newer}}
@available(OSX 10.0, *)
struct EntryPoint {
@available(OSX 10.0, *) struct EntryPoint { // expected-note {{update existing @available attribute}} {{16-20=10.99}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why but adding expected fixit note like:

@available(OSX 10.0, *) // expected-note {{16-20=10.99}}

didn't work. (maybe because the original note is from the struct EntryPointline?)
So I put the two expressions in the same line, but is there a proper way to fix this?

@available(OSX 10.99, *)
static func main() {
}
Expand Down
16 changes: 13 additions & 3 deletions test/attr/attr_availability_osx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,29 @@ extension TestStruct { // expected-note {{enclosing scope here}}
@available(swift 1)
func doFourthThing() {}

@available(macOS 10.12, iOS 13, tvOS 13, *)
func doFifthThing() {}

@available(*, deprecated)
func doDeprecatedThing() {}
}

@available(macOS 10.11, *)
func testMemberAvailability() {
@available(macOS 10.11, *) func testMemberAvailability() { // expected-note {{update existing @available attribute}} {{18-23=10.12}}
TestStruct().doTheThing() // expected-error {{'doTheThing()' is unavailable}}
TestStruct().doAnotherThing() // expected-error {{'doAnotherThing()' is unavailable}}
TestStruct().doThirdThing() // expected-error {{'doThirdThing()' is unavailable}}
TestStruct().doFourthThing() // expected-error {{'doFourthThing()' is only available in macOS 10.12 or newer}} expected-note {{'if #available'}}
TestStruct().doDeprecatedThing() // expected-warning {{'doDeprecatedThing()' is deprecated}}
}

@available(iOS 11, macOS 10.11, *) func testUpdateAvailabilityAttribute() { // expected-note {{update existing @available attribute}} {{26-31=10.12}}
TestStruct().doFifthThing() // expected-error {{'doFifthThing()' is only available in macOS 10.12 or newer}} expected-note {{'if #available'}}
}

@available(macOS, introduced: 10.11) func testUpdateAvailabilityAttribute2() { // expected-note {{update existing @available attribute}} {{31-36=10.12}}
TestStruct().doFifthThing() // expected-error {{'doFifthThing()' is only available in macOS 10.12 or newer}} expected-note {{'if #available'}}
}

extension TestStruct {
struct Data {
mutating func mutate() {}
Expand Down Expand Up @@ -208,4 +218,4 @@ extension UnavailableStruct { } // no-error
#if os(macOS)
@available(macOS, unavailable)
extension UnavailableStruct { } // no-error
#endif
#endif