Skip to content

Commit 9a8a9d4

Browse files
authored
Merge pull request #16058 from davezarzycki/qoi_formalize_ReferenceOwnership_diags
[Diag] QoI: Add ReferenceOwnership to DiagnosticArgumentKind
2 parents 047f9e8 + e326954 commit 9a8a9d4

15 files changed

+171
-136
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace swift {
3030
class ValueDecl;
3131

3232
enum class PatternKind : uint8_t;
33+
enum class ReferenceOwnership : uint8_t;
3334
enum class StaticSpellingKind : uint8_t;
3435
enum class DescriptiveDeclKind : uint8_t;
3536
enum DeclAttrKind : unsigned;
@@ -76,6 +77,7 @@ namespace swift {
7677
Type,
7778
TypeRepr,
7879
PatternKind,
80+
ReferenceOwnership,
7981
StaticSpellingKind,
8082
DescriptiveDeclKind,
8183
DeclAttribute,
@@ -103,6 +105,7 @@ namespace swift {
103105
Type TypeVal;
104106
TypeRepr *TyR;
105107
PatternKind PatternKindVal;
108+
ReferenceOwnership ReferenceOwnershipVal;
106109
StaticSpellingKind StaticSpellingKindVal;
107110
DescriptiveDeclKind DescriptiveDeclKindVal;
108111
const DeclAttribute *DeclAttributeVal;
@@ -162,6 +165,10 @@ namespace swift {
162165
DiagnosticArgument(PatternKind K)
163166
: Kind(DiagnosticArgumentKind::PatternKind), PatternKindVal(K) {}
164167

168+
DiagnosticArgument(ReferenceOwnership RO)
169+
: Kind(DiagnosticArgumentKind::ReferenceOwnership),
170+
ReferenceOwnershipVal(RO) {}
171+
165172
DiagnosticArgument(StaticSpellingKind SSK)
166173
: Kind(DiagnosticArgumentKind::StaticSpellingKind),
167174
StaticSpellingKindVal(SSK) {}
@@ -237,6 +244,11 @@ namespace swift {
237244
return PatternKindVal;
238245
}
239246

247+
ReferenceOwnership getAsReferenceOwnership() const {
248+
assert(Kind == DiagnosticArgumentKind::ReferenceOwnership);
249+
return ReferenceOwnershipVal;
250+
}
251+
240252
StaticSpellingKind getAsStaticSpellingKind() const {
241253
assert(Kind == DiagnosticArgumentKind::StaticSpellingKind);
242254
return StaticSpellingKindVal;

include/swift/AST/DiagnosticsSema.def

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,9 +1960,8 @@ ERROR(override_argument_name_mismatch,none,
19601960
"of overridden %select{method|initializer}0 %2",
19611961
(bool, DeclName, DeclName))
19621962
ERROR(override_ownership_mismatch,none,
1963-
"cannot override %select{strong|weak|unowned|unowned(unsafe)}0 property "
1964-
"with %select{strong|weak|unowned|unowned(unsafe)}1 property",
1965-
(/*ReferenceOwnership*/unsigned, /*ReferenceOwnership*/unsigned))
1963+
"cannot override %0 property with %1 property",
1964+
(ReferenceOwnership, ReferenceOwnership))
19661965
ERROR(override_dynamic_self_mismatch,none,
19671966
"cannot override a Self return type with a non-Self return type",
19681967
())
@@ -3078,10 +3077,9 @@ NOTE(add_explicit_type_annotation_to_silence,none,
30783077
"add an explicit type annotation to silence this warning", ())
30793078

30803079
WARNING(unowned_assignment_immediate_deallocation,none,
3081-
"instance will be immediately deallocated as %0 is "
3082-
"%select{a 'strong'|a 'weak'|an 'unowned'|an 'unowned'}1 "
3083-
"%select{variable|property}2",
3084-
(Identifier, /*Ownership*/unsigned, /*Is Property*/unsigned))
3080+
"instance will be immediately deallocated because "
3081+
"%select{variable|property}2 %0 is %1",
3082+
(Identifier, ReferenceOwnership, /*Is Property*/unsigned))
30853083
NOTE(unowned_assignment_requires_strong,none,
30863084
"a strong reference is required to prevent the instance from being "
30873085
"deallocated", ())
@@ -3239,24 +3237,23 @@ ERROR(implicitly_unwrapped_optional_in_illegal_position,none,
32393237

32403238
// Ownership
32413239
ERROR(invalid_ownership_type,none,
3242-
"'%select{strong|weak|unowned|unowned}0' may only be applied to "
3243-
"class and class-bound protocol types, not %1",
3244-
(/*ReferenceOwnership*/unsigned, Type))
3240+
"%0 may only be applied to class and class-bound protocol types, not %1",
3241+
(ReferenceOwnership, Type))
32453242
ERROR(invalid_ownership_protocol_type,none,
3246-
"'%select{strong|weak|unowned|unowned}0' must not be applied to "
3247-
"non-class-bound %1; consider adding a protocol conformance that has a class bound",
3248-
(/*ReferenceOwnership*/unsigned, Type))
3243+
"%0 must not be applied to non-class-bound %1; "
3244+
"consider adding a protocol conformance that has a class bound",
3245+
(ReferenceOwnership, Type))
32493246
ERROR(invalid_weak_ownership_not_optional,none,
32503247
"'weak' variable should have optional type %0", (Type))
32513248
ERROR(invalid_weak_let,none,
32523249
"'weak' must be a mutable variable, because it may change at runtime", ())
32533250
ERROR(ownership_invalid_in_protocols,none,
3254-
"'%select{strong|weak|unowned|unowned}0' cannot be applied to a property declaration in a protocol",
3255-
(/*ReferenceOwnership*/unsigned))
3251+
"%0 cannot be applied to a property declaration in a protocol",
3252+
(ReferenceOwnership))
32563253
WARNING(ownership_invalid_in_protocols_compat_warning,none,
3257-
"'%select{strong|weak|unowned|unowned}0' should not be applied to a property declaration "
3254+
"%0 should not be applied to a property declaration "
32583255
"in a protocol and will be disallowed in future versions",
3259-
(/*ReferenceOwnership*/unsigned))
3256+
(ReferenceOwnership))
32603257

32613258
// required
32623259
ERROR(required_initializer_nonclass,none,

include/swift/AST/Ownership.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SWIFT_OWNERSHIP_H
2121

2222
#include "swift/Basic/InlineBitfield.h"
23+
#include "llvm/Support/raw_ostream.h"
2324
#include <stdint.h>
2425

2526
namespace swift {
@@ -46,6 +47,9 @@ enum class ReferenceOwnership : uint8_t {
4647
enum : unsigned { NumReferenceOwnershipBits =
4748
countBitsUsed(static_cast<unsigned>(ReferenceOwnership::Last_Kind)) };
4849

50+
/// Diagnostic printing of \c StaticSpellingKind.
51+
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceOwnership RO);
52+
4953
/// Different kinds of value ownership supported by Swift.
5054
enum class ValueOwnership : uint8_t {
5155
/// \brief the context-dependent default ownership (sometimes shared,

lib/AST/Decl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &OS,
296296
llvm_unreachable("bad StaticSpellingKind");
297297
}
298298

299+
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &OS,
300+
ReferenceOwnership RO) {
301+
switch (RO) {
302+
case ReferenceOwnership::Strong: return OS << "'strong'";
303+
case ReferenceOwnership::Weak: return OS << "'weak'";
304+
case ReferenceOwnership::Unowned: return OS << "'unowned'";
305+
case ReferenceOwnership::Unmanaged: return OS << "'unowned(unsafe)'";
306+
}
307+
llvm_unreachable("bad ReferenceOwnership kind");
308+
}
309+
299310
DeclContext *Decl::getInnermostDeclContext() const {
300311
if (auto func = dyn_cast<AbstractFunctionDecl>(this))
301312
return const_cast<AbstractFunctionDecl*>(func);

lib/AST/DiagnosticEngine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,19 @@ static void formatDiagnosticArgument(StringRef Modifier,
472472
assert(Modifier.empty() && "Improper modifier for PatternKind argument");
473473
Out << Arg.getAsPatternKind();
474474
break;
475+
476+
case DiagnosticArgumentKind::ReferenceOwnership:
477+
if (Modifier == "select") {
478+
formatSelectionArgument(ModifierArguments, Args,
479+
unsigned(Arg.getAsReferenceOwnership()),
480+
FormatOpts, Out);
481+
} else {
482+
assert(Modifier.empty() &&
483+
"Improper modifier for ReferenceOwnership argument");
484+
Out << Arg.getAsReferenceOwnership();
485+
}
486+
break;
487+
475488
case DiagnosticArgumentKind::StaticSpellingKind:
476489
if (Modifier == "select") {
477490
formatSelectionArgument(ModifierArguments, Args,

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,8 +2129,7 @@ static void diagnoseUnownedImmediateDeallocationImpl(TypeChecker &TC,
21292129
storageKind = SK_Property;
21302130

21312131
TC.diagnose(diagLoc, diag::unowned_assignment_immediate_deallocation,
2132-
varDecl->getName(),
2133-
(unsigned) ownershipAttr->get(), (unsigned) storageKind)
2132+
varDecl->getName(), ownershipAttr->get(), unsigned(storageKind))
21342133
.highlight(diagRange);
21352134

21362135
TC.diagnose(diagLoc, diag::unowned_assignment_requires_strong)

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,21 +2178,21 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,
21782178
D = diag::invalid_ownership_protocol_type;
21792179
}
21802180

2181-
diagnose(var->getStartLoc(), D, (unsigned) ownershipKind, underlyingType);
2181+
diagnose(var->getStartLoc(), D, ownershipKind, underlyingType);
21822182
attr->setInvalid();
21832183
} else if (isa<ProtocolDecl>(var->getDeclContext()) &&
21842184
!cast<ProtocolDecl>(var->getDeclContext())->isObjC()) {
21852185
// Ownership does not make sense in protocols, except for "weak" on
21862186
// properties of Objective-C protocols.
21872187
if (Context.isSwiftVersionAtLeast(5))
21882188
diagnose(attr->getLocation(),
2189-
diag::ownership_invalid_in_protocols,
2190-
(unsigned) ownershipKind)
2189+
diag::ownership_invalid_in_protocols,
2190+
ownershipKind)
21912191
.fixItRemove(attr->getRange());
21922192
else
21932193
diagnose(attr->getLocation(),
2194-
diag::ownership_invalid_in_protocols_compat_warning,
2195-
(unsigned) ownershipKind)
2194+
diag::ownership_invalid_in_protocols_compat_warning,
2195+
ownershipKind)
21962196
.fixItRemove(attr->getRange());
21972197

21982198
attr->setInvalid();

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,8 +5484,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
54845484
parentOwnership = ReferenceOwnership::Strong;
54855485
if (parentOwnership != ownershipAttr->get()) {
54865486
TC.diagnose(decl, diag::override_ownership_mismatch,
5487-
(unsigned)parentOwnership,
5488-
(unsigned)ownershipAttr->get());
5487+
parentOwnership, ownershipAttr->get());
54895488
TC.diagnose(matchDecl, diag::overridden_here);
54905489
}
54915490
}

test/SILOptimizer/definite_init_diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func test2() {
111111
weak var w1 : SomeClass?
112112
_ = w1 // ok: default-initialized
113113

114-
// expected-warning@+3 {{instance will be immediately deallocated as 'w2' is a 'weak' variable}}
114+
// expected-warning@+3 {{instance will be immediately deallocated because variable 'w2' is 'weak'}}
115115
// expected-note@+2 {{a strong reference is required to prevent the instance from being deallocated}}
116116
// expected-note@+1 {{'w2' declared here}}
117117
weak var w2 = SomeClass()
@@ -124,7 +124,7 @@ func test2() {
124124
unowned var u1 : SomeClass // expected-note {{variable defined here}}
125125
_ = u1 // expected-error {{variable 'u1' used before being initialized}}
126126

127-
// expected-warning@+3 {{instance will be immediately deallocated as 'u2' is an 'unowned' variable}}
127+
// expected-warning@+3 {{instance will be immediately deallocated because variable 'u2' is 'unowned'}}
128128
// expected-note@+2 {{a strong reference is required to prevent the instance from being deallocated}}
129129
// expected-note@+1 {{'u2' declared here}}
130130
unowned let u2 = SomeClass()

0 commit comments

Comments
 (0)