Skip to content
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
1 change: 1 addition & 0 deletions include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ GROUP(StringInterpolationConformance, "string-interpolation-conformance")
GROUP(TemporaryPointers, "temporary-pointers")
GROUP(TrailingClosureMatching, "trailing-closure-matching")
GROUP(UnknownWarningGroup, "unknown-warning-group")
GROUP(WeakMutability, "weak-mutability")

#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
#include "swift/AST/DefineDiagnosticGroupsMacros.h"
8 changes: 8 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7304,6 +7304,14 @@ WARNING(variable_tuple_elt_never_mutated, none,
"variable %0 was never mutated; "
"consider changing the pattern to 'case (..., let %1, ...)'",
(Identifier, StringRef))
GROUPED_WARNING(weak_variable_never_mutated, WeakMutability, none,
"weak variable %0 was never mutated; "
"consider %select{removing 'var' to make it|changing to 'let'}1 constant",
(Identifier, bool))
GROUPED_WARNING(weak_variable_tuple_elt_never_mutated, WeakMutability, none,
"weak variable %0 was never mutated; "
"consider changing the pattern to 'case (..., let %1, ...)'",
(Identifier, StringRef))
WARNING(variable_never_read, none,
"variable %0 was written to, but never read",
(Identifier))
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ class alignas(1 << PatternAlignInBits) StmtConditionElement {
/// RHS of the self condition references a var defined in a capture list.
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
/// the self condition is a `LoadExpr`.
/// TODO: Remove `requireLoadExpr` after full-on of the ImmutableWeakCaptures
/// feature
bool rebindsSelf(ASTContext &Ctx, bool requiresCaptureListRef = false,
bool requireLoadExpr = false) const;

Expand Down Expand Up @@ -838,8 +840,6 @@ class LabeledConditionalStmt : public LabeledStmt {
/// or `let self = self` condition.
/// - If `requiresCaptureListRef` is `true`, additionally requires that the
/// RHS of the self condition references a var defined in a capture list.
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
/// the self condition is a `LoadExpr`.
bool rebindsSelf(ASTContext &Ctx, bool requiresCaptureListRef = false,
bool requireLoadExpr = false) const;

Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
MIGRATABLE_UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
MIGRATABLE_UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
MIGRATABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)
UPCOMING_FEATURE(ImmutableWeakCaptures, 481, 7)

// Optional language features / modes

Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1366,9 +1366,9 @@ CaptureListEntry CaptureListEntry::createParsed(
SourceRange ownershipRange, Identifier name, SourceLoc nameLoc,
SourceLoc equalLoc, Expr *initializer, DeclContext *DC) {

auto introducer =
(ownershipKind != ReferenceOwnership::Weak ? VarDecl::Introducer::Let
: VarDecl::Introducer::Var);
bool forceVar = ownershipKind == ReferenceOwnership::Weak &&
!Ctx.LangOpts.hasFeature(Feature::ImmutableWeakCaptures);
auto introducer = forceVar ? VarDecl::Introducer::Var : VarDecl::Introducer::Let;
auto *VD =
new (Ctx) VarDecl(/*isStatic==*/false, introducer, nameLoc, name, DC);

Expand Down
1 change: 1 addition & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ static bool usesFeatureAlwaysInheritActorContext(Decl *decl) {
static bool usesFeatureDefaultIsolationPerFile(Decl *D) {
return isa<UsingDecl>(D);
}
UNINTERESTING_FEATURE(ImmutableWeakCaptures)

// ----------------------------------------------------------------------------
// MARK: - FeatureSet
Expand Down
4 changes: 0 additions & 4 deletions lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@ void LabeledConditionalStmt::setCond(StmtCondition e) {
/// or `let self = self` condition.
/// - If `requiresCaptureListRef` is `true`, additionally requires that the
/// RHS of the self condition references a var defined in a capture list.
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
/// the self condition is a `LoadExpr`.
bool LabeledConditionalStmt::rebindsSelf(ASTContext &Ctx,
bool requiresCaptureListRef,
bool requireLoadExpr) const {
Expand All @@ -529,8 +527,6 @@ bool LabeledConditionalStmt::rebindsSelf(ASTContext &Ctx,
/// or `let self = self` condition.
/// - If `requiresCaptureListRef` is `true`, additionally requires that the
/// RHS of the self condition references a var defined in a capture list.
/// - If `requireLoadExpr` is `true`, additionally requires that the RHS of
/// the self condition is a `LoadExpr`.
bool StmtConditionElement::rebindsSelf(ASTContext &Ctx,
bool requiresCaptureListRef,
bool requireLoadExpr) const {
Expand Down
58 changes: 44 additions & 14 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,15 +1784,34 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
return false;
}

// Require `LoadExpr`s when validating the self binding.
// This lets us reject invalid examples like:
//
// let `self` = self ?? .somethingElse
// guard let self = self else { return }
// method() // <- implicit self is not allowed
//
return conditionalStmt->rebindsSelf(Ctx, /*requiresCaptureListRef*/ false,
/*requireLoadExpr*/ true);
if (Ctx.LangOpts.hasFeature(Feature::ImmutableWeakCaptures)) {
// Require that the RHS of the `let self = self` condition
// refers to a variable defined in a capture list.
// This lets us reject invalid examples like:
//
// var `self` = self ?? .somethingElse
// guard let self = self else { return }
// method() // <- implicit self is not allowed
//
// In 5.10, instead of this check, compiler was checking that RHS of the
// self binding is loaded from a mutable variable. This is incorrect, but
// before immutable weak captures compiler was trying to maintain this
// behavior in Swift 5 mode for source compatibility. With immutable weak
// captures this does not work anymore, because even in Swift 5 mode there
// is no `LoadExpr` to use.
//
return conditionalStmt->rebindsSelf(Ctx, /*requiresCaptureListRef*/ true);
} else {
// Require `LoadExpr`s when validating the self binding.
// This lets us reject invalid examples like:
//
// let `self` = self ?? .somethingElse
// guard let self = self else { return }
// method() // <- implicit self is not allowed
//
return conditionalStmt->rebindsSelf(Ctx, /*requiresCaptureListRef*/ false,
/*requireLoadExpr*/ true);
}
}

static bool
Expand Down Expand Up @@ -4043,9 +4062,12 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {

// If this variable has WeakStorageType, then it can be mutated in ways we
// don't know.
if (var->getInterfaceType()->is<WeakStorageType>())
if (var->getInterfaceType()->is<WeakStorageType>() &&
(access & RK_CaptureList) &&
!DC->getASTContext().LangOpts.hasFeature(
Feature::ImmutableWeakCaptures))
access |= RK_Written;

// Diagnose variables that were never used (other than their
// initialization).
//
Expand Down Expand Up @@ -4216,6 +4238,8 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
if (isUsedInInactive(var))
continue;

bool isWeak = var->getInterfaceType()->is<WeakStorageType>();

// If this is a parameter explicitly marked 'var', remove it.
if (FixItLoc.isInvalid()) {
bool suggestCaseLet = false;
Expand All @@ -4226,10 +4250,14 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
suggestCaseLet = isa<ForEachStmt>(stmt);
}
if (suggestCaseLet)
Diags.diagnose(var->getLoc(), diag::variable_tuple_elt_never_mutated,
Diags.diagnose(var->getLoc(),
isWeak ? diag::weak_variable_tuple_elt_never_mutated
: diag::variable_tuple_elt_never_mutated,
var->getName(), var->getNameStr());
else
Diags.diagnose(var->getLoc(), diag::variable_never_mutated,
Diags.diagnose(var->getLoc(),
isWeak ? diag::weak_variable_never_mutated
: diag::variable_never_mutated,
var->getName(), true);

}
Expand All @@ -4242,7 +4270,9 @@ VarDeclUsageChecker::~VarDeclUsageChecker() {
suggestLet = !isa<ForEachStmt>(stmt);
}

auto diag = Diags.diagnose(var->getLoc(), diag::variable_never_mutated,
auto diag = Diags.diagnose(var->getLoc(),
isWeak ? diag::weak_variable_never_mutated
: diag::variable_never_mutated,
var->getName(), suggestLet);

if (suggestLet)
Expand Down
5 changes: 0 additions & 5 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5265,11 +5265,6 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
case ReferenceOwnershipOptionality::Allowed:
break;
case ReferenceOwnershipOptionality::Required:
if (var->isLet()) {
var->diagnose(diag::invalid_ownership_is_let, ownershipKind);
attr->setInvalid();
}

if (!isOptional) {
attr->setInvalid();

Expand Down
Loading