Skip to content

[DiagnosticsQoI] Strike VarDecls in Pattern Binding Initializers From Overloads #27668

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 1 commit into from
Oct 14, 2019
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
15 changes: 14 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/AST/ProtocolConformance.h"
Expand Down Expand Up @@ -4864,7 +4865,19 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
// reasonable choice.
auto addChoice = [&](OverloadChoice candidate) {
auto decl = candidate.getDecl();


// In a pattern binding initializer, immediately reject all of its bound
// variables. These would otherwise allow circular references.
if (auto *PBI = dyn_cast<PatternBindingInitializer>(DC)) {
if (auto *VD = dyn_cast<VarDecl>(decl)) {
if (PBI->getBinding() == VD->getParentPatternBinding()) {
result.addUnviable(candidate,
MemberLookupResult::UR_InstanceMemberOnType);
return;
}
}
}

// If the result is invalid, skip it.
// FIXME(InterfaceTypeRequest): isInvalid() should be based on the interface type.
(void)decl->getInterfaceType();
Expand Down
7 changes: 5 additions & 2 deletions test/NameBinding/name_lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,15 @@ struct PatternBindingWithTwoVars1 { var x = 3, y = x }
// expected-error@-1 {{cannot use instance member 'x' within property initializer; property initializers run before 'self' is available}}

struct PatternBindingWithTwoVars2 { var x = y, y = 3 }
// expected-error@-1 {{type 'PatternBindingWithTwoVars2' has no member 'y'}}
// expected-error@-1 {{property 'y' references itself}}
// expected-error@-2 {{cannot use instance member 'y' within property initializer; property initializers run before 'self' is available}}

// This one should be accepted, but for now PatternBindingDecl validation
// circularity detection is not fine grained enough.
struct PatternBindingWithTwoVars3 { var x = y, y = x }
// expected-error@-1 {{type 'PatternBindingWithTwoVars3' has no member 'y'}}
// expected-error@-1 {{cannot use instance member 'x' within property initializer; property initializers run before 'self' is available}}
// expected-error@-2 {{cannot use instance member 'y' within property initializer; property initializers run before 'self' is available}}
// expected-error@-3 {{property 'y' references itself}}

// https://bugs.swift.org/browse/SR-9015
func sr9015() {
Expand Down
24 changes: 16 additions & 8 deletions test/decl/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -571,23 +571,23 @@ enum SR_10084_E_8 {
}

enum SR_10084_E_9 {
case A // expected-note {{found this candidate}} // expected-note {{'A' previously declared here}}
static let A: SR_10084_E_9 = .A // expected-note {{found this candidate}} // expected-error {{invalid redeclaration of 'A'}} // expected-error {{ambiguous use of 'A'}}
case A // expected-note {{'A' previously declared here}}
static let A: SR_10084_E_9 = .A // expected-error {{invalid redeclaration of 'A'}}
}

enum SR_10084_E_10 {
static let A: SR_10084_E_10 = .A // expected-note {{found this candidate}} // expected-note {{'A' previously declared here}} // expected-error {{ambiguous use of 'A'}}
case A // expected-note {{found this candidate}} // expected-error {{invalid redeclaration of 'A'}}
static let A: SR_10084_E_10 = .A // expected-note {{'A' previously declared here}}
case A // expected-error {{invalid redeclaration of 'A'}}
}

enum SR_10084_E_11 {
case A // expected-note {{found this candidate}} // expected-note {{'A' previously declared here}}
static var A: SR_10084_E_11 = .A // expected-note {{found this candidate}} // expected-error {{invalid redeclaration of 'A'}} // expected-error {{ambiguous use of 'A'}}
case A // expected-note {{'A' previously declared here}}
static var A: SR_10084_E_11 = .A // expected-error {{invalid redeclaration of 'A'}}
}

enum SR_10084_E_12 {
static var A: SR_10084_E_12 = .A // expected-note {{found this candidate}} // expected-note {{'A' previously declared here}} // expected-error {{ambiguous use of 'A'}}
case A // expected-note {{found this candidate}} // expected-error {{invalid redeclaration of 'A'}}
static var A: SR_10084_E_12 = .A // expected-note {{'A' previously declared here}}
case A // expected-error {{invalid redeclaration of 'A'}}
}

enum SR_10084_E_13 {
Expand All @@ -609,3 +609,11 @@ enum SR_10084_E_16 {
typealias Z = Int // expected-note {{'Z' previously declared here}}
case Z // expected-error {{invalid redeclaration of 'Z'}}
}

// N.B. Validating the pattern binding initializer for `raw` used to cause
// recursive validation of the VarDecl. Check that we don't regress now that
// this isn't the case.
public struct Cyclic {
static func pickMe(please: Bool) -> Int { return 42 }
public static let pickMe = Cyclic.pickMe(please: true)
}