Skip to content

Commit 571859f

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 7e831f2 commit 571859f

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
@@ -4900,7 +4900,7 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
49004900

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

@@ -7100,7 +7100,7 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
71007100

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

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,21 +3886,20 @@ bool ExprAvailabilityWalker::diagnoseDeclRefAvailability(
38863886
// If the declaration itself is "safe" but we don't disallow unsafe uses,
38873887
// check whether it traffics in unsafe types.
38883888
ASTContext &ctx = D->getASTContext();
3889-
if (ctx.LangOpts.hasFeature(Feature::DisallowUnsafe) && !D->isUnsafe()) {
3889+
if (ctx.LangOpts.hasFeature(Feature::WarnUnsafe) && !D->isUnsafe()) {
38903890
auto type = D->getInterfaceType();
38913891
if (auto subs = declRef.getSubstitutions())
38923892
type = type.subst(subs);
38933893
if (type->isUnsafe()) {
3894-
if (diagnoseUnsafeType(
3895-
ctx, R.Start, type,
3896-
[&](Type specificType) {
3897-
ctx.Diags.diagnose(
3898-
R.Start, diag::reference_to_unsafe_typed_decl,
3899-
call != nullptr && !isa<ParamDecl>(D), D,
3900-
specificType);
3901-
D->diagnose(diag::decl_declared_here, D);
3902-
}))
3903-
return true;
3894+
diagnoseUnsafeType(
3895+
ctx, R.Start, type,
3896+
[&](Type specificType) {
3897+
ctx.Diags.diagnose(
3898+
R.Start, diag::reference_to_unsafe_typed_decl,
3899+
call != nullptr && !isa<ParamDecl>(D), D,
3900+
specificType);
3901+
D->diagnose(diag::decl_declared_here, D);
3902+
});
39043903
}
39053904
}
39063905

@@ -3966,21 +3965,20 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
39663965
}
39673966

39683967
/// Diagnose uses of unsafe declarations.
3969-
static bool
3968+
static void
39703969
diagnoseDeclUnsafe(const ValueDecl *D, SourceRange R,
39713970
const Expr *call, const ExportContext &Where) {
39723971
ASTContext &ctx = D->getASTContext();
3973-
if (!ctx.LangOpts.hasFeature(Feature::DisallowUnsafe))
3974-
return false;
3972+
if (!ctx.LangOpts.hasFeature(Feature::WarnUnsafe))
3973+
return;
39753974

39763975
if (!D->isUnsafe())
3977-
return false;
3976+
return;
39783977

39793978
SourceLoc diagLoc = call ? call->getLoc() : R.Start;
39803979
ctx.Diags
39813980
.diagnose(diagLoc, diag::reference_to_unsafe_decl, call != nullptr, D);
39823981
D->diagnose(diag::decl_declared_here, D);
3983-
return true;
39843982
}
39853983

39863984
/// Diagnose uses of unavailable declarations. Returns true if a diagnostic
@@ -4019,8 +4017,7 @@ bool swift::diagnoseDeclAvailability(const ValueDecl *D, SourceRange R,
40194017
if (diagnoseDeclAsyncAvailability(D, R, call, Where))
40204018
return true;
40214019

4022-
if (diagnoseDeclUnsafe(D, R, call, Where))
4023-
return true;
4020+
diagnoseDeclUnsafe(D, R, call, Where);
40244021

40254022
// Make sure not to diagnose an accessor's deprecation if we already
40264023
// 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
@@ -2430,7 +2430,7 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {
24302430

24312431
// @unchecked conformances are considered unsafe in strict concurrency mode.
24322432
if (conformance->isUnchecked() &&
2433-
Context.LangOpts.hasFeature(Feature::DisallowUnsafe) &&
2433+
Context.LangOpts.hasFeature(Feature::WarnUnsafe) &&
24342434
Context.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete) {
24352435
Context.Diags.diagnose(ComplainLoc, diag::unchecked_conformance_is_unsafe);
24362436
}
@@ -5032,7 +5032,7 @@ void ConformanceChecker::resolveValueWitnesses() {
50325032

50335033
// If we're disallowing unsafe code, check for an unsafe witness to a
50345034
// safe requirement.
5035-
if (C.LangOpts.hasFeature(Feature::DisallowUnsafe) &&
5035+
if (C.LangOpts.hasFeature(Feature::WarnUnsafe) &&
50365036
witness->isUnsafe() && !requirement->isUnsafe()) {
50375037
witness->diagnose(diag::witness_unsafe,
50385038
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)