Skip to content

Fixes for @_versioned attribute #11979

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
Sep 18, 2017
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
6 changes: 2 additions & 4 deletions lib/Sema/DerivedConformanceCodable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,7 @@ static FuncDecl *deriveEncodable_encode(TypeChecker &tc, Decl *parentDecl,
}

encodeDecl->setInterfaceType(interfaceType);
encodeDecl->setAccess(std::max(target->getFormalAccess(),
AccessLevel::Internal));
encodeDecl->setAccess(target->getFormalAccess());

// If the type was not imported, the derived conformance is either from the
// type itself or an extension, in which case we will emit the declaration
Expand Down Expand Up @@ -1149,8 +1148,7 @@ static ValueDecl *deriveDecodable_init(TypeChecker &tc, Decl *parentDecl,

initDecl->setInterfaceType(interfaceType);
initDecl->setInitializerInterfaceType(initializerType);
initDecl->setAccess(std::max(target->getFormalAccess(),
AccessLevel::Internal));
initDecl->setAccess(target->getFormalAccess());

// If the type was not imported, the derived conformance is either from the
// type itself or an extension, in which case we will emit the declaration
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/DerivedConformanceCodingKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ static ValueDecl *deriveInitDecl(TypeChecker &tc, Decl *parentDecl,
}
initDecl->setInterfaceType(allocIfaceType);
initDecl->setInitializerInterfaceType(initIfaceType);
initDecl->setAccess(std::max(AccessLevel::Internal,
enumDecl->getFormalAccess()));
initDecl->setAccess(enumDecl->getFormalAccess());

// If the enum was not imported, the derived conformance is either from the
// enum itself or an extension, in which case we will emit the declaration
Expand Down
11 changes: 3 additions & 8 deletions lib/Sema/DerivedConformanceEquatableHashable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,7 @@ deriveEquatable_enum_eq(TypeChecker &tc, Decl *parentDecl, EnumDecl *enumDecl) {
FunctionType::ExtInfo());
}
eqDecl->setInterfaceType(interfaceTy);

// Since we can't insert the == operator into the same FileUnit as the enum,
// itself, we have to give it at least internal access.
eqDecl->setAccess(std::max(enumDecl->getFormalAccess(),
AccessLevel::Internal));
copyFormalAccess(eqDecl, enumDecl);

// If the enum was not imported, the derived conformance is either from the
// enum itself or an extension, in which case we will emit the declaration
Expand Down Expand Up @@ -430,8 +426,7 @@ deriveHashable_enum_hashValue(TypeChecker &tc, Decl *parentDecl,
AnyFunctionType::ExtInfo());

getterDecl->setInterfaceType(interfaceType);
getterDecl->setAccess(std::max(AccessLevel::Internal,
enumDecl->getFormalAccess()));
copyFormalAccess(getterDecl, enumDecl);

// If the enum was not imported, the derived conformance is either from the
// enum itself or an extension, in which case we will emit the declaration
Expand All @@ -447,7 +442,7 @@ deriveHashable_enum_hashValue(TypeChecker &tc, Decl *parentDecl,
hashValueDecl->setInterfaceType(intType);
hashValueDecl->makeComputed(SourceLoc(), getterDecl,
nullptr, nullptr, SourceLoc());
hashValueDecl->setAccess(getterDecl->getFormalAccess());
copyFormalAccess(hashValueDecl, enumDecl);

Pattern *hashValuePat = new (C) NamedPattern(hashValueDecl, /*implicit*/true);
hashValuePat->setType(intType);
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/DerivedConformanceRawRepresentable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ static ConstructorDecl *deriveRawRepresentable_init(TypeChecker &tc,
}
initDecl->setInterfaceType(allocIfaceType);
initDecl->setInitializerInterfaceType(initIfaceType);
initDecl->setAccess(std::max(AccessLevel::Internal,
enumDecl->getFormalAccess()));
copyFormalAccess(initDecl, enumDecl);

// If the enum was not imported, the derived conformance is either from the
// enum itself or an extension, in which case we will emit the declaration
Expand Down
16 changes: 13 additions & 3 deletions lib/Sema/DerivedConformances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ FuncDecl *DerivedConformance::declareDerivedPropertyGetter(TypeChecker &tc,
interfaceType = FunctionType::get({selfParam}, interfaceType,
FunctionType::ExtInfo());
getterDecl->setInterfaceType(interfaceType);
getterDecl->setAccess(std::max(typeDecl->getFormalAccess(),
AccessLevel::Internal));
copyFormalAccess(getterDecl, typeDecl);

// If the enum was not imported, the derived conformance is either from the
// enum itself or an extension, in which case we will emit the declaration
Expand Down Expand Up @@ -202,7 +201,7 @@ DerivedConformance::declareDerivedReadOnlyProperty(TypeChecker &tc,
propDecl->setImplicit();
propDecl->makeComputed(SourceLoc(), getterDecl, nullptr, nullptr,
SourceLoc());
propDecl->setAccess(getterDecl->getFormalAccess());
copyFormalAccess(propDecl, typeDecl);
propDecl->setInterfaceType(propertyInterfaceType);

// If this is supposed to be a final property, mark it as such.
Expand All @@ -225,3 +224,14 @@ DerivedConformance::declareDerivedReadOnlyProperty(TypeChecker &tc,

return {propDecl, pbDecl};
}

void DerivedConformance::copyFormalAccess(ValueDecl *dest, ValueDecl *source) {
dest->setAccess(source->getFormalAccess());

// Inherit the @_versioned attribute.
if (source->getAttrs().hasAttribute<VersionedAttr>()) {
auto &ctx = source->getASTContext();
auto *clonedAttr = new (ctx) VersionedAttr(/*implicit=*/true);
dest->getAttrs().add(clonedAttr);
}
}
3 changes: 3 additions & 0 deletions lib/Sema/DerivedConformances.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ declareDerivedReadOnlyProperty(TypeChecker &tc,
/// Build a reference to the 'self' decl of a derived function.
DeclRefExpr *createSelfDeclRef(AbstractFunctionDecl *fn);

/// Copy access from the source decl to the destination decl.
void copyFormalAccess(ValueDecl *dest, ValueDecl *source);

}

}
Expand Down
2 changes: 1 addition & 1 deletion test/attr/accessibility_print.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private enum DA_PrivateEnum {
case Foo, Bar
// CHECK: internal init()
init() { self = .Foo }
// CHECK: internal var hashValue
// CHECK: private var hashValue
} // CHECK: {{^[}]}}

// CHECK-LABEL: internal{{(\*/)?}} enum DB_InternalEnum {
Expand Down
20 changes: 20 additions & 0 deletions test/attr/attr_versioned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,23 @@ protocol VersionedProtocol {
@_versioned func versionedRequirement() -> T
// expected-error@-1 {{'@_versioned' attribute cannot be used in protocols}}
}

// Derived conformances had issues with @_versioned - rdar://problem/34342955
@_versioned
internal enum EqEnum {
case foo
}

@_versioned
internal enum RawEnum : Int {
case foo = 0
}

@_inlineable
public func usesEqEnum() -> Bool {
_ = (EqEnum.foo == .foo)
_ = EqEnum.foo.hashValue

_ = RawEnum.foo.rawValue
_ = RawEnum(rawValue: 0)
}