Skip to content
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))
Copy link
Contributor

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.

Copy link
Contributor

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.

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can call diagnostic directly on AttributeChecker here.

.fixItReplace(decl->getLoc(), "dynamicMember");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should suggest to add func subscript(dynamicMember member: <#Type#>) -> <#Result> {} stub.


emitInvalidTypeDiagnostic(attr->getLocation());
Comment on lines +2167 to 2173
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
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}}
Copy link
Contributor

Choose a reason for hiding this comment

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


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