Skip to content

Commit 2d8bca0

Browse files
authored
Merge pull request #30679 from CodaFi/repetez-repartee
2 parents 1b08f3e + d7e9738 commit 2d8bca0

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

include/swift/AST/Decl.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,6 +2436,7 @@ class ValueDecl : public Decl {
24362436
friend class IsDynamicRequest;
24372437
friend class IsImplicitlyUnwrappedOptionalRequest;
24382438
friend class InterfaceTypeRequest;
2439+
friend class CheckRedeclarationRequest;
24392440
friend class Decl;
24402441
SourceLoc getLocFromSource() const { return NameLoc; }
24412442
protected:
@@ -2457,24 +2458,24 @@ class ValueDecl : public Decl {
24572458
Bits.ValueDecl.AlreadyInLookupTable = value;
24582459
}
24592460

2460-
public:
2461-
/// Return true if this protocol member is a protocol requirement.
2462-
///
2463-
/// Asserts if this is not a member of a protocol.
2464-
bool isProtocolRequirement() const;
2465-
24662461
/// Determine whether we have already checked whether this
24672462
/// declaration is a redeclaration.
2468-
bool alreadyCheckedRedeclaration() const {
2463+
bool alreadyCheckedRedeclaration() const {
24692464
return Bits.ValueDecl.CheckedRedeclaration;
24702465
}
24712466

24722467
/// Set whether we have already checked this declaration as a
24732468
/// redeclaration.
2474-
void setCheckedRedeclaration(bool checked) {
2475-
Bits.ValueDecl.CheckedRedeclaration = checked;
2469+
void setCheckedRedeclaration() {
2470+
Bits.ValueDecl.CheckedRedeclaration = true;
24762471
}
24772472

2473+
public:
2474+
/// Return true if this protocol member is a protocol requirement.
2475+
///
2476+
/// Asserts if this is not a member of a protocol.
2477+
bool isProtocolRequirement() const;
2478+
24782479
void setUserAccessible(bool Accessible) {
24792480
Bits.ValueDecl.IsUserAccessible = Accessible;
24802481
}

include/swift/AST/TypeCheckRequests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,27 @@ class LookupAllConformancesInContextRequest :
22092209
evaluate(Evaluator &evaluator, const DeclContext *DC) const;
22102210
};
22112211

2212+
class CheckRedeclarationRequest :
2213+
public SimpleRequest<CheckRedeclarationRequest,
2214+
evaluator::SideEffect (ValueDecl *),
2215+
CacheKind::SeparatelyCached> {
2216+
public:
2217+
using SimpleRequest::SimpleRequest;
2218+
2219+
private:
2220+
friend SimpleRequest;
2221+
2222+
// Evaluation.
2223+
evaluator::SideEffect
2224+
evaluate(Evaluator &evaluator, ValueDecl *VD) const;
2225+
2226+
public:
2227+
// Separate caching.
2228+
bool isCached() const { return true; }
2229+
Optional<evaluator::SideEffect> getCachedResult() const;
2230+
void cacheResult(evaluator::SideEffect) const;
2231+
};
2232+
22122233
// Allow AnyValue to compare two Type values, even though Type doesn't
22132234
// support ==.
22142235
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ SWIFT_REQUEST(TypeChecker, AttachedPropertyWrappersRequest,
2929
NoLocationInfo)
3030
SWIFT_REQUEST(TypeChecker, CallerSideDefaultArgExprRequest,
3131
Expr *(DefaultArgumentExpr *), SeparatelyCached, NoLocationInfo)
32+
SWIFT_REQUEST(TypeChecker, CheckRedeclarationRequest,
33+
evaluator::SideEffect(ValueDecl *),
34+
Uncached, NoLocationInfo)
3235
SWIFT_REQUEST(TypeChecker, ClassAncestryFlagsRequest,
3336
AncestryFlags(ClassDecl *), Cached, NoLocationInfo)
3437
SWIFT_REQUEST(TypeChecker, CompareDeclSpecializationRequest,

lib/AST/TypeCheckRequests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,21 @@ void DifferentiableAttributeTypeCheckRequest::cacheResult(
12641264
attr->ParameterIndicesAndBit.setPointerAndInt(parameterIndices, true);
12651265
}
12661266

1267+
//----------------------------------------------------------------------------//
1268+
// CheckRedeclarationRequest computation.
1269+
//----------------------------------------------------------------------------//
1270+
1271+
Optional<evaluator::SideEffect>
1272+
CheckRedeclarationRequest::getCachedResult() const {
1273+
if (!std::get<0>(getStorage())->alreadyCheckedRedeclaration())
1274+
return None;
1275+
return std::make_tuple<>();
1276+
}
1277+
1278+
void CheckRedeclarationRequest::cacheResult(evaluator::SideEffect) const {
1279+
std::get<0>(getStorage())->setCheckedRedeclaration();
1280+
}
1281+
12671282
//----------------------------------------------------------------------------//
12681283
// TypeCheckSourceFileRequest computation.
12691284
//----------------------------------------------------------------------------//

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -479,24 +479,18 @@ static void checkRedeclaration(PrecedenceGroupDecl *group) {
479479
}
480480

481481
/// Check whether \c current is a redeclaration.
482-
static void checkRedeclaration(ASTContext &ctx, ValueDecl *current) {
483-
// If we've already checked this declaration, don't do it again.
484-
if (current->alreadyCheckedRedeclaration())
485-
return;
486-
487-
// Make sure we don't do this checking again.
488-
current->setCheckedRedeclaration(true);
489-
482+
evaluator::SideEffect
483+
CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
490484
// Ignore invalid and anonymous declarations.
491485
if (current->isInvalid() || !current->hasName())
492-
return;
486+
return std::make_tuple<>();
493487

494488
// If this declaration isn't from a source file, don't check it.
495489
// FIXME: Should restrict this to the source file we care about.
496490
DeclContext *currentDC = current->getDeclContext();
497491
SourceFile *currentFile = currentDC->getParentSourceFile();
498492
if (!currentFile || currentDC->isLocalContext())
499-
return;
493+
return std::make_tuple<>();
500494

501495
ReferencedNameTracker *tracker = currentFile->getReferencedNameTracker();
502496
bool isCascading = (current->getFormalAccess() > AccessLevel::FilePrivate);
@@ -525,6 +519,7 @@ static void checkRedeclaration(ASTContext &ctx, ValueDecl *current) {
525519
OverloadSignature currentSig = current->getOverloadSignature();
526520
CanType currentSigType = current->getOverloadSignatureType();
527521
ModuleDecl *currentModule = current->getModuleContext();
522+
auto &ctx = current->getASTContext();
528523
for (auto other : otherDefinitions) {
529524
// Skip invalid declarations and ourselves.
530525
//
@@ -726,10 +721,11 @@ static void checkRedeclaration(ASTContext &ctx, ValueDecl *current) {
726721
// set this at the beginning of the function, but we might have swapped
727722
// the decls for diagnostics; so ensure we also set this for the actual
728723
// decl we diagnosed on.
729-
current->setCheckedRedeclaration(true);
724+
current->setCheckedRedeclaration();
730725
break;
731726
}
732727
}
728+
return std::make_tuple<>();
733729
}
734730

735731
static Optional<unsigned>
@@ -1250,11 +1246,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
12501246
if (auto VD = dyn_cast<ValueDecl>(decl)) {
12511247
auto &Context = getASTContext();
12521248
TypeChecker::checkForForbiddenPrefix(Context, VD->getBaseName());
1253-
1254-
checkRedeclaration(Context, VD);
12551249

12561250
// Force some requests, which can produce diagnostics.
12571251

1252+
// Check redeclaration.
1253+
(void) evaluateOrDefault(decl->getASTContext().evaluator,
1254+
CheckRedeclarationRequest{VD}, {});
1255+
12581256
// Compute access level.
12591257
(void) VD->getFormalAccess();
12601258

0 commit comments

Comments
 (0)