Skip to content

Commit f676dde

Browse files
committed
[Typechecker] Use a 'is Self class-bound?' check instead
1 parent b254741 commit f676dde

File tree

3 files changed

+10
-33
lines changed

3 files changed

+10
-33
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5131,8 +5131,7 @@ class VarDecl : public AbstractStorageDecl {
51315131
/// If this is a simple 'let' constant, emit a note with a fixit indicating
51325132
/// that it can be rewritten to a 'var'. This is used in situations where the
51335133
/// compiler detects obvious attempts to mutate a constant.
5134-
void emitLetToVarNoteIfSimple(DeclContext *UseDC,
5135-
ValueDecl *Anchor = nullptr) const;
5134+
void emitLetToVarNoteIfSimple(DeclContext *UseDC) const;
51365135

51375136
/// Returns true if the name is the self identifier and is implicit.
51385137
bool isSelfParameter() const;

lib/AST/Decl.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5689,8 +5689,7 @@ ObjCSelector VarDecl::getDefaultObjCSetterSelector(ASTContext &ctx,
56895689
/// If this is a simple 'let' constant, emit a note with a fixit indicating
56905690
/// that it can be rewritten to a 'var'. This is used in situations where the
56915691
/// compiler detects obvious attempts to mutate a constant.
5692-
void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC,
5693-
ValueDecl *Anchor) const {
5692+
void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC) const {
56945693
// If it isn't a 'let', don't touch it.
56955694
if (!isLet()) return;
56965695

@@ -5709,23 +5708,11 @@ void VarDecl::emitLetToVarNoteIfSimple(DeclContext *UseDC,
57095708
if (AD->isGetter() && !AD->getAccessorKeywordLoc().isValid())
57105709
return;
57115710

5712-
// Do not suggest the fix-it if we have an implicitly nonmutating
5713-
// setter in a protocol extension and we're assigning to a mutating
5714-
// protocol requirement.
5715-
if (Anchor && Anchor->isProtocolRequirement() && isa<VarDecl>(Anchor)) {
5716-
auto requirementVar = cast<VarDecl>(Anchor);
5717-
auto innermostTyCtx = AD->getInnermostTypeContext();
5718-
bool isRequirementSetterMutating = requirementVar->isSetterMutating();
5719-
bool isProtoExtension =
5720-
innermostTyCtx
5721-
? innermostTyCtx->getExtendedProtocolDecl() != nullptr
5722-
: false;
5723-
bool isImplicitlyNonMutatingSetter =
5724-
AD->isSetter() && AD->isNonMutating() &&
5725-
!AD->getAttrs().hasAttribute<NonMutatingAttr>();
5726-
if (isRequirementSetterMutating && isProtoExtension &&
5727-
isImplicitlyNonMutatingSetter)
5728-
return;
5711+
// Do not suggest the fix-it if `Self` is a class type.
5712+
if (AD->getDeclContext()
5713+
->getSelfTypeInContext()
5714+
->isAnyClassReferenceType()) {
5715+
return;
57295716
}
57305717
}
57315718

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,18 +1468,9 @@ bool AssignmentFailure::diagnoseAsError() {
14681468
}
14691469
}
14701470

1471-
// If this is a simple variable marked with a 'let', emit a note to change
1472-
// it to 'var'.
1473-
//
1474-
// We also need a reference to the overload for the anchor, because it's
1475-
// possible that we're assigning to a mutating protocol property from an
1476-
// implicitly nonmutating setter in a protocol extension. In that case,
1477-
// we want to drop the fix-it to add 'mutating' as it's gonna re-trigger
1478-
// this error.
1479-
auto overload =
1480-
getConstraintSystem().findSelectedOverloadFor(getAnchor());
1481-
auto anchor = overload ? overload->Choice.getDeclOrNull() : nullptr;
1482-
VD->emitLetToVarNoteIfSimple(DC, anchor);
1471+
// If this is a simple variable marked with a 'let', emit a note to fixit
1472+
// hint it to 'var'.
1473+
VD->emitLetToVarNoteIfSimple(DC);
14831474
return true;
14841475
}
14851476

0 commit comments

Comments
 (0)