@@ -1619,21 +1619,37 @@ bool AssignmentFailure::diagnoseAsError() {
1619
1619
// If there is a masked instance variable of the same type, emit a
1620
1620
// note to fixit prepend a 'self.'.
1621
1621
if (auto typeContext = DC->getInnermostTypeContext ()) {
1622
- UnqualifiedLookup lookup (VD->getFullName (), typeContext);
1623
- for (auto &result : lookup.Results ) {
1624
- const VarDecl *typeVar = dyn_cast<VarDecl>(result.getValueDecl ());
1625
- if (typeVar && typeVar != VD && typeVar->isSettable (DC) &&
1626
- typeVar->isSetterAccessibleFrom (DC) &&
1627
- typeVar->getType ()->isEqual (VD->getType ())) {
1628
- // But not in its own accessor.
1629
- auto AD =
1630
- dyn_cast_or_null<AccessorDecl>(DC->getInnermostMethodContext ());
1631
- if (!AD || AD->getStorage () != typeVar) {
1632
- emitDiagnostic (Loc, diag::masked_instance_variable,
1633
- typeContext->getSelfTypeInContext ())
1634
- .fixItInsert (Loc, " self." );
1635
- }
1636
- }
1622
+ SmallVector<ValueDecl *, 2 > results;
1623
+ DC->lookupQualified (typeContext->getSelfNominalTypeDecl (),
1624
+ VD->getFullName (),
1625
+ NL_QualifiedDefault | NL_RemoveNonVisible, results);
1626
+
1627
+ auto foundProperty = llvm::find_if (results, [&](ValueDecl *decl) {
1628
+ // We're looking for a settable property that is the same type as the
1629
+ // var we found.
1630
+ auto *var = dyn_cast<VarDecl>(decl);
1631
+ if (!var || var == VD)
1632
+ return false ;
1633
+
1634
+ if (!var->isSettable (DC) || !var->isSetterAccessibleFrom (DC))
1635
+ return false ;
1636
+
1637
+ if (!var->getType ()->isEqual (VD->getType ()))
1638
+ return false ;
1639
+
1640
+ // Don't suggest a property if we're in one of its accessors.
1641
+ auto *methodDC = DC->getInnermostMethodContext ();
1642
+ if (auto *AD = dyn_cast_or_null<AccessorDecl>(methodDC))
1643
+ if (AD->getStorage () == var)
1644
+ return false ;
1645
+
1646
+ return true ;
1647
+ });
1648
+
1649
+ if (foundProperty != results.end ()) {
1650
+ emitDiagnostic (Loc, diag::masked_instance_variable,
1651
+ typeContext->getSelfTypeInContext ())
1652
+ .fixItInsert (Loc, " self." );
1637
1653
}
1638
1654
}
1639
1655
0 commit comments