Skip to content

Commit 72ee213

Browse files
committed
Move "unsafe" diagnostics from errors to warnings
Warnings fit better with the approach we're going for, and can be escalated to errors by `-warnings-as-errors` for clients that need it.
1 parent c0c70eb commit 72ee213

14 files changed

+63
-71
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7969,24 +7969,24 @@ NOTE(sending_function_result_with_sending_param_note, none,
79697969
//------------------------------------------------------------------------------
79707970
ERROR(unsafe_attr_disabled,none,
79717971
"attribute requires '-enable-experimental-feature AllowUnsafeAttribute'", ())
7972-
ERROR(override_safe_withunsafe,none,
7972+
WARNING(override_safe_withunsafe,none,
79737973
"override of safe %0 with unsafe %0", (DescriptiveDeclKind))
7974-
ERROR(witness_unsafe,none,
7974+
WARNING(witness_unsafe,none,
79757975
"unsafe %0 %1 cannot satisfy safe requirement",
79767976
(DescriptiveDeclKind, DeclName))
7977-
ERROR(type_witness_unsafe,none,
7977+
WARNING(type_witness_unsafe,none,
79787978
"unsafe type %0 cannot satisfy safe associated type %1",
79797979
(Type, DeclName))
7980-
ERROR(unchecked_conformance_is_unsafe,none,
7980+
WARNING(unchecked_conformance_is_unsafe,none,
79817981
"@unchecked conformance involves unsafe code", ())
7982-
ERROR(unowned_unsafe_is_unsafe,none,
7982+
WARNING(unowned_unsafe_is_unsafe,none,
79837983
"unowned(unsafe) involves unsafe code", ())
7984-
ERROR(nonisolated_unsafe_is_unsafe,none,
7984+
WARNING(nonisolated_unsafe_is_unsafe,none,
79857985
"nonisolated(unsafe) involves unsafe code", ())
7986-
ERROR(reference_to_unsafe_decl,none,
7986+
WARNING(reference_to_unsafe_decl,none,
79877987
"%select{reference|call}0 to unsafe %kindbase1",
79887988
(bool, const ValueDecl *))
7989-
ERROR(reference_to_unsafe_typed_decl,none,
7989+
WARNING(reference_to_unsafe_typed_decl,none,
79907990
"%select{reference|call}0 to %kindbase1 involves unsafe type %2",
79917991
(bool, const ValueDecl *, Type))
79927992
NOTE(unsafe_decl_here,none,

include/swift/Basic/Features.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,8 @@ EXPERIMENTAL_FEATURE(TrailingComma, false)
405405
/// Allow the @unsafe attribute.
406406
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(AllowUnsafeAttribute, true)
407407

408-
/// Disallow use of unsafe code.
409-
EXPERIMENTAL_FEATURE(DisallowUnsafe, true)
408+
/// Warn on use of unsafe constructs.
409+
EXPERIMENTAL_FEATURE(WarnUnsafe, true)
410410

411411
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
412412
#undef EXPERIMENTAL_FEATURE

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static bool usesFeatureAllowUnsafeAttribute(Decl *decl) {
230230
return decl->getAttrs().hasAttribute<UnsafeAttr>();
231231
}
232232

233-
UNINTERESTING_FEATURE(DisallowUnsafe)
233+
UNINTERESTING_FEATURE(WarnUnsafe)
234234

235235
// ----------------------------------------------------------------------------
236236
// MARK: - FeatureSet

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static void recordTypeWitness(NormalProtocolConformance *conformance,
346346
}
347347

348348
// If we're disallowing unsafe code, check for an unsafe type witness.
349-
if (ctx.LangOpts.hasFeature(Feature::DisallowUnsafe) &&
349+
if (ctx.LangOpts.hasFeature(Feature::WarnUnsafe) &&
350350
!assocType->isUnsafe() && type->isUnsafe()) {
351351
SourceLoc loc = typeDecl->getLoc();
352352
if (loc.isInvalid())

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4897,7 +4897,7 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
48974897

48984898
// unowned(unsafe) is unsafe (duh).
48994899
if (ownershipKind == ReferenceOwnership::Unmanaged &&
4900-
ctx.LangOpts.hasFeature(Feature::DisallowUnsafe)) {
4900+
ctx.LangOpts.hasFeature(Feature::WarnUnsafe)) {
49014901
Diags.diagnose(attr->getLocation(), diag::unowned_unsafe_is_unsafe);
49024902
}
49034903

@@ -7097,7 +7097,7 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
70977097

70987098
// nonisolated(unsafe) is unsafe, but only under strict concurrency.
70997099
if (attr->isUnsafe() &&
7100-
Ctx.LangOpts.hasFeature(Feature::DisallowUnsafe) &&
7100+
Ctx.LangOpts.hasFeature(Feature::WarnUnsafe) &&
71017101
Ctx.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete)
71027102
Ctx.Diags.diagnose(attr->getLocation(), diag::nonisolated_unsafe_is_unsafe);
71037103

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3877,21 +3877,20 @@ bool ExprAvailabilityWalker::diagnoseDeclRefAvailability(
38773877
// If the declaration itself is "safe" but we don't disallow unsafe uses,
38783878
// check whether it traffics in unsafe types.
38793879
ASTContext &ctx = D->getASTContext();
3880-
if (ctx.LangOpts.hasFeature(Feature::DisallowUnsafe) && !D->isUnsafe()) {
3880+
if (ctx.LangOpts.hasFeature(Feature::WarnUnsafe) && !D->isUnsafe()) {
38813881
auto type = D->getInterfaceType();
38823882
if (auto subs = declRef.getSubstitutions())
38833883
type = type.subst(subs);
38843884
if (type->isUnsafe()) {
3885-
if (diagnoseUnsafeType(
3886-
ctx, R.Start, type,
3887-
[&](Type specificType) {
3888-
ctx.Diags.diagnose(
3889-
R.Start, diag::reference_to_unsafe_typed_decl,
3890-
call != nullptr && !isa<ParamDecl>(D), D,
3891-
specificType);
3892-
D->diagnose(diag::decl_declared_here, D);
3893-
}))
3894-
return true;
3885+
diagnoseUnsafeType(
3886+
ctx, R.Start, type,
3887+
[&](Type specificType) {
3888+
ctx.Diags.diagnose(
3889+
R.Start, diag::reference_to_unsafe_typed_decl,
3890+
call != nullptr && !isa<ParamDecl>(D), D,
3891+
specificType);
3892+
D->diagnose(diag::decl_declared_here, D);
3893+
});
38953894
}
38963895
}
38973896

@@ -3957,21 +3956,20 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
39573956
}
39583957

39593958
/// Diagnose uses of unsafe declarations.
3960-
static bool
3959+
static void
39613960
diagnoseDeclUnsafe(const ValueDecl *D, SourceRange R,
39623961
const Expr *call, const ExportContext &Where) {
39633962
ASTContext &ctx = D->getASTContext();
3964-
if (!ctx.LangOpts.hasFeature(Feature::DisallowUnsafe))
3965-
return false;
3963+
if (!ctx.LangOpts.hasFeature(Feature::WarnUnsafe))
3964+
return;
39663965

39673966
if (!D->isUnsafe())
3968-
return false;
3967+
return;
39693968

39703969
SourceLoc diagLoc = call ? call->getLoc() : R.Start;
39713970
ctx.Diags
39723971
.diagnose(diagLoc, diag::reference_to_unsafe_decl, call != nullptr, D);
39733972
D->diagnose(diag::decl_declared_here, D);
3974-
return true;
39753973
}
39763974

39773975
/// Diagnose uses of unavailable declarations. Returns true if a diagnostic
@@ -4010,8 +4008,7 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *D, SourceRange R,
40104008
if (diagnoseDeclAsyncAvailability(D, R, call, Where))
40114009
return true;
40124010

4013-
if (diagnoseDeclUnsafe(D, R, call, Where))
4014-
return true;
4011+
diagnoseDeclUnsafe(D, R, call, Where);
40154012

40164013
// Make sure not to diagnose an accessor's deprecation if we already
40174014
// complained about the property/subscript.

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ namespace {
17331733
void visitObjCAttr(ObjCAttr *attr) {}
17341734

17351735
void visitUnsafeAttr(UnsafeAttr *attr) {
1736-
if (!Base->getASTContext().LangOpts.hasFeature(Feature::DisallowUnsafe))
1736+
if (!Base->getASTContext().LangOpts.hasFeature(Feature::WarnUnsafe))
17371737
return;
17381738

17391739
if (Override->isUnsafe() && !Base->isUnsafe()) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,7 +2445,7 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
24452445

24462446
// @unchecked conformances are considered unsafe in strict concurrency mode.
24472447
if (conformance->isUnchecked() &&
2448-
Context.LangOpts.hasFeature(Feature::DisallowUnsafe) &&
2448+
Context.LangOpts.hasFeature(Feature::WarnUnsafe) &&
24492449
Context.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete) {
24502450
Context.Diags.diagnose(ComplainLoc, diag::unchecked_conformance_is_unsafe);
24512451
}
@@ -5047,7 +5047,7 @@ void ConformanceChecker::resolveValueWitnesses() {
50475047

50485048
// If we're disallowing unsafe code, check for an unsafe witness to a
50495049
// safe requirement.
5050-
if (C.LangOpts.hasFeature(Feature::DisallowUnsafe) &&
5050+
if (C.LangOpts.hasFeature(Feature::WarnUnsafe) &&
50515051
witness->isUnsafe() && !requirement->isUnsafe()) {
50525052
witness->diagnose(diag::witness_unsafe,
50535053
witness->getDescriptiveKind(),

lib/Sema/TypeCheckType.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6422,21 +6422,21 @@ Type ExplicitCaughtTypeRequest::evaluate(
64226422
llvm_unreachable("Unhandled catch node");
64236423
}
64246424

6425-
bool swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
6425+
void swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
64266426
llvm::function_ref<void(Type)> diagnose) {
6427-
if (!ctx.LangOpts.hasFeature(Feature::DisallowUnsafe))
6428-
return false;
6427+
if (!ctx.LangOpts.hasFeature(Feature::WarnUnsafe))
6428+
return;
64296429

64306430
if (!type->isUnsafe())
6431-
return false;
6431+
return;
64326432

64336433
// Look for a specific @unsafe nominal type.
64346434
Type specificType;
64356435
type.findIf([&specificType](Type type) {
64366436
if (auto typeDecl = type->getAnyNominal()) {
64376437
if (typeDecl->isUnsafe()) {
64386438
specificType = type;
6439-
return true;
6439+
return false;
64406440
}
64416441
}
64426442

@@ -6450,6 +6450,4 @@ bool swift::diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
64506450
specificTypeDecl->diagnose(diag::unsafe_decl_here, specificTypeDecl);
64516451
}
64526452
}
6453-
6454-
return true;
64556453
}

lib/Sema/TypeCheckType.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,7 @@ bool diagnoseMissingOwnership(ParamSpecifier ownership,
688688

689689
/// If the given type involves an unsafe type, diagnose it by calling the
690690
/// diagnose function with the most specific unsafe type that can be provided.
691-
///
692-
/// \returns \c true if the type was unsafe and we are diagnosing unsafe types
693-
/// here.
694-
bool diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
691+
void diagnoseUnsafeType(ASTContext &ctx, SourceLoc loc, Type type,
695692
llvm::function_ref<void(Type)> diagnose);
696693

697694
} // end namespace swift

0 commit comments

Comments
 (0)