Skip to content

[Sema] Improved error message for static functions on existential metatypes #2127

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

Merged
merged 2 commits into from
Apr 11, 2016
Merged
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 @@ -93,6 +93,9 @@ ERROR(could_not_use_type_member,none,
ERROR(could_not_use_type_member_on_instance,none,
"static member %1 cannot be used on instance of type %0",
(Type, DeclName))
ERROR(could_not_use_type_member_on_existential,none,
"static member %1 cannot be used on protocol metatype %0",
(Type, DeclName))
ERROR(could_not_use_instance_member_on_type,none,
"instance member %1 cannot be used on type %0",
(Type, DeclName))
Expand Down
17 changes: 14 additions & 3 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2251,10 +2251,21 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
instanceTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
return;

case MemberLookupResult::UR_TypeMemberOnInstance:
diagnose(loc, diag::could_not_use_type_member_on_instance,
baseObjTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
if (instanceTy->isExistentialType() && baseObjTy->is<AnyMetatypeType>()) {
// If the base of the lookup is an existential metatype, emit an
// error specific to that
diagnose(loc, diag::could_not_use_type_member_on_existential,
baseObjTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
} else {
// Otherwise the static member lookup was invalid because it was
// called on an instance
diagnose(loc, diag::could_not_use_type_member_on_instance,
baseObjTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
}
return;

case MemberLookupResult::UR_MutatingMemberOnRValue:
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -820,15 +820,15 @@ struct MemberLookupResult {
/// Argument labels don't match.
UR_LabelMismatch,

/// This uses a type like Self it its signature that cannot be used on an
/// This uses a type like Self in its signature that cannot be used on an
/// existential box.
UR_UnavailableInExistential,

/// This is an instance member being accessed through something of metatype
/// type.
UR_InstanceMemberOnType,

/// This is a static/class member being access through an instance.
/// This is a static/class member being accessed through an instance.
UR_TypeMemberOnInstance,

/// This is a mutating member, being used on an rvalue.
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ func staticExistential(_ p: P.Type, pp: P.Protocol) {
_ = p.mut // expected-error{{instance member 'mut' cannot be used on type 'P'}}

// Static member of metatype -- not allowed
_ = pp.tum // expected-error{{static member 'tum' cannot be used on instance of type 'P.Protocol'}}
_ = P.tum // expected-error{{static member 'tum' cannot be used on instance of type 'P.Protocol'}}
_ = pp.tum // expected-error{{static member 'tum' cannot be used on protocol metatype 'P.Protocol'}}
_ = P.tum // expected-error{{static member 'tum' cannot be used on protocol metatype 'P.Protocol'}}

// Static member of extension returning Self)
let _: () -> P = id(p.returnSelfStatic)
Expand Down
4 changes: 2 additions & 2 deletions test/expr/unary/selector/selector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func testSelector(_ c1: C1, p1: P1, obj: AnyObject) {
// Methods on a protocol.
_ = #selector(P1.method4)
_ = #selector(P1.method4(_:b:))
_ = #selector(P1.method5) // FIXME: expected-error{{static member 'method5' cannot be used on instance of type 'P1.Protocol'}}
_ = #selector(P1.method5(_:b:)) // FIXME: expected-error{{static member 'method5(_:b:)' cannot be used on instance of type 'P1.Protocol'}}
_ = #selector(P1.method5) // expected-error{{static member 'method5' cannot be used on protocol metatype 'P1.Protocol'}}
_ = #selector(P1.method5(_:b:)) // expected-error{{static member 'method5(_:b:)' cannot be used on protocol metatype 'P1.Protocol'}}
_ = #selector(p1.method4)
_ = #selector(p1.method4(_:b:))
_ = #selector(p1.dynamicType.method5)
Expand Down