Skip to content

Commit 18ce8fe

Browse files
committed
IUO: Update decl checking for result type optionality differences.
A previous commit handled parameters but missed handling result types. As with that commit, we need to pay attention to the IUO attribute on the decl rather than just concerning ourselves with the types.
1 parent 238b876 commit 18ce8fe

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4373,6 +4373,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
43734373

43744374
validateAttributes(TC, SD);
43754375

4376+
auto *TyR = SD->getElementTypeLoc().getTypeRepr();
4377+
if (TyR && TyR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional) {
4378+
auto &C = SD->getASTContext();
4379+
SD->getAttrs().add(
4380+
new (C) ImplicitlyUnwrappedOptionalAttr(/* implicit= */ true));
4381+
}
4382+
43764383
if (!checkOverrides(TC, SD)) {
43774384
// If a subscript has an override attribute but does not override
43784385
// anything, complain.
@@ -4415,13 +4422,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
44154422
synthesizeSetterForMutableAddressedStorage(SD, TC);
44164423
}
44174424

4418-
auto *TyR = SD->getElementTypeLoc().getTypeRepr();
4419-
if (TyR && TyR->getKind() == TypeReprKind::ImplicitlyUnwrappedOptional) {
4420-
auto &C = SD->getASTContext();
4421-
SD->getAttrs().add(
4422-
new (C) ImplicitlyUnwrappedOptionalAttr(/* implicit= */ true));
4423-
}
4424-
44254425
TC.checkDeclAttributes(SD);
44264426
}
44274427

@@ -5746,7 +5746,11 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
57465746

57475747
TypeRepr *TR = resultTL.getTypeRepr();
57485748

5749-
if (resultOTK == OTK_Optional || treatIUOResultAsError) {
5749+
bool resultIsPlainOptional = resultOTK == OTK_Optional;
5750+
if (member->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>())
5751+
resultIsPlainOptional = false;
5752+
5753+
if (resultIsPlainOptional || treatIUOResultAsError) {
57505754
if (parentResultTy->getAnyOptionalObjectType())
57515755
return;
57525756
emittedError = true;
@@ -6119,7 +6123,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
61196123
auto canDeclTy = declTy->getCanonicalType(genericSig);
61206124
auto canParentDeclTy = parentDeclTy->getCanonicalType(genericSig);
61216125

6122-
if (canDeclTy == canParentDeclTy) {
6126+
auto declIUOAttr =
6127+
decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
6128+
auto parentDeclIUOAttr =
6129+
parentDecl->getAttrs()
6130+
.hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
6131+
6132+
if (declIUOAttr == parentDeclIUOAttr && canDeclTy == canParentDeclTy) {
61236133
matches.push_back({parentDecl, true, parentDeclTy});
61246134
hadExactMatch = true;
61256135
continue;
@@ -6321,8 +6331,13 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
63216331
(attempt == OverrideCheckingAttempt::MismatchedOptional ||
63226332
attempt == OverrideCheckingAttempt::BaseNameWithMismatchedOptional);
63236333

6334+
auto declIUOAttr =
6335+
decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
6336+
auto matchDeclIUOAttr =
6337+
matchDecl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
6338+
63246339
// If this is an exact type match, we're successful!
6325-
if (declTy->isEqual(matchType)) {
6340+
if (declIUOAttr == matchDeclIUOAttr && declTy->isEqual(matchType)) {
63266341
// Nothing to do.
63276342

63286343
} else if (method) {

0 commit comments

Comments
 (0)