Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,9 @@ ERROR(invalid_dynamic_member_lookup_type,none,
"'@dynamicMemberLookup' requires %0 to have a "
"'subscript(dynamicMember:)' method that accepts either "
"'ExpressibleByStringLiteral' or a key path", (Type))
NOTE(add_subscript_method, none,
"add to %0 a 'subscript(dynamicMember:)' method that accepts "
"either 'ExpressibleByStringLiteral' or a key path", (Type))
NOTE(invalid_dynamic_member_subscript, none,
"add an explicit argument label to this subscript to satisfy "
"the '@dynamicMemberLookup' requirement", ())
Expand Down
9 changes: 8 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
///
Expand Down Expand Up @@ -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)
.fixItReplace(decl->getLoc(), "dynamicMember");

emitInvalidTypeDiagnostic(attr->getLocation());
return;
}
Expand Down
3 changes: 2 additions & 1 deletion test/attr/attr_dynamic_member_lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

class InvalidDerived : InvalidBase { subscript(dynamicMember: String) -> Int { get {}} }

Expand Down Expand Up @@ -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}}
Expand Down