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

Conversation

mininny
Copy link
Contributor

@mininny mininny commented Aug 16, 2021

Currently, fixAvailabilityForDecl() in TypeCheckAvailability.cpp exits early if there is already an @available attribute on the declaration, with a comment noting work that should be done in the future:

/// Emit a diagnostic note and Fix-It to add an @available attribute
/// on the given declaration for the given version range.
static void fixAvailabilityForDecl(SourceRange ReferenceRange, const Decl *D,
                                   const VersionRange &RequiredRange,
                                   ASTContext &Context) {
  // ...irrelevant code omitted...

  if (getActiveAvailableAttribute(D, Context)) {
    // For QoI, in future should emit a fixit to update the existing attribute.
    return;
  }

Indeed, we should make this change. For instance, if you give the compiler this code:

@available(macOS 42, *) func foo() {}

@available(macOS 12, *) func bar() {
    foo()
}

Swift should emit a note with a fix-it replacing "macOS 12" with "macOS 42".

Resolves SR-15051

@@ -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?

@mininny mininny marked this pull request as ready for review August 28, 2021 08:20
@mininny
Copy link
Contributor Author

mininny commented Sep 7, 2021

Wondering if you had a chance to look at this :) @beccadax

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant