Skip to content

Commit cf5e1ff

Browse files
committed
[Diagnostics] Add a tailored diagnostic for contextual lookup on protocols without Self requirement
1 parent 2d8f002 commit cf5e1ff

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,10 @@ ERROR(type_does_not_conform_owner,none,
19021902
ERROR(type_does_not_conform_in_decl_ref,none,
19031903
"referencing %0 %1 on %2 requires that %3 conform to %4",
19041904
(DescriptiveDeclKind, DeclName, Type, Type, Type))
1905+
ERROR(contextual_member_ref_on_protocol_requires_self_requirement,none,
1906+
"contextual member reference to %0 %1 requires "
1907+
"'Self' constraint in the protocol extension",
1908+
(DescriptiveDeclKind, DeclName))
19051909
NOTE(missing_sametype_requirement_on_self,none,
19061910
"missing same-type requirement on 'Self'", ())
19071911
ERROR(type_does_not_conform_anyobject_in_decl_ref,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7202,8 +7202,9 @@ bool InvalidMemberRefOnProtocolMetatype::diagnoseAsError() {
72027202
auto *member = overload->choice.getDeclOrNull();
72037203
assert(member);
72047204

7205-
emitDiagnostic(diag::cannot_infer_base_of_unresolved_member,
7206-
DeclNameRef(member->getName()));
7205+
emitDiagnostic(
7206+
diag::contextual_member_ref_on_protocol_requires_self_requirement,
7207+
member->getDescriptiveKind(), member->getName());
72077208

72087209
auto *extension = dyn_cast<ExtensionDecl>(member->getDeclContext());
72097210

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6861,6 +6861,12 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
68616861
// type via same-type requirement, otherwise it would be
68626862
// impossible to find a witness for this member.
68636863

6864+
if (!isa<ExtensionDecl>(decl->getDeclContext())) {
6865+
result.addUnviable(candidate,
6866+
MemberLookupResult::UR_TypeMemberOnInstance);
6867+
return;
6868+
}
6869+
68646870
// Cannot instantiate a protocol or reference a member on
68656871
// protocol composition type.
68666872
if (isa<ConstructorDecl>(decl) ||

test/Constraints/static_members_on_protocol_in_generic_context.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,43 +227,43 @@ _ = P[q: ""].other
227227
test(.doesntExist) // expected-error {{type 'P' has no member 'doesntExist'}}
228228
test(.doesnt.exist()) // expected-error {{type 'P' has no member 'doesnt'}}
229229
test(.invalidProp)
230-
// expected-error@-1 {{cannot infer contextual base in reference to member 'invalidProp'}}
230+
// expected-error@-1 {{contextual member reference to static property 'invalidProp' requires 'Self' constraint in the protocol extension}}
231231
test(.invalidProp.other)
232-
// expected-error@-1 {{cannot infer contextual base in reference to member 'invalidProp'}}
232+
// expected-error@-1 {{contextual member reference to static property 'invalidProp' requires 'Self' constraint in the protocol extension}}
233233
// expected-error@-2 {{value of type 'Int' has no member 'other'}}
234234
test(.invalidMethod())
235-
// expected-error@-1 {{cannot infer contextual base in reference to member 'invalidMethod()'}}
235+
// expected-error@-1 {{contextual member reference to static method 'invalidMethod()' requires 'Self' constraint in the protocol extension}}
236236
test(.invalidMethod().other)
237-
// expected-error@-1 {{cannot infer contextual base in reference to member 'invalidMethod()'}}
237+
// expected-error@-1 {{contextual member reference to static method 'invalidMethod()' requires 'Self' constraint in the protocol extension}}
238238
// expected-error@-2 {{value of type 'Int' has no member 'other'}}
239239
test(.generic(42))
240-
// expected-error@-1 {{cannot infer contextual base in reference to member 'generic'}}
240+
// expected-error@-1 {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
241241
test(.generic(42).other)
242-
// expected-error@-1 {{cannot infer contextual base in reference to member 'generic'}}
242+
// expected-error@-1 {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
243243
// expected-error@-2 {{value of type 'Int' has no member 'other'}}
244-
test(.generic(S())) // expected-error {{cannot infer contextual base in reference to member 'generic'}}
245-
test(.generic(G<Int>())) // expected-error {{cannot infer contextual base in reference to member 'generic'}}
246-
test(.genericWithReqs([S()])) // expected-error {{cannot infer contextual base in reference to member 'genericWithReqs'}}
244+
test(.generic(S())) // expected-error {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
245+
test(.generic(G<Int>())) // expected-error {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
246+
test(.genericWithReqs([S()])) // expected-error {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
247247
test(.genericWithReqs([42]))
248-
// expected-error@-1 {{cannot infer contextual base in reference to member 'genericWithReqs'}}
248+
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
249249
test(.genericWithReqs(()))
250-
// expected-error@-1 {{cannot infer contextual base in reference to member 'genericWithReqs'}}
250+
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
251251

252252
test_combo(.doesntExist) // expected-error {{reference to member 'doesntExist' cannot be resolved without a contextual type}}
253253
test_combo(.doesnt.exist()) // expected-error {{reference to member 'doesnt' cannot be resolved without a contextual type}}
254254
test_combo(.invalidProp)
255-
// expected-error@-1 {{cannot infer contextual base in reference to member 'invalidProp'}}
255+
// expected-error@-1 {{contextual member reference to static property 'invalidProp' requires 'Self' constraint in the protocol extension}}
256256
test_combo(.invalidMethod())
257-
// expected-error@-1 {{cannot infer contextual base in reference to member 'invalidMethod()'}}
257+
// expected-error@-1 {{contextual member reference to static method 'invalidMethod()' requires 'Self' constraint in the protocol extension}}
258258
test_combo(.generic(42))
259-
// expected-error@-1 {{cannot infer contextual base in reference to member 'generic'}}
260-
test_combo(.generic(S())) // expected-error {{cannot infer contextual base in reference to member 'generic'}}
261-
test_combo(.generic(G<Int>())) // expected-error {{cannot infer contextual base in reference to member 'generic'}}
262-
test_combo(.genericWithReqs([S()])) // expected-error {{cannot infer contextual base in reference to member 'genericWithReqs'}}
259+
// expected-error@-1 {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
260+
test_combo(.generic(S())) // expected-error {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
261+
test_combo(.generic(G<Int>())) // expected-error {{contextual member reference to static method 'generic' requires 'Self' constraint in the protocol extension}}
262+
test_combo(.genericWithReqs([S()])) // expected-error {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
263263
test_combo(.genericWithReqs([42]))
264-
// expected-error@-1 {{cannot infer contextual base in reference to member 'genericWithReqs'}}
264+
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
265265
test_combo(.genericWithReqs(()))
266-
// expected-error@-1 {{cannot infer contextual base in reference to member 'genericWithReqs'}}
266+
// expected-error@-1 {{contextual member reference to static method 'genericWithReqs' requires 'Self' constraint in the protocol extension}}
267267

268268
protocol Z {
269269
associatedtype T = Int
@@ -297,5 +297,5 @@ extension TestWithAssoc where U == Int { // expected-note {{missing same-type re
297297

298298
func test_fixit_with_where_clause() {
299299
func test_assoc<T: TestWithAssoc>(_: T) {}
300-
test_assoc(.intVar) // expected-error {{cannot infer contextual base in reference to member 'intVar'}}
300+
test_assoc(.intVar) // expected-error {{contextual member reference to static property 'intVar' requires 'Self' constraint in the protocol extension}}
301301
}

test/expr/postfix/dot/optional_context_member.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protocol Horse {
8282
func rideAHorse(_ horse: Horse?) {}
8383

8484
rideAHorse(.palomino)
85-
// expected-error@-1 {{cannot infer contextual base in reference to member 'palomino'}}
85+
// expected-error@-1 {{static member 'palomino' cannot be used on protocol metatype 'Horse.Protocol'}}
8686

8787
// FIXME: This should work if the static member is part of a class though
8888
class Donkey {

0 commit comments

Comments
 (0)