Skip to content

Commit 5b304bc

Browse files
committed
Merge branch 'release/5.8' into yonihemi/fix-5.8-merge-20230202
2 parents fd8d115 + b5c3c76 commit 5b304bc

35 files changed

+1169
-763
lines changed

include/swift/IDE/Utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ struct ResolvedCursorInfo {
161161
/// that names both the newly declared variable and the referenced variable
162162
/// by the same identifier in the source text. This includes shorthand
163163
/// closure captures (`[foo]`) and shorthand if captures
164-
/// (`if let foo {`).
164+
/// (`if let foo {`). Ordered from innermost to outermost shadows.
165+
///
165166
/// Decls that are shadowed using shorthand syntax should be reported as
166167
/// additional cursor info results.
167168
SmallVector<ValueDecl *> ShorthandShadowedDecls;

include/swift/SIL/MemAccessUtils.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,16 +1909,14 @@ class AccessUseDefChainCloner
19091909
/// Clone all projections and casts on the access use-def chain until the
19101910
/// checkBase predicate returns a valid base.
19111911
///
1912-
/// Returns the cloned value equivalent to \p addr.
1913-
///
19141912
/// This will not clone ref_element_addr or ref_tail_addr because those aren't
19151913
/// part of the access chain.
19161914
///
19171915
/// CheckBase is a unary predicate that takes the next source address and either
19181916
/// returns a valid SILValue to use as the base of the cloned access path, or an
19191917
/// invalid SILValue to continue cloning.
19201918
///
1921-
/// CheckBase must return a valid SILValue before attempting to clone the
1919+
/// CheckBase must return a valid SILValue either before attempting to clone the
19221920
/// access base. The most basic valid predicate is:
19231921
///
19241922
/// auto checkBase = [&](SILValue srcAddr) {
@@ -1933,8 +1931,6 @@ SILValue cloneUseDefChain(SILValue addr, SILInstruction *insertionPoint,
19331931

19341932
/// Analog to cloneUseDefChain to check validity. begin_borrow and
19351933
/// mark_dependence currently cannot be cloned.
1936-
///
1937-
/// Returns the cloned value equivalent to \p addr.
19381934
template <typename CheckBase>
19391935
bool canCloneUseDefChain(SILValue addr, CheckBase checkBase) {
19401936
return AccessUseDefChainCloner<CheckBase>(checkBase, nullptr)

include/swift/Sema/CSFix.h

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -587,21 +587,38 @@ class RelabelArguments final
587587
}
588588
};
589589

590+
class RequirementFix : public ConstraintFix {
591+
protected:
592+
Type LHS;
593+
Type RHS;
594+
595+
RequirementFix(ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
596+
ConstraintLocator *locator)
597+
: ConstraintFix(cs, kind, locator), LHS(lhs), RHS(rhs) {}
598+
599+
public:
600+
std::string getName() const override = 0;
601+
602+
Type lhsType() const { return LHS; }
603+
Type rhsType() const { return RHS; }
604+
605+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;
606+
607+
bool diagnose(const Solution &solution,
608+
bool asNote = false) const override = 0;
609+
};
610+
590611
/// Add a new conformance to the type to satisfy a requirement.
591-
class MissingConformance final : public ConstraintFix {
612+
class MissingConformance final : public RequirementFix {
592613
// Determines whether given protocol type comes from the context e.g.
593614
// assignment destination or argument comparison.
594615
bool IsContextual;
595616

596-
Type NonConformingType;
597-
// This could either be a protocol or protocol composition.
598-
Type ProtocolType;
599-
600617
MissingConformance(ConstraintSystem &cs, bool isContextual, Type type,
601618
Type protocolType, ConstraintLocator *locator)
602-
: ConstraintFix(cs, FixKind::AddConformance, locator),
603-
IsContextual(isContextual), NonConformingType(type),
604-
ProtocolType(protocolType) {}
619+
: RequirementFix(cs, FixKind::AddConformance, type, protocolType,
620+
locator),
621+
IsContextual(isContextual) {}
605622

606623
public:
607624
std::string getName() const override {
@@ -610,8 +627,6 @@ class MissingConformance final : public ConstraintFix {
610627

611628
bool diagnose(const Solution &solution, bool asNote = false) const override;
612629

613-
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;
614-
615630
static MissingConformance *forRequirement(ConstraintSystem &cs, Type type,
616631
Type protocolType,
617632
ConstraintLocator *locator);
@@ -620,9 +635,9 @@ class MissingConformance final : public ConstraintFix {
620635
Type protocolType,
621636
ConstraintLocator *locator);
622637

623-
Type getNonConformingType() { return NonConformingType; }
638+
Type getNonConformingType() const { return LHS; }
624639

625-
Type getProtocolType() { return ProtocolType; }
640+
Type getProtocolType() const { return RHS; }
626641

627642
bool isEqual(const ConstraintFix *other) const;
628643

@@ -633,13 +648,11 @@ class MissingConformance final : public ConstraintFix {
633648

634649
/// Skip same-type generic requirement constraint,
635650
/// and assume that types are equal.
636-
class SkipSameTypeRequirement final : public ConstraintFix {
637-
Type LHS, RHS;
638-
651+
class SkipSameTypeRequirement final : public RequirementFix {
639652
SkipSameTypeRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
640653
ConstraintLocator *locator)
641-
: ConstraintFix(cs, FixKind::SkipSameTypeRequirement, locator), LHS(lhs),
642-
RHS(rhs) {}
654+
: RequirementFix(cs, FixKind::SkipSameTypeRequirement, lhs, rhs,
655+
locator) {}
643656

644657
public:
645658
std::string getName() const override {
@@ -648,9 +661,6 @@ class SkipSameTypeRequirement final : public ConstraintFix {
648661

649662
bool diagnose(const Solution &solution, bool asNote = false) const override;
650663

651-
Type lhsType() { return LHS; }
652-
Type rhsType() { return RHS; }
653-
654664
static SkipSameTypeRequirement *create(ConstraintSystem &cs, Type lhs,
655665
Type rhs, ConstraintLocator *locator);
656666

@@ -661,13 +671,11 @@ class SkipSameTypeRequirement final : public ConstraintFix {
661671

662672
/// Skip same-shape generic requirement constraint,
663673
/// and assume that types are equal.
664-
class SkipSameShapeRequirement final : public ConstraintFix {
665-
Type LHS, RHS;
666-
674+
class SkipSameShapeRequirement final : public RequirementFix {
667675
SkipSameShapeRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
668676
ConstraintLocator *locator)
669-
: ConstraintFix(cs, FixKind::SkipSameShapeRequirement, locator), LHS(lhs),
670-
RHS(rhs) {}
677+
: RequirementFix(cs, FixKind::SkipSameShapeRequirement, lhs, rhs,
678+
locator) {}
671679

672680
public:
673681
std::string getName() const override {
@@ -676,9 +684,6 @@ class SkipSameShapeRequirement final : public ConstraintFix {
676684

677685
bool diagnose(const Solution &solution, bool asNote = false) const override;
678686

679-
Type lhsType() { return LHS; }
680-
Type rhsType() { return RHS; }
681-
682687
static SkipSameShapeRequirement *create(ConstraintSystem &cs, Type lhs,
683688
Type rhs, ConstraintLocator *locator);
684689

@@ -689,13 +694,11 @@ class SkipSameShapeRequirement final : public ConstraintFix {
689694

690695
/// Skip 'superclass' generic requirement constraint,
691696
/// and assume that types are equal.
692-
class SkipSuperclassRequirement final : public ConstraintFix {
693-
Type LHS, RHS;
694-
697+
class SkipSuperclassRequirement final : public RequirementFix {
695698
SkipSuperclassRequirement(ConstraintSystem &cs, Type lhs, Type rhs,
696699
ConstraintLocator *locator)
697-
: ConstraintFix(cs, FixKind::SkipSuperclassRequirement, locator),
698-
LHS(lhs), RHS(rhs) {}
700+
: RequirementFix(cs, FixKind::SkipSuperclassRequirement, lhs, rhs,
701+
locator) {}
699702

700703
public:
701704
std::string getName() const override {
@@ -1839,6 +1842,10 @@ class AllowInaccessibleMember final : public AllowInvalidMemberRef {
18391842

18401843
bool diagnose(const Solution &solution, bool asNote = false) const override;
18411844

1845+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
1846+
return diagnose(*commonFixes.front().first);
1847+
}
1848+
18421849
static AllowInaccessibleMember *create(ConstraintSystem &cs, Type baseType,
18431850
ValueDecl *member, DeclNameRef name,
18441851
ConstraintLocator *locator);

lib/IDE/CursorInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class NodeFinder : ASTWalker {
112112
std::unique_ptr<NodeFinderResult> takeResult() { return std::move(Result); }
113113

114114
/// Get the declarations that \p ShadowingDecl shadows using shorthand shadow
115-
/// syntax.
115+
/// syntax. Ordered from innermost to outermost shadows.
116116
SmallVector<ValueDecl *, 2>
117117
getShorthandShadowedDecls(ValueDecl *ShadowingDecl) {
118118
SmallVector<ValueDecl *, 2> Result;

lib/IDE/IDERequests.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class CursorInfoResolver : public SourceEntityWalker {
6464
llvm::SmallVector<Expr*, 8> ExprStack;
6565
/// If a decl shadows another decl using shorthand syntax (`[foo]` or
6666
/// `if let foo {`), this maps the re-declared variable to the one that is
67-
/// being shadowed.
67+
/// being shadowed. Ordered from innermost to outermost shadows.
68+
///
6869
/// The transitive closure of shorthand shadowed decls should be reported as
6970
/// additional results in cursor info.
7071
llvm::DenseMap<ValueDecl *, ValueDecl *> ShorthandShadowedDecls;
@@ -180,17 +181,14 @@ ResolvedCursorInfo CursorInfoResolver::resolve(SourceLoc Loc) {
180181
walk(SrcFile);
181182

182183
if (auto ValueRefInfo = dyn_cast<ResolvedValueRefCursorInfo>(&CursorInfo)) {
183-
if (!ValueRefInfo->isRef()) {
184-
SmallVector<ValueDecl *> ShadowedDecls;
185-
// If we have a definition, add any decls that it potentially shadows
186-
auto ShorthandShadowedDecl =
187-
ShorthandShadowedDecls[ValueRefInfo->getValueD()];
188-
while (ShorthandShadowedDecl) {
189-
ShadowedDecls.push_back(ShorthandShadowedDecl);
190-
ShorthandShadowedDecl = ShorthandShadowedDecls[ShorthandShadowedDecl];
191-
}
192-
ValueRefInfo->setShorthandShadowedDecls(ShadowedDecls);
184+
SmallVector<ValueDecl *> ShadowedDecls;
185+
auto ShorthandShadowedDecl =
186+
ShorthandShadowedDecls[ValueRefInfo->getValueD()];
187+
while (ShorthandShadowedDecl) {
188+
ShadowedDecls.push_back(ShorthandShadowedDecl);
189+
ShorthandShadowedDecl = ShorthandShadowedDecls[ShorthandShadowedDecl];
193190
}
191+
ValueRefInfo->setShorthandShadowedDecls(ShadowedDecls);
194192
}
195193

196194
return CursorInfo;

lib/IDE/IDETypeChecking.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,6 @@ swift::getShorthandShadows(CaptureListExpr *CaptureList, DeclContext *DC) {
942942
if (!ReferencedVar)
943943
continue;
944944

945-
assert(DeclaredVar->getName() == ReferencedVar->getName());
946-
947945
Result.emplace_back(std::make_pair(DeclaredVar, ReferencedVar));
948946
}
949947
return Result;
@@ -970,7 +968,6 @@ swift::getShorthandShadows(LabeledConditionalStmt *CondStmt, DeclContext *DC) {
970968
Cond.getPattern()->forEachVariable([&](VarDecl *DeclaredVar) {
971969
if (DeclaredVar->getLoc() != Init->getLoc())
972970
return;
973-
assert(DeclaredVar->getName() == ReferencedVar->getName());
974971
Result.emplace_back(std::make_pair(DeclaredVar, ReferencedVar));
975972
});
976973
}

lib/Index/Index.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
432432
SymbolInfo SymInfo;
433433
SymbolRoleSet Roles;
434434
SmallVector<IndexedWitness, 6> ExplicitWitnesses;
435-
SmallVector<SourceLoc, 6> RefsToSuppress;
436435
};
437436
SmallVector<Entity, 6> EntitiesStack;
438437
SmallVector<Expr *, 8> ExprStack;
@@ -449,6 +448,9 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
449448
StringScratchSpace stringStorage;
450449
ContainerTracker Containers;
451450

451+
// Already handled references that should be suppressed if found later.
452+
llvm::DenseSet<SourceLoc> RefsToSuppress;
453+
452454
// Contains a mapping for captures of the form [x], from the declared "x"
453455
// to the captured "x" in the enclosing scope. Also includes shorthand if
454456
// let bindings.
@@ -832,7 +834,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
832834
ReferenceMetaData Data) override {
833835
SourceLoc Loc = Range.getStart();
834836

835-
if (isRepressed(Loc) || Loc.isInvalid())
837+
if (Loc.isInvalid() || isSuppressed(Loc))
836838
return true;
837839

838840
// Dig back to the original captured variable
@@ -912,17 +914,13 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
912914
});
913915
}
914916

915-
void repressRefAtLoc(SourceLoc Loc) {
917+
void suppressRefAtLoc(SourceLoc Loc) {
916918
if (Loc.isInvalid()) return;
917-
assert(!EntitiesStack.empty());
918-
EntitiesStack.back().RefsToSuppress.push_back(Loc);
919+
RefsToSuppress.insert(Loc);
919920
}
920921

921-
bool isRepressed(SourceLoc Loc) const {
922-
if (EntitiesStack.empty() || Loc.isInvalid())
923-
return false;
924-
auto &Suppressed = EntitiesStack.back().RefsToSuppress;
925-
return std::find(Suppressed.begin(), Suppressed.end(), Loc) != Suppressed.end();
922+
bool isSuppressed(SourceLoc Loc) const {
923+
return Loc.isValid() && RefsToSuppress.contains(Loc);
926924
}
927925

928926
Expr *getContainingExpr(size_t index) const {
@@ -1253,7 +1251,7 @@ bool IndexSwiftASTWalker::startEntity(Decl *D, IndexSymbol &Info, bool IsRef) {
12531251
if (!handleWitnesses(D, explicitWitnesses))
12541252
return false;
12551253
}
1256-
EntitiesStack.push_back({D, Info.symInfo, Info.roles, std::move(explicitWitnesses), {}});
1254+
EntitiesStack.push_back({D, Info.symInfo, Info.roles, std::move(explicitWitnesses)});
12571255
return true;
12581256
}
12591257
}
@@ -1325,7 +1323,7 @@ bool IndexSwiftASTWalker::reportRelatedRef(ValueDecl *D, SourceLoc Loc, bool isI
13251323
Info.roles |= (unsigned)SymbolRole::Implicit;
13261324

13271325
// don't report this ref again when visitDeclReference reports it
1328-
repressRefAtLoc(Loc);
1326+
suppressRefAtLoc(Loc);
13291327

13301328
if (!reportRef(D, Loc, Info, None)) {
13311329
Cancelled = true;
@@ -1494,7 +1492,7 @@ bool IndexSwiftASTWalker::report(ValueDecl *D) {
14941492
// Suppress the reference if there is any (it is implicit and hence
14951493
// already skipped in the shorthand if let case, but explicit in the
14961494
// captured case).
1497-
repressRefAtLoc(loc);
1495+
suppressRefAtLoc(loc);
14981496

14991497
// Skip the definition of a shadowed decl
15001498
return true;

lib/Refactoring/Refactoring.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,11 @@ bool RefactoringActionLocalRename::performChange() {
912912
auto ValueRefCursorInfo = dyn_cast<ResolvedValueRefCursorInfo>(&CursorInfo);
913913
if (ValueRefCursorInfo && ValueRefCursorInfo->getValueD()) {
914914
ValueDecl *VD = ValueRefCursorInfo->typeOrValue();
915+
// The index always uses the outermost shadow for references
916+
if (!ValueRefCursorInfo->getShorthandShadowedDecls().empty()) {
917+
VD = ValueRefCursorInfo->getShorthandShadowedDecls().back();
918+
}
919+
915920
SmallVector<DeclContext *, 8> Scopes;
916921

917922
Optional<RenameRefInfo> RefInfo;

0 commit comments

Comments
 (0)