From 49ba3814e82abf18c3f1493589d259b09f84e04d Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Thu, 26 Oct 2017 18:54:40 -0500 Subject: [PATCH 1/7] add error definition --- include/swift/AST/DiagnosticsSema.def | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index f18ce60cf44a3..72969a931372c 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -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 element %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)) From 04de483ac53cb679729592e17a3aaf841256b389 Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Thu, 26 Oct 2017 18:55:16 -0500 Subject: [PATCH 2/7] Check for DeclRefExpr --- lib/Sema/CSDiag.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Sema/CSDiag.cpp b/lib/Sema/CSDiag.cpp index bf8f627839cf9..ce23c6b4f62d1 100644 --- a/lib/Sema/CSDiag.cpp +++ b/lib/Sema/CSDiag.cpp @@ -1243,12 +1243,16 @@ diagnoseTypeMemberOnInstanceLookup(Type baseObjTy, return; } - if (isa(member)) + if (isa(member)) { Diag.emplace(diagnose(loc, diag::could_not_use_enum_element_on_instance, memberName)); - else + } else if (isa(member)) { + 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()); From f98c1e2c240d42f38264bc475cf35d41bfb46fc2 Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Thu, 26 Oct 2017 18:55:29 -0500 Subject: [PATCH 3/7] initial test --- test/Sema/diag_typealias.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/Sema/diag_typealias.swift b/test/Sema/diag_typealias.swift index db58b0a39eb03..2cbeb709bcb93 100644 --- a/test/Sema/diag_typealias.swift +++ b/test/Sema/diag_typealias.swift @@ -4,3 +4,11 @@ struct S {} typealias S = S // expected-error {{redundant type alias declaration}}{{1-17=}} + +struct HasStatic { + func foo() { + print(cvar) // expected-error {{static element cvar cannot be referenced as an instance member}} + } + + static let cvar = 123 +} \ No newline at end of file From 8d584571af592adc95ada51fdb60a4f2eeb0c3be Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Thu, 26 Oct 2017 19:01:52 -0500 Subject: [PATCH 4/7] Check if the base expression is a either a DeclRefExpr or a MemberRefExpr --- lib/Sema/CSDiag.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Sema/CSDiag.cpp b/lib/Sema/CSDiag.cpp index ce23c6b4f62d1..7c8d9c7af1b04 100644 --- a/lib/Sema/CSDiag.cpp +++ b/lib/Sema/CSDiag.cpp @@ -1246,7 +1246,7 @@ diagnoseTypeMemberOnInstanceLookup(Type baseObjTy, if (isa(member)) { Diag.emplace(diagnose(loc, diag::could_not_use_enum_element_on_instance, memberName)); - } else if (isa(member)) { + } else if (isa(baseExpr) || isa(baseExpr)) { Diag.emplace(diagnose(loc, diag::could_not_use_type_member_on_instance_without_name, memberName)); } else { From e5e3383e5d40290942cbc9f34dbfbe202ac2d9a8 Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Thu, 26 Oct 2017 19:17:34 -0500 Subject: [PATCH 5/7] Move tests locations --- test/Constraints/members.swift | 9 +++++++++ test/Sema/diag_typealias.swift | 8 -------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/test/Constraints/members.swift b/test/Constraints/members.swift index 50347d9de7f9d..17e662cf58e23 100644 --- a/test/Constraints/members.swift +++ b/test/Constraints/members.swift @@ -1,3 +1,4 @@ + // RUN: %target-typecheck-verify-swift -swift-version 4 //// @@ -464,3 +465,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 +} diff --git a/test/Sema/diag_typealias.swift b/test/Sema/diag_typealias.swift index 2cbeb709bcb93..db58b0a39eb03 100644 --- a/test/Sema/diag_typealias.swift +++ b/test/Sema/diag_typealias.swift @@ -4,11 +4,3 @@ struct S {} typealias S = S // expected-error {{redundant type alias declaration}}{{1-17=}} - -struct HasStatic { - func foo() { - print(cvar) // expected-error {{static element cvar cannot be referenced as an instance member}} - } - - static let cvar = 123 -} \ No newline at end of file From 0c8f0b197357dc7acb8b1d48455d0ea010b73c83 Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Thu, 26 Oct 2017 21:29:40 -0500 Subject: [PATCH 6/7] remove whitespace --- test/Constraints/members.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Constraints/members.swift b/test/Constraints/members.swift index 17e662cf58e23..a2a37ff302c88 100644 --- a/test/Constraints/members.swift +++ b/test/Constraints/members.swift @@ -1,4 +1,3 @@ - // RUN: %target-typecheck-verify-swift -swift-version 4 //// From ad67c8721b9daf40520048f9f3a06f9ffc9fc62b Mon Sep 17 00:00:00 2001 From: Oscar Swanros Date: Fri, 27 Oct 2017 00:35:53 -0500 Subject: [PATCH 7/7] Update desc --- include/swift/AST/DiagnosticsSema.def | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index 72969a931372c..d531f2cfda845 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -99,7 +99,7 @@ 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 element %0 cannot be referenced as an instance member", + "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",