Skip to content

Commit c01799c

Browse files
committed
[AST] Prefer available member in lookupVisibleDecls()
'init?()' and 'init()' are considerd conflicting. But user can declare both if only one of them is available. rdar://problem/47408946
1 parent d58c072 commit c01799c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,10 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
794794
OtherSignature, OtherSignatureType,
795795
/*wouldConflictInSwift5*/nullptr,
796796
/*skipProtocolExtensionCheck*/true)) {
797-
if (VD->getFormalAccess() > OtherVD->getFormalAccess()) {
797+
if (VD->getFormalAccess() > OtherVD->getFormalAccess() ||
798+
//Prefer available one.
799+
(!AvailableAttr::isUnavailable(VD) &&
800+
AvailableAttr::isUnavailable(OtherVD))) {
798801
PossiblyConflicting.erase(I);
799802
PossiblyConflicting.insert(VD);
800803

test/IDE/complete_constructor.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CLOSURE_IN_INIT_3 | %FileCheck %s -check-prefix=CLOSURE_IN_INIT_1
4848
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CLOSURE_IN_INIT_4 | %FileCheck %s -check-prefix=CLOSURE_IN_INIT_1
4949

50+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABLE_1 | %FileCheck %s -check-prefix=AVAILABLE_1
51+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=AVAILABLE_2 | %FileCheck %s -check-prefix=AVAILABLE_2
52+
5053
func freeFunc() {}
5154

5255
//===---
@@ -332,3 +335,33 @@ struct ClosureInInit1 {
332335
S(#^CLOSURE_IN_INIT_4^#
333336
}()
334337
}
338+
339+
public class AvailableTest {
340+
341+
@available(swift, obsoleted: 4)
342+
init(opt: Int) { }
343+
344+
@available(swift, introduced: 4)
345+
init?(opt: Int) { }
346+
347+
init(normal1: Int) { }
348+
init(normal2: Int) { }
349+
350+
351+
}
352+
func testAvailable() {
353+
let _ = AvailableTest(#^AVAILABLE_1^#
354+
// AVAILABLE_1: Begin completions, 3 items
355+
// AVAILABLE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#opt: Int#}[')'][#AvailableTest?#]; name=opt: Int
356+
// AVAILABLE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#normal1: Int#}[')'][#AvailableTest#]; name=normal1: Int
357+
// AVAILABLE_1-DAG: Decl[Constructor]/CurrNominal: ['(']{#normal2: Int#}[')'][#AvailableTest#]; name=normal2: Int
358+
// AVAILABLE_1: End completions
359+
360+
let _ = AvailableTest.init(#^AVAILABLE_2^#
361+
// AVAILABLE_2: Begin completions, 3 items
362+
// AVAILABLE_2-DAG: Pattern/CurrModule: ['(']{#opt: Int#}[')'][#AvailableTest?#]; name=opt: Int
363+
// AVAILABLE_2-DAG: Pattern/CurrModule: ['(']{#normal1: Int#}[')'][#AvailableTest#]; name=normal1: Int
364+
// AVAILABLE_2-DAG: Pattern/CurrModule: ['(']{#normal2: Int#}[')'][#AvailableTest#]; name=normal2: Int
365+
// AVAILABLE_2: End completions
366+
367+
}

0 commit comments

Comments
 (0)