Skip to content

Sema: Suppress set accessor availability diagnostics in LoadExprs #74690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3524,13 +3524,19 @@ class ExprAvailabilityWalker : public ASTWalker {
return call;
}

/// Walks up to the first enclosing LoadExpr and returns it.
/// Walks up from a potential member reference to the first LoadExpr that would
/// make the member reference an r-value instead of an l-value.
const LoadExpr *getEnclosingLoadExpr() const {
assert(!ExprStack.empty() && "must be called while visiting an expression");
ArrayRef<const Expr *> stack = ExprStack;
stack = stack.drop_back();

for (auto expr : llvm::reverse(stack)) {
// Do not search past the first enclosing ApplyExpr. Any enclosing
// LoadExpr from this point only applies to the result of the call.
if (auto applyExpr = dyn_cast<ApplyExpr>(expr))
return nullptr;

if (auto loadExpr = dyn_cast<LoadExpr>(expr))
return loadExpr;
}
Expand Down Expand Up @@ -3631,12 +3637,7 @@ class ExprAvailabilityWalker : public ASTWalker {

/// Walk an inout expression, checking for availability.
void walkInOutExpr(InOutExpr *E) {
// If there is a LoadExpr in the stack, then this InOutExpr is not actually
// indicative of any mutation so the access context should just be Getter.
auto accessContext = getEnclosingLoadExpr() ? MemberAccessContext::Getter
: MemberAccessContext::InOut;

walkInContext(E, E->getSubExpr(), accessContext);
walkInContext(E, E->getSubExpr(), MemberAccessContext::InOut);
}

bool shouldWalkIntoClosure(AbstractClosureExpr *closure) const {
Expand All @@ -3657,7 +3658,6 @@ class ExprAvailabilityWalker : public ASTWalker {
return;
}


/// Walk the given expression in the member access context.
void walkInContext(Expr *baseExpr, Expr *E,
MemberAccessContext AccessContext) {
Expand Down Expand Up @@ -3693,18 +3693,22 @@ class ExprAvailabilityWalker : public ASTWalker {
break;

case MemberAccessContext::Setter:
diagAccessorAvailability(D->getOpaqueAccessor(AccessorKind::Set),
ReferenceRange, ReferenceDC, std::nullopt);
if (!getEnclosingLoadExpr()) {
diagAccessorAvailability(D->getOpaqueAccessor(AccessorKind::Set),
ReferenceRange, ReferenceDC, std::nullopt);
}
break;

case MemberAccessContext::InOut:
diagAccessorAvailability(D->getOpaqueAccessor(AccessorKind::Get),
ReferenceRange, ReferenceDC,
DeclAvailabilityFlag::ForInout);

diagAccessorAvailability(D->getOpaqueAccessor(AccessorKind::Set),
ReferenceRange, ReferenceDC,
DeclAvailabilityFlag::ForInout);
if (!getEnclosingLoadExpr()) {
diagAccessorAvailability(D->getOpaqueAccessor(AccessorKind::Set),
ReferenceRange, ReferenceDC,
DeclAvailabilityFlag::ForInout);
}
break;
}
}
Expand Down
Loading