Skip to content

[Diagnostics] Resolves: SR-5324 Improved diagnostic when instance member of outer type is referenced from nested type #11609

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 5 commits into from
Sep 19, 2017
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
6 changes: 4 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ ERROR(could_not_use_type_member_on_protocol_metatype,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))
"instance member %1"
"%select{| of type %2}3 cannot be used on"
"%select{| instance of nested}3 type %0",
(Type, DeclName, Type, bool))
ERROR(could_not_use_member_on_existential,none,
"member %1 cannot be used on value of protocol type %0; use a generic"
" constraint instead",
Expand Down
40 changes: 30 additions & 10 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2698,7 +2698,7 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
instanceTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
return;
case MemberLookupResult::UR_InstanceMemberOnType:
case MemberLookupResult::UR_InstanceMemberOnType: {
// If the base is an implicit self type reference, and we're in a
// an initializer, then the user wrote something like:
//
Expand Down Expand Up @@ -2744,11 +2744,28 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
return;
}
}

diagnose(loc, diag::could_not_use_instance_member_on_type,
instanceTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());

// Check whether the instance member is declared on parent context and if so
// provide more specialized message.
auto memberTypeContext = member->getDeclContext()->getInnermostTypeContext();
auto currentTypeContext = CS.DC->getInnermostTypeContext();
if (memberTypeContext && currentTypeContext &&
memberTypeContext->getSemanticDepth() <
currentTypeContext->getSemanticDepth()) {
diagnose(loc, diag::could_not_use_instance_member_on_type,
currentTypeContext->getDeclaredInterfaceType(), memberName,
memberTypeContext->getDeclaredTypeOfContext(),
true)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
} else {
diagnose(loc, diag::could_not_use_instance_member_on_type,
instanceTy, memberName,
instanceTy,
false)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
}
return;
}

case MemberLookupResult::UR_TypeMemberOnInstance:
diagnoseTypeMemberOnInstanceLookup(baseObjTy, baseExpr,
Expand Down Expand Up @@ -5027,12 +5044,15 @@ diagnoseInstanceMethodAsCurriedMemberOnType(CalleeCandidateInfo &CCI,
}

// Otherwise, complain about use of instance value on type.
auto diagnostic = isa<TypeExpr>(baseExpr)
? diag::instance_member_use_on_type
: diag::could_not_use_instance_member_on_type;

TC.diagnose(UDE->getLoc(), diagnostic, instanceType, UDE->getName())
if (isa<TypeExpr>(baseExpr)) {
TC.diagnose(UDE->getLoc(), diag::instance_member_use_on_type,
instanceType, UDE->getName())
.highlight(baseExpr->getSourceRange());
} else {
TC.diagnose(UDE->getLoc(), diag::could_not_use_instance_member_on_type,
instanceType, UDE->getName(), instanceType, false)
.highlight(baseExpr->getSourceRange());
}
return true;
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,18 @@ func rdar33914444() {
_ = A.S(e: .e1)
// expected-error@-1 {{type 'A.R<A.S.E>' has no member 'e1'}}
}

// SR-5324: Better diagnostic when instance member of outer type is referenced from nested type

struct Outer {
var outer: Int

struct Inner {
var inner: Int

func sum() -> Int {
return inner + outer
// expected-error@-1 {{instance member 'outer' of type 'Outer' cannot be used on instance of nested type 'Outer.Inner'}}
}
}
}