-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Provide a fix-it that inserts omitted generic parameters when they can't be inferred #4682
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
Provide a fix-it that inserts omitted generic parameters when they can't be inferred #4682
Conversation
@nkcsgexi, @akyrtzi, what do you think about what's there so far? In particular, what do you think about the defaults?
Stepping back, is it okay to use placeholders at all here? (@DougGregor?) I see we have one other diagnostic with placeholders. Maybe we can change the text output format to special-case them (later). |
@swift-ci Please smoke test |
if (FoundGenericTypeBase && !isa<GenericIdentTypeRepr>(FoundGenericTypeBase)){ | ||
assert(FoundDecl); | ||
|
||
SmallString<64> genericParamBuf; |
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.
Is this in any way similar to diagnoseUnboundGenericType() in TypeCheckType.cpp?
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.
Oh, good catch. I'll see if I can factor out common logic here. (I like my implementation better.)
I like the idea of using placeholders in fixits. We have a precedent of that in the fixits for auto-filling protocol conformances skeletons. |
I like the idea of using placeholders. It works very well in Xcode, and is fairly easy to handle in other tools that might want to present Fix-Its to the user. |
2c749af
to
63fcb91
Compare
Implementation mostly done, but I still need to write tests. @swift-ci Please test |
63fcb91
to
85b793e
Compare
Whoops, a test change snuck in there. Gotta fix that. |
Build failed |
Build failed |
85b793e
to
634055e
Compare
And it looks like master's messed up at the moment. I'll wait. |
Will review. This is a fixit associated with error, right? Isn't that automatically picked up by migrator? |
I guess also:
|
@nkcsgexi The new fix-it is on a note because usually you want a more specific type than the default…but in migration, you might not have a type because in Swift 2.3 it wasn't generic. |
@swift-ci Please test |
1 similar comment
@swift-ci Please test |
Build failed |
634055e
to
66fd330
Compare
@swift-ci Please test Linux platform |
macOS full tests passed earlier. @swift-ci Please smoke test macOS platform |
Build failed |
^ @aciidb0mb3r, have you seen this SwiftPM failure before? (unrelated to this patch) |
@jrose-apple unfortunately yes, I disabled a similar test failure here will disable all related tests. |
Disabled here: swiftlang/swift-package-manager@76714d2 |
Thanks, Ankit. @swift-ci Please test Linux platform |
return Type(); | ||
|
||
TypeVariableType *unused; | ||
Type maybeFixedType = cs.getFixedTypeRecursive(preferred, unused, |
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 confused here. If we can get generic argument type, why do we invoke CS to fix it? to remove unsubstituted type variables?
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.
This is for cases where we have some but not all of the generic arguments. Suggesting <Int, Any>
is usually better than <Any, Any>
.
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.
oh, I see. Mind adding some inline comments?
|
||
if (protocols.empty()) { | ||
genericParamText << "Any"; | ||
return; |
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.
For future proof, can we retrieve "Any" from ASTContext.getAnyDecl()->getName().
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.
Otherwise, LGTM!
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.
There's no "Any" decl anymore, but I'll switch it to Context.ID_Any
so it's at least more searchable.
66fd330
to
ebb639d
Compare
Updated for Xi's review comments. @swift-ci Please smoke test |
Thank you! |
For example, if someone tries to use the newly-generic type Cache, from Foundation: var cache = Cache() they'll now get a fix-it to substitute the default generic parameters: var cache = Cache<AnyObject, AnyObject>() The rules for choosing this placeholder type are based on constraints and won't be right 100% of the time, but they should be reasonable. (In particular, constraints on associated types are ignored.) In cases where there's no one concrete type that will work, an Xcode- style placeholder is inserted instead. - An unconstrained generic parameter defaults to 'Any'. - A superclass-constrained parameter defaults to that class, e.g. 'UIView'. - A parameter constrained to a single @objc protocol (or to AnyObject) defaults to that protocol, e.g. 'NSCoding'. - Anything else gets a placeholder using the generic parameter's name and protocol composition syntax. rdar://problem/27087345
Slava pointed out that we have an existing version of the code from the previous commit that's only used when checking types. Replace it with the new code, which handles more cases.
Last bit of rdar://problem/27087345.
ebb639d
to
fea7878
Compare
@swift-ci Please smoke test |
1 similar comment
@swift-ci Please smoke test |
Connectivity issue. @swift-ci Please smoke test OS X platform |
@swift-ci Please smoke test OS X platform |
1 similar comment
@swift-ci Please smoke test OS X platform |
Provide a fix-it that inserts omitted generic parameters when they can't be inferred. rdar://problem/27087345
Still waiting for feedback from @DougGregor or @rudkx on accessing the constraint system, and @slavapestov on the use of GenericTypeParamDecl. |
@jrose-apple I suspect this is safe, but I could not guarantee it. |
This will help when migrating to Swift 3 and Objective-C classes with newly-added generic parameters.
Still to do:
rdar://problem/27087345