Skip to content

Take the IUO-ness of a parameter declaration into account in ranking. #15490

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
Mar 25, 2018
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
26 changes: 26 additions & 0 deletions lib/Sema/CSRanking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,21 @@ static Type getAdjustedParamType(const AnyFunctionType::Param &param) {
return param.getType();
}

// Is a particular parameter of a function or subscript declaration
// declared to be an IUO?
static bool paramIsIUO(Decl *decl, int paramNum) {
if (auto *fn = dyn_cast<AbstractFunctionDecl>(decl)) {
auto *paramList =
fn->getParameterList(fn->getDeclContext()->isTypeContext());
auto *param = paramList->get(paramNum);
return param->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
}

auto *subscript = cast<SubscriptDecl>(decl);
auto *index = subscript->getIndices()->get(paramNum);
return index->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
}

/// \brief Determine whether the first declaration is as "specialized" as
/// the second declaration.
///
Expand Down Expand Up @@ -676,6 +691,17 @@ static bool isDeclAsSpecializedAs(TypeChecker &tc, DeclContext *dc,
continue;
}

// Emulate behavior from when IUO was a type, where IUOs
// were considered subtypes of plain optionals, but not
// vice-versa. This wouldn't normally happen, but there are
// cases where we can rename imported APIs so that we have a
// name collision, and where the parameter type(s) are the
// same except for details of the kind of optional declared.
auto param1IsIUO = paramIsIUO(decl1, param1);
auto param2IsIUO = paramIsIUO(decl2, param2);
if (param2IsIUO && !param1IsIUO)
return false;

if (!maybeAddSubtypeConstraint(params1[param1], params2[param2]))
return false;

Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/Inputs/disambiguate_iuo_param.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import Foundation;

@interface NSObject (NSMyTest)
- (BOOL)isEqualTo:(nullable id)object;
@end

@interface Obj : NSObject
- (BOOL)isEqualToObject:(id)value;
@end
11 changes: 11 additions & 0 deletions test/Constraints/disambiguate_iuo_param.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %empty-directory(%t)
// RUN: %build-clang-importer-objc-overlays
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -import-objc-header %S/Inputs/disambiguate_iuo_param.h %s -emit-ir | %FileCheck %s
// -module-name objc_ir -I %S/Inputs/custom-modules -emit-ir -g -o - -primary-file %s | %FileCheck %s

// REQUIRES: objc_interop

// CHECK: define {{.*}} @main
// CHECK: load {{.*}}, {{.*}}@"\01L_selector(isEqualToObject:)"
let c = Obj()
_ = c.isEqual(to: c)