-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Fixit for missing @dynamicMemberLookup impl #83440
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2106,6 +2106,7 @@ bool swift::isValidKeyPathDynamicMemberLookup(SubscriptDecl *decl, | |
return bool(getKeyPathTypeForDynamicMemberLookup(decl, ignoreLabel)); | ||
} | ||
|
||
|
||
/// The @dynamicMemberLookup attribute is only allowed on types that have at | ||
/// least one subscript member declared like this: | ||
/// | ||
|
@@ -2161,8 +2162,14 @@ visitDynamicMemberLookupAttr(DynamicMemberLookupAttr *attr) { | |
return isValidDynamicMemberLookupSubscript(cand, /*ignoreLabel*/ true); | ||
}); | ||
|
||
// If there were no potentially valid candidates, then throw an error. | ||
// If there were no potentially valid candidates, then throw an error and emit a fix-it. | ||
if (newCandidates.empty()) { | ||
|
||
// Emit a fix-it to suggest the user to add a subscript method. | ||
auto &d = ctx.Diags; | ||
d.diagnose(decl->getLoc(), diag::add_subscript_method, type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can call |
||
.fixItReplace(decl->getLoc(), "dynamicMember"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should suggest to add |
||
|
||
emitInvalidTypeDiagnostic(attr->getLocation()); | ||
Comment on lines
+2167
to
2173
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notes are secondary diagnostics, i.e. they are always attached to a primary diagnostic such as a warning or error, so emission order is important — warning/error first, then its notes. |
||
return; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,7 +191,7 @@ func NotAllowedOnFunc() {} | |
|
||
// expected-error @+1 {{'@dynamicMemberLookup' requires 'InvalidBase' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}} | ||
@dynamicMemberLookup | ||
class InvalidBase {} | ||
class InvalidBase {} // expected-note {{add to 'InvalidBase' a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a check to make sure that fix-it it positioned correctly, similar to https://github.com/swiftlang/swift/pull/83440/files#diff-146c63878b2e88e6d9540d0fbd3ba9c7987ca24f36fc6b22ca3de1aef17fe143R806 |
||
|
||
class InvalidDerived : InvalidBase { subscript(dynamicMember: String) -> Int { get {}} } | ||
|
||
|
@@ -787,6 +787,7 @@ do { | |
|
||
// https://github.com/apple/swift/issues/52957 | ||
|
||
|
||
@dynamicMemberLookup | ||
struct S1_52957 { | ||
subscript(dynamicMember: String) -> String { // expected-error {{'@dynamicMemberLookup' requires 'S1_52957' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}} | ||
|
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 don't think we need "add to <>" here because the note is attached to the declaration in question already.
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 could also be modeled similar to a "missing unwrap" diagnostic where there are two notes - one for
?
and one for!
, each with a dedicated fix-it.