Skip to content

[Qol]Fixit nested static value #11640

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

Merged
merged 10 commits into from
Sep 15, 2017

Conversation

gibachan
Copy link
Contributor

This PR fixes a insufficient fixit on use of static value in nested structure.

Resolves SR-5715.

Variable name in nested static value should be fixed to have complete declarelation
@@ -2559,7 +2559,7 @@ diagnoseTypeMemberOnInstanceLookup(Type baseObjTy,
->getAsNominalTypeOrNominalTypeExtensionContext();
SmallString<32> typeName;
llvm::raw_svector_ostream typeNameStream(typeName);
typeNameStream << nominal->getName() << ".";
typeNameStream << nominal->getDeclaredInterfaceType() << ".";
Copy link
Contributor

@CodaFi CodaFi Aug 26, 2017

Choose a reason for hiding this comment

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

I'm a little hesitant to print this directly, but it seems to be correct. Could you add a test that uses a generic nested type as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

The declared interface type should be fine. It will use sugared generic parameter names and include the full parent type. So for a nested generic type, it should print Outer<T>.Inner<U, V>, etc, and we should add a test for this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually to handle members of protocols you want getSelfInterfaceType() instead. This is identical to getDeclaredInterfaceType() for concrete types, but for protocols it has 'Self' instead of the existential.

Right now, this produces a bad fixit:

protocol P {}

extension P {
  static func f() {}

  func g() {
    f
  }
}

ext.swift:7:5: error: static member 'f' cannot be used on instance of type 'Self'
    f
    ^
    P.

Note that the correct fix is to add 'Self.', not 'P.'.

@gibachan Do you mind adding a test for this case as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for kind response.
I made those fixes, but now I get a strange error on the test you advised above.
When I run the test by utils/run-test, the error message says "use of unresolved identifier 'f'" instead of "static member 'f' cannot be used ...". However if I run it by ../build/Ninja-ReleaseAssert/swift-macosx-x86_64/bin/swift, it says "static member 'f' cannot be used ..." as I expect. I do not know the difference between them. Am I missing something?

func f() -> Int {
return x // expected-error {{static member 'x' cannot be used on instance of type 'SR5715.A.B'}} {{16-16=SR5715.A.B.}}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Drive-by nit: could you please reformat these lines to use two-space indents like the rest of the file, and restore the trailing newline?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I did reformat lines.

@slavapestov
Copy link
Contributor

Hi @gibachan, can you take a look at this PR? #11609 It looks like there are two very similar code paths in diagnostics to handle the same error of an instance member being used on static type value, or vice versa. Maybe there's a function that can be factored out here.

cc @Kacper20

getDeclaredInterfaceType() is invalid for protocol. getSelfInterfaceType() is good for both concrete types and protocol.
@slavapestov slavapestov self-assigned this Aug 28, 2017
@slavapestov
Copy link
Contributor

Looks good, thanks! I'll look at the other code path later, maybe they're not really possible to unify.

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

1 similar comment
@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor

Hi @gibachan, can you take a look at the test failure?

/Users/buildnode/jenkins/workspace/swift-PR-osx-smoke-test/branch-master/swift/test/expr/postfix/dot/dot_keywords.swift:104:31: error: incorrect message found
    f()   // expected-error {{static member 'f' cannot be used on instance of type 'Self'}} {{12-12=Self.}}
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                              use of unresolved identifier 'f'

@gibachan
Copy link
Contributor Author

gibachan commented Sep 5, 2017

Hi @slavapestov, thank you for your support!
The failed test expects that statement lacks of 'Self.' but it was different because of swift version.
The expected error happens only with Swift4. With Swift3, it would be as described above.
Would you please let me know how to handle the different results by Swift version in test case like this situation? or any better approach?

@slavapestov
Copy link
Contributor

Oh right, Swift 3 had a strange quirk with unqualified lookup vs static members...

You could update the test's RUN line to pass -swift-version 4, and add a new test case to test/Compatibility/ to test the old behavior.

Also do you know how to run the tests locally? That would save you a lot of time while iterating on this.

@gibachan
Copy link
Contributor Author

gibachan commented Sep 7, 2017

Thanks @slavapestov! I followed your advice and I tried tests locally with utils/build-script -RT then it passed without any unexpected failures.
Could you please take a look at it?

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test

@slavapestov slavapestov merged commit a102341 into swiftlang:master Sep 15, 2017
@gibachan gibachan deleted the fixit-nested-static-value branch July 17, 2023 12:00
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.

4 participants