Skip to content

Commit

Permalink
Strongly prefer available declarations to unavailable ones in type ch…
Browse files Browse the repository at this point in the history
…ecking.

Fixes rdar://problem/18847642, rdar://problem/16554496, and the
current 1_stdlib/Array.swift.

Swift SVN r25212
  • Loading branch information
DougGregor committed Feb 12, 2015
1 parent 26ee4c3 commit 0e74268
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
17 changes: 5 additions & 12 deletions lib/Sema/CSRanking.cpp
Expand Up @@ -37,7 +37,11 @@ void ConstraintSystem::increaseScore(ScoreKind kind) {
log.indent(solverState->depth * 2);
log << "(increasing score due to ";
switch (kind) {
case SK_Fix:
case SK_Unavailable:
log << "use of an unavailable declaration";
break;

case SK_Fix:
log << "attempting to fix the source";
break;

Expand Down Expand Up @@ -671,17 +675,6 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
}
}

// If one declaration is available and the other is not,
bool unavail1 = decl1->getAttrs().isUnavailable(cs.getASTContext());
bool unavail2 = decl2->getAttrs().isUnavailable(cs.getASTContext());
if (unavail1 != unavail2) {
if (unavail1)
++score2;
else
++score1;
continue;
}

// If both declarations come from Clang, and one is a type and the other
// is a function, prefer the function.
if (decl1->hasClangNode() &&
Expand Down
5 changes: 5 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Expand Up @@ -1311,6 +1311,11 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
refType = ImplicitlyUnwrappedOptionalType::get(refType->getRValueType());
}

// If the declaration is unavailable, note that in the score.
if (choice.getDecl()->getAttrs().isUnavailable(getASTContext())) {
increaseScore(SK_Unavailable);
}

break;
}

Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/ConstraintSystem.h
Expand Up @@ -705,6 +705,8 @@ struct SelectedOverload {
enum ScoreKind {
// These values are used as indices into a Score value.

/// A reference to an @unavailable declaration.
SK_Unavailable,
/// A fix needs to be applied to the source.
SK_Fix,
/// An implicit force of an implicitly unwrapped optional value.
Expand Down
9 changes: 9 additions & 0 deletions test/ClangModules/Inputs/custom-modules/ObjCParseExtras.h
Expand Up @@ -104,3 +104,12 @@ __weak id globalWeakVar;
@protocol FooDelegate <NSObject>
@property (nonatomic, assign, readonly, getter=isStarted) BOOL started;
@end

// rdar://problem/18847642
@interface NonNullDefaultInit
-(nonnull instancetype)init;
@end

@interface NonNullDefaultInitSub : NonNullDefaultInit
+ (null_unspecified instancetype)sub;
@end
7 changes: 7 additions & 0 deletions test/ClangModules/objc_init.swift
Expand Up @@ -144,3 +144,10 @@ class View: NSView {
var p = MyViewController.init()
}
}

// rdar://problem/19726164
class NonNullDefaultInitSubSub : NonNullDefaultInitSub {
func foo() {
var x: NonNullDefaultInitSubSub? = NonNullDefaultInitSubSub()
}
}
8 changes: 8 additions & 0 deletions test/Constraints/overload.swift
Expand Up @@ -106,3 +106,11 @@ struct HasX1aProperty {
write(prop) // no error, not ambiguous
}
}

// rdar://problem/16554496
@availability(*, unavailable)
func availTest(x: Int) {}
func availTest(x: Any) { println("this one") }
func doAvailTest(x: Int) {
availTest(x)
}

0 comments on commit 0e74268

Please sign in to comment.