Skip to content

Commit 230dfcc

Browse files
authored
Merge pull request #64490 from xedin/rdar-102412006
[CSFix] Diagnose an invalid member reference in ambiguous contexts
2 parents c60b486 + d7a41c2 commit 230dfcc

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

include/swift/Sema/CSFix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,8 @@ class AllowInvalidMemberRef : public ConstraintFix {
14771477
ValueDecl *getMember() const { return Member; }
14781478

14791479
DeclNameRef getMemberName() const { return Name; }
1480+
1481+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;
14801482
};
14811483

14821484
class AllowMemberRefOnExistential final : public AllowInvalidMemberRef {

lib/Sema/CSFix.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,21 @@ bool AllowMemberRefOnExistential::diagnose(const Solution &solution,
947947
return failure.diagnose(asNote);
948948
}
949949

950+
bool AllowInvalidMemberRef::diagnoseForAmbiguity(
951+
CommonFixesArray commonFixes) const {
952+
auto *primaryFix =
953+
static_cast<const AllowInvalidMemberRef *>(commonFixes.front().second);
954+
955+
Type baseTy = primaryFix->getBaseType();
956+
for (const auto &entry : commonFixes) {
957+
auto *memberFix = static_cast<const AllowInvalidMemberRef *>(entry.second);
958+
if (!baseTy->isEqual(memberFix->getBaseType()))
959+
return false;
960+
}
961+
962+
return diagnose(*commonFixes.front().first);
963+
}
964+
950965
bool AllowTypeOrInstanceMember::diagnose(const Solution &solution,
951966
bool asNote) const {
952967
AllowTypeOrInstanceMemberFailure failure(solution, getBaseType(), getMember(),

test/Constraints/dynamic_lookup.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,18 @@ func testAnyObjectConstruction(_ x: AnyObject) {
471471
// FIXME: This should also be rejected.
472472
_ = type(of: x).init()
473473
}
474+
475+
// rdar://102412006 - failed to produce a diagnostic for invalid member ref
476+
class AmbiguityA : NSObject {
477+
@objc class var testProp: A { get { A() } }
478+
}
479+
480+
481+
class AmbuguityB : NSObject {
482+
@objc class var testProp: B { get { B() } }
483+
}
484+
485+
do {
486+
func test(_: AnyObject?) {}
487+
test(.testProp) // expected-error {{static member 'testProp' cannot be used on protocol metatype '(any AnyObject).Type'}}
488+
}

0 commit comments

Comments
 (0)