Skip to content

AST: Restore unqualified lookup quirk for Swift 3 mode #6777

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
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
28 changes: 26 additions & 2 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,13 +649,16 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
ValueDecl *MetaBaseDecl = nullptr;
GenericParamList *GenericParams = nullptr;
Type ExtendedType;

bool isTypeLookup = false;

// If this declcontext is an initializer for a static property, then we're
// implicitly doing a static lookup into the parent declcontext.
if (auto *PBI = dyn_cast<PatternBindingInitializer>(DC))
if (!DC->getParent()->isModuleScopeContext()) {
if (PBI->getBinding())
if (auto *PBD = PBI->getBinding()) {
isTypeLookup = PBD->isStatic();
DC = DC->getParent();
}
}

if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
Expand Down Expand Up @@ -694,6 +697,10 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
->getAsNominalTypeOrNominalTypeExtensionContext();
DC = DC->getParent();

if (auto *FD = dyn_cast<FuncDecl>(AFD))
if (FD->isStatic())
isTypeLookup = true;

// If we're not in the body of the function, the base declaration
// is the nominal type, not 'self'.
if (Loc.isValid() &&
Expand Down Expand Up @@ -780,6 +787,23 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
DC->lookupQualified(ExtendedType, Name, options, TypeResolver, Lookup);
bool FoundAny = false;
for (auto Result : Lookup) {
// In Swift 3 mode, unqualified lookup skips static methods when
// performing lookup from instance context.
//
// We don't want to carry this forward to Swift 4, since it makes
// for poor diagnostics.
//
// Also, it was quite a special case and not as general as it
// should be -- it didn't apply to properties or subscripts, and
// the opposite case where we're in static context and an instance
// member shadows the module member wasn't handled either.
if (Ctx.isSwiftVersion3() &&
!isTypeLookup &&
isa<FuncDecl>(Result) &&
cast<FuncDecl>(Result)->isStatic()) {
continue;
}

// Classify this declaration.
FoundAny = true;

Expand Down
11 changes: 11 additions & 0 deletions test/Compatibility/members.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-typecheck-verify-swift -swift-version 3

struct X {
func f1(_ i: Int) { }
mutating func f1(_ f: Float) { }
}

func g0(_: (inout X) -> (Float) -> ()) {}

// This becomes an error in Swift 4 mode -- probably a bug
g0(X.f1)
21 changes: 21 additions & 0 deletions test/Compatibility/unqualified_lookup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-typecheck-verify-swift -swift-version 3

// Stupid Swift 3 unqualified lookup quirk

func f3(_ x: Int, _ y: Int, z: Int) { } // expected-note{{did you mean 'f3'?}}

struct S0 {
func testS0() {
_ = f3(_:y:z:) // expected-error{{use of unresolved identifier 'f3(_:y:z:)'}}
}

static func f3(_ x: Int, y: Int, z: Int) -> S0 { return S0() }
}

extension Float {
func isClose(to: Float, epiValue: Float = 1e-5) -> Bool {
// Float.abs() and Swift.abs() are both visible here, but
// Swift 3 drops 'Float.abs()'.
return abs(self - to) < epiValue
}
}
12 changes: 7 additions & 5 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// RUN: %target-typecheck-verify-swift

import Swift
// RUN: %target-typecheck-verify-swift -swift-version 4

////
// Members of structs
Expand All @@ -9,9 +7,9 @@ import Swift
struct X {
func f0(_ i: Int) -> X { }

func f1(_ i: Int) { }
func f1(_ i: Int) { } // expected-note {{found this candidate}}

mutating func f1(_ f: Float) { }
mutating func f1(_ f: Float) { } // expected-note {{found this candidate}}

func f2<T>(_ x: T) -> T { }
}
Expand All @@ -29,7 +27,11 @@ func g0(_: (inout X) -> (Float) -> ()) {}

_ = x.f0(i)
x.f0(i).f1(i)

// FIXME: Is this a bug in Swift 4 mode?
g0(X.f1)
// expected-error@-1 {{ambiguous reference to member 'f1'}}

_ = x.f0(x.f2(1))
_ = x.f0(1).f2(i)
_ = yf.f0(1)
Expand Down
2 changes: 1 addition & 1 deletion test/expr/primary/unqualified_name.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -swift-version 4

func f0(_ x: Int, y: Int, z: Int) { }
func f1(_ x: Int, while: Int) { }
Expand Down
2 changes: 1 addition & 1 deletion test/expr/unary/selector/property.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -disable-objc-attr-requires-foundation-module -typecheck -primary-file %s %S/Inputs/property_helper.swift -verify
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -disable-objc-attr-requires-foundation-module -typecheck -primary-file %s %S/Inputs/property_helper.swift -verify -swift-version 4
import ObjectiveC

// REQUIRES: objc_interop
Expand Down