-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[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
[Qol]Fixit nested static value #11640
Conversation
Variable name in nested static value should be fixed to have complete declarelation
lib/Sema/CSDiag.cpp
Outdated
@@ -2559,7 +2559,7 @@ diagnoseTypeMemberOnInstanceLookup(Type baseObjTy, | |||
->getAsNominalTypeOrNominalTypeExtensionContext(); | |||
SmallString<32> typeName; | |||
llvm::raw_svector_ostream typeNameStream(typeName); | |||
typeNameStream << nominal->getName() << "."; | |||
typeNameStream << nominal->getDeclaredInterfaceType() << "."; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.}} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
getDeclaredInterfaceType() is invalid for protocol. getSelfInterfaceType() is good for both concrete types and protocol.
Looks good, thanks! I'll look at the other code path later, maybe they're not really possible to unify. |
@swift-ci Please smoke test |
1 similar comment
@swift-ci Please smoke test |
Hi @gibachan, can you take a look at the test failure?
|
Hi @slavapestov, thank you for your support! |
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. |
Thanks @slavapestov! I followed your advice and I tried tests locally with |
@swift-ci Please smoke test |
This PR fixes a insufficient fixit on use of static value in nested structure.
Resolves SR-5715.