Skip to content
Closed
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 @@ -98,6 +98,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_instance_without_name,none,
"static member %0 cannot be referenced as an instance member",
(DeclName))
ERROR(could_not_use_enum_element_on_instance,none,
"enum element %0 cannot be referenced as an instance member",
(DeclName))
Expand Down
8 changes: 6 additions & 2 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,12 +1243,16 @@ diagnoseTypeMemberOnInstanceLookup(Type baseObjTy,
return;
}

if (isa<EnumElementDecl>(member))
if (isa<EnumElementDecl>(member)) {
Diag.emplace(diagnose(loc, diag::could_not_use_enum_element_on_instance,
memberName));
else
} else if (isa<DeclRefExpr>(baseExpr) || isa<MemberRefExpr>(baseExpr)) {
Diag.emplace(diagnose(loc, diag::could_not_use_type_member_on_instance_without_name,
memberName));
} else {
Diag.emplace(diagnose(loc, diag::could_not_use_type_member_on_instance,
baseObjTy, memberName));
}

Diag->highlight(nameLoc.getSourceRange());

Expand Down
8 changes: 8 additions & 0 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,11 @@ struct Outer {
}
}
}

struct HasStaticSR6207 {
func foo() {
print(cvar) // expected-error {{static element cvar cannot be referenced as an instance member}}
}

static let cvar = 123
}