Skip to content

[ClangImporter] Ignore redeclared properties with different types #6175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,58 @@ struct ASTNodeBase {};
var->getType().print(Out);
abort();
}

Type typeForAccessors =
var->getInterfaceType()->getReferenceStorageReferent();
typeForAccessors =
ArchetypeBuilder::mapTypeIntoContext(var->getDeclContext(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the mapTypeIntoContext() calls are unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried just comparing the interface types and it actually failed, because sometimes the interface type of the property was a generic parameter but the interface type of an accessor was a concrete type. Or vice versa.

typeForAccessors);
if (const FuncDecl *getter = var->getGetter()) {
if (getter->getParameterLists().back()->size() != 0) {
Out << "property getter has parameters\n";
abort();
}
Type getterResultType = getter->getResultInterfaceType();
getterResultType =
ArchetypeBuilder::mapTypeIntoContext(var->getDeclContext(),
getterResultType);
if (!getterResultType->isEqual(typeForAccessors)) {
Out << "property and getter have mismatched types: '";
typeForAccessors.print(Out);
Out << "' vs. '";
getterResultType.print(Out);
Out << "'\n";
abort();
}
}

if (const FuncDecl *setter = var->getSetter()) {
if (!setter->getResultInterfaceType()->isVoid()) {
Out << "property setter has non-Void result type\n";
abort();
}
if (setter->getParameterLists().back()->size() == 0) {
Out << "property setter has no parameters\n";
abort();
}
if (setter->getParameterLists().back()->size() != 1) {
Out << "property setter has 2+ parameters\n";
abort();
}
const ParamDecl *param = setter->getParameterLists().back()->get(0);
Type paramType = param->getInterfaceType();
paramType = ArchetypeBuilder::mapTypeIntoContext(var->getDeclContext(),
paramType);
if (!paramType->isEqual(typeForAccessors)) {
Out << "property and setter param have mismatched types: '";
typeForAccessors.print(Out);
Out << "' vs. '";
paramType.print(Out);
Out << "'\n";
abort();
}
}

verifyCheckedBase(var);
}

Expand Down
10 changes: 10 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4023,6 +4023,16 @@ namespace {
if (!setter)
return;

// Check that the redeclared property's setter uses the same type as the
// original property. Objective-C can get away with the types being
// different (usually in something like nullability), but for Swift it's
// an AST invariant that's assumed and asserted elsewhere. If the type is
// different, just drop the setter, and leave the property as get-only.
assert(setter->getParameterLists().back()->size() == 1);
const ParamDecl *param = setter->getParameterLists().back()->get(0);
if (!param->getInterfaceType()->isEqual(original->getInterfaceType()))
return;

original->setComputedSetter(setter);
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4524,7 +4524,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
auto typeRepr = func->getBodyResultTypeLoc().getTypeRepr();
if (!typeRepr)
return false;


// 'Self' on a property accessor is not dynamic 'Self'...even on a read-only
// property. We could implement it as such in the future.
if (func->isAccessor())
return false;

return checkDynamicSelfReturn(func, typeRepr, 0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@interface RPFoo
@property (readonly, nonnull) int *nonnullToNullable;
@property (readonly, nullable) int *nullableToNonnull;
@property (readonly, nonnull) id typeChangeMoreSpecific;
@property (readonly, nonnull) RPFoo *typeChangeMoreGeneral;

@property (readonly, nonnull) id accessorRedeclaredAsNullable;
- (nullable id)accessorRedeclaredAsNullable;

- (nullable id)accessorDeclaredFirstAsNullable;
@property (readonly, nonnull) id accessorDeclaredFirstAsNullable;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@interface RPFoo ()
@property (readwrite, nullable) int *nonnullToNullable;
@property (readwrite, nonnull) int *nullableToNonnull;
@property (readwrite, nonnull) RPFoo *typeChangeMoreSpecific;
@property (readwrite, nonnull) id typeChangeMoreGeneral;
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#import "RPFirst.h"
#import "RPSecond.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "RPFirst.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#import "RedeclaredPropertiesSplit.h"
#import "RPSecond.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "RPFirst.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#import "RedeclaredPropertiesSub.h"
#import "RPSecond.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module RedeclaredPropertiesTextual {
textual header "RPFirst.h"
textual header "RPSecond.h"
}

module RedeclaredProperties {
header "RedeclaredProperties.h"
}

module RedeclaredPropertiesSub {
header "RedeclaredPropertiesSub.h"
explicit module Private {
header "RedeclaredPropertiesSubPrivate.h"
}
}

module RedeclaredPropertiesSplit {
header "RedeclaredPropertiesSplit.h"
}
module RedeclaredPropertiesSplit2 {
header "RedeclaredPropertiesSplit2.h"
}
32 changes: 32 additions & 0 deletions test/ClangImporter/objc_redeclared_properties.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %S/Inputs/custom-modules -D ONE_MODULE %s -verify
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %S/Inputs/custom-modules -D SUB_MODULE %s -verify
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -I %S/Inputs/custom-modules -D TWO_MODULES %s -verify

// REQUIRES: objc_interop

#if ONE_MODULE
import RedeclaredProperties
#elseif SUB_MODULE
import RedeclaredPropertiesSub
import RedeclaredPropertiesSub.Private
#elseif TWO_MODULES
import RedeclaredPropertiesSplit
import RedeclaredPropertiesSplit2
#endif

func test(obj: RPFoo) {
if let _ = obj.nonnullToNullable {} // expected-error {{initializer for conditional binding must have Optional type}}
obj.nonnullToNullable = obj // expected-error {{cannot assign to property: 'nonnullToNullable' is a get-only property}}

if let _ = obj.nullableToNonnull {} // okay
obj.nullableToNonnull = obj // expected-error {{cannot assign to property: 'nullableToNonnull' is a get-only property}}

let _: RPFoo = obj.typeChangeMoreSpecific // expected-error {{cannot convert value of type 'Any' to specified type 'RPFoo'}}
obj.typeChangeMoreSpecific = obj // expected-error {{cannot assign to property: 'typeChangeMoreSpecific' is a get-only property}}

let _: RPFoo = obj.typeChangeMoreGeneral
obj.typeChangeMoreGeneral = obj // expected-error {{cannot assign to property: 'typeChangeMoreGeneral' is a get-only property}}

if let _ = obj.accessorRedeclaredAsNullable {} // expected-error {{initializer for conditional binding must have Optional type}}
if let _ = obj.accessorDeclaredFirstAsNullable {} // expected-error {{initializer for conditional binding must have Optional type}}
}