Skip to content

Commit 45296d1

Browse files
committed
[NFC] Make checkReferenceOwnershipAttr a Utility
We can get the context from the provided VarDecl.
1 parent c849652 commit 45296d1

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,7 @@ void TypeChecker::checkParameterAttributes(ParameterList *params) {
26842684

26852685
Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
26862686
ReferenceOwnershipAttr *attr) {
2687+
auto &Diags = var->getASTContext().Diags;
26872688
auto *dc = var->getDeclContext();
26882689

26892690
// Don't check ownership attribute if the type is invalid.
@@ -2699,18 +2700,16 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
26992700
switch (optionalityOf(ownershipKind)) {
27002701
case ReferenceOwnershipOptionality::Disallowed:
27012702
if (isOptional) {
2702-
diagnose(var->getStartLoc(), diag::invalid_ownership_with_optional,
2703-
ownershipKind)
2704-
.fixItReplace(attr->getRange(), "weak");
2703+
var->diagnose(diag::invalid_ownership_with_optional, ownershipKind)
2704+
.fixItReplace(attr->getRange(), "weak");
27052705
attr->setInvalid();
27062706
}
27072707
break;
27082708
case ReferenceOwnershipOptionality::Allowed:
27092709
break;
27102710
case ReferenceOwnershipOptionality::Required:
27112711
if (var->isLet()) {
2712-
diagnose(var->getStartLoc(), diag::invalid_ownership_is_let,
2713-
ownershipKind);
2712+
var->diagnose(diag::invalid_ownership_is_let, ownershipKind);
27142713
attr->setInvalid();
27152714
}
27162715

@@ -2722,10 +2721,8 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
27222721
if (var->getAttrs().hasAttribute<IBOutletAttr>())
27232722
break;
27242723

2725-
auto diag = diagnose(var->getStartLoc(),
2726-
diag::invalid_ownership_not_optional,
2727-
ownershipKind,
2728-
OptionalType::get(type));
2724+
auto diag = var->diagnose(diag::invalid_ownership_not_optional,
2725+
ownershipKind, OptionalType::get(type));
27292726
auto typeRange = var->getTypeSourceRangeForDiagnostics();
27302727
if (type->hasSimpleTypeRepr()) {
27312728
diag.fixItInsertAfter(typeRange.End, "?");
@@ -2750,36 +2747,37 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
27502747
D = diag::invalid_ownership_protocol_type;
27512748
}
27522749

2753-
diagnose(var->getStartLoc(), D, ownershipKind, underlyingType);
2750+
var->diagnose(D, ownershipKind, underlyingType);
27542751
attr->setInvalid();
27552752
}
27562753

27572754
ClassDecl *underlyingClass = underlyingType->getClassOrBoundGenericClass();
27582755
if (underlyingClass && underlyingClass->isIncompatibleWithWeakReferences()) {
2759-
diagnose(attr->getLocation(),
2760-
diag::invalid_ownership_incompatible_class,
2761-
underlyingType, ownershipKind)
2762-
.fixItRemove(attr->getRange());
2756+
Diags
2757+
.diagnose(attr->getLocation(),
2758+
diag::invalid_ownership_incompatible_class, underlyingType,
2759+
ownershipKind)
2760+
.fixItRemove(attr->getRange());
27632761
attr->setInvalid();
27642762
}
27652763

27662764
auto PDC = dyn_cast<ProtocolDecl>(dc);
27672765
if (PDC && !PDC->isObjC()) {
27682766
// Ownership does not make sense in protocols, except for "weak" on
27692767
// properties of Objective-C protocols.
2770-
auto D = Context.isSwiftVersionAtLeast(5)
2771-
? diag::ownership_invalid_in_protocols
2772-
: diag::ownership_invalid_in_protocols_compat_warning;
2773-
diagnose(attr->getLocation(), D, ownershipKind)
2774-
.fixItRemove(attr->getRange());
2768+
auto D = var->getASTContext().isSwiftVersionAtLeast(5)
2769+
? diag::ownership_invalid_in_protocols
2770+
: diag::ownership_invalid_in_protocols_compat_warning;
2771+
Diags.diagnose(attr->getLocation(), D, ownershipKind)
2772+
.fixItRemove(attr->getRange());
27752773
attr->setInvalid();
27762774
}
27772775

27782776
if (attr->isInvalid())
27792777
return type;
27802778

27812779
// Change the type to the appropriate reference storage type.
2782-
return ReferenceStorageType::get(type, ownershipKind, Context);
2780+
return ReferenceStorageType::get(type, ownershipKind, var->getASTContext());
27832781
}
27842782

27852783
Optional<Diag<>>

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4211,7 +4211,8 @@ void TypeChecker::validateDecl(ValueDecl *D) {
42114211
// In SIL mode, VarDecls are written as having reference storage types.
42124212
if (!interfaceType->is<ReferenceStorageType>()) {
42134213
if (auto *attr = VD->getAttrs().getAttribute<ReferenceOwnershipAttr>())
4214-
interfaceType = checkReferenceOwnershipAttr(VD, interfaceType, attr);
4214+
interfaceType =
4215+
TypeChecker::checkReferenceOwnershipAttr(VD, interfaceType, attr);
42154216
}
42164217
VD->setInterfaceType(interfaceType);
42174218

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,8 @@ class TypeChecker final : public LazyResolver {
10201020
void checkParameterAttributes(ParameterList *params);
10211021
static ValueDecl *findReplacedDynamicFunction(const ValueDecl *d);
10221022

1023-
Type checkReferenceOwnershipAttr(VarDecl *D, Type interfaceType,
1024-
ReferenceOwnershipAttr *attr);
1023+
static Type checkReferenceOwnershipAttr(VarDecl *D, Type interfaceType,
1024+
ReferenceOwnershipAttr *attr);
10251025

10261026
virtual void resolveDeclSignature(ValueDecl *VD) override {
10271027
validateDecl(VD);

0 commit comments

Comments
 (0)