Skip to content

@_implementationOnly: fix over-eager checking for vars in extensions #24629

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 1 commit into from
May 16, 2019
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
15 changes: 10 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2429,12 +2429,17 @@ NOTE(construct_raw_representable_from_unwrapped_value,none,
"construct %0 from unwrapped %1 value", (Type, Type))

ERROR(decl_from_implementation_only_module,none,
"cannot use %0 %1 here; %2 has been imported as implementation-only",
(DescriptiveDeclKind, DeclName, Identifier))
"cannot use %0 %1 %select{here|"
"in an extension with public or '@usableFromInline' members|"
"in an extension with conditional conformances}2; %3 has been imported "
"as implementation-only",
(DescriptiveDeclKind, DeclName, unsigned, Identifier))
ERROR(conformance_from_implementation_only_module,none,
"cannot use conformance of %0 to %1 here; %2 has been imported as "
"implementation-only",
(Type, DeclName, Identifier))
"cannot use conformance of %0 to %1 %select{here|"
"in an extension with public or '@usableFromInline' members|"
"in an extension with conditional conformances}2; %3 has been imported "
"as implementation-only",
(Type, DeclName, unsigned, Identifier))
ERROR(assoc_conformance_from_implementation_only_module,none,
"cannot use conformance of %0 to %1 in associated type %3 (inferred as "
"%4); %2 has been imported as implementation-only",
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/ResilienceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ diagnoseGenericArgumentsExportability(SourceLoc loc,
ASTContext &ctx = M->getASTContext();
ctx.Diags.diagnose(loc, diag::conformance_from_implementation_only_module,
rootConf->getType(),
rootConf->getProtocol()->getFullName(), M->getName());
rootConf->getProtocol()->getFullName(), 0, M->getName());
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be better to expose the Reason enum widely enough to use it here, but I can live with this if you can.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm not sure where it would live. Splitting it into two diagnostics might make more sense, but I think this is okay specifically because it's 0 and the first case and not any of the others.

hadAnyIssues = true;
}
return hadAnyIssues;
Expand Down
70 changes: 51 additions & 19 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class AccessControlChecker : public AccessControlCheckerBase,
highlightOffendingType(TC, diag, complainRepr);
});
}

void visitOpaqueTypeDecl(OpaqueTypeDecl *OTD) {
// TODO(opaque): The constraint class/protocols on the opaque interface, as
// well as the naming decl for the opaque type, need to be accessible.
Expand Down Expand Up @@ -1597,18 +1597,30 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
});
}

// This enum must be kept in sync with
// diag::decl_from_implementation_only_module and
// diag::conformance_from_implementation_only_module.
enum class Reason : unsigned {
General,
ExtensionWithPublicMembers,
ExtensionWithConditionalConformances
};

class DiagnoseGenerically {
TypeChecker &TC;
const Decl *D;
Reason reason;
public:
DiagnoseGenerically(TypeChecker &TC, const Decl *D) : TC(TC), D(D) {}
DiagnoseGenerically(TypeChecker &TC, const Decl *D, Reason reason)
: TC(TC), D(D), reason(reason) {}

void operator()(const TypeDecl *offendingType,
const TypeRepr *complainRepr) {
ModuleDecl *M = offendingType->getModuleContext();
auto diag = TC.diagnose(D, diag::decl_from_implementation_only_module,
offendingType->getDescriptiveKind(),
offendingType->getFullName(), M->getName());
offendingType->getFullName(),
static_cast<unsigned>(reason), M->getName());
highlightOffendingType(TC, diag, complainRepr);
}

Expand All @@ -1617,7 +1629,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
TC.diagnose(D, diag::conformance_from_implementation_only_module,
offendingConformance->getType(),
offendingConformance->getProtocol()->getFullName(),
M->getName());
static_cast<unsigned>(reason), M->getName());
}
};

Expand All @@ -1630,14 +1642,20 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
CheckExportabilityConformanceCallback>::value,
"DiagnoseGenerically has wrong call signature for conformance diags");

DiagnoseGenerically getDiagnoseCallback(const Decl *D) {
return DiagnoseGenerically(TC, D);
DiagnoseGenerically getDiagnoseCallback(const Decl *D,
Reason reason = Reason::General) {
return DiagnoseGenerically(TC, D, reason);
}

public:
explicit ExportabilityChecker(TypeChecker &TC) : TC(TC) {}

static bool shouldSkipChecking(const ValueDecl *VD) {
// Accessors are handled as part of their Var or Subscript, and we don't
// want to redo extension signature checking for them.
if (isa<AccessorDecl>(VD))
return true;

// Is this part of the module's API or ABI?
AccessScope accessScope =
VD->getFormalAccessScope(nullptr,
Expand Down Expand Up @@ -1671,12 +1689,6 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
return;

DeclVisitor<ExportabilityChecker>::visit(D);

if (auto *extension = dyn_cast<ExtensionDecl>(D->getDeclContext())) {
checkType(extension->getExtendedTypeLoc(), extension,
getDiagnoseCallback(extension), getDiagnoseCallback(extension));
checkConstrainedExtensionRequirements(extension);
}
}

// Force all kinds to be handled at a lower level.
Expand Down Expand Up @@ -1716,12 +1728,12 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
void checkNamedPattern(const NamedPattern *NP,
const llvm::DenseSet<const VarDecl *> &seenVars) {
const VarDecl *theVar = NP->getDecl();
if (shouldSkipChecking(theVar))
return;
// Only check individual variables if we didn't check an enclosing
// TypedPattern.
if (seenVars.count(theVar) || theVar->isInvalid())
return;
if (shouldSkipChecking(theVar))
return;

checkType(theVar->getInterfaceType(), /*typeRepr*/nullptr, theVar,
getDiagnoseCallback(theVar), getDiagnoseCallback(theVar));
Expand Down Expand Up @@ -1848,12 +1860,13 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
getDiagnoseCallback(EED));
}

void checkConstrainedExtensionRequirements(ExtensionDecl *ED) {
void checkConstrainedExtensionRequirements(ExtensionDecl *ED,
Reason reason) {
if (!ED->getTrailingWhereClause())
return;
forAllRequirementTypes(ED, [&](Type type, TypeRepr *typeRepr) {
checkType(type, typeRepr, ED, getDiagnoseCallback(ED),
getDiagnoseCallback(ED));
checkType(type, typeRepr, ED, getDiagnoseCallback(ED, reason),
getDiagnoseCallback(ED, reason));
});
}

Expand All @@ -1869,8 +1882,26 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
getDiagnoseCallback(ED));
});

if (!ED->getInherited().empty())
checkConstrainedExtensionRequirements(ED);
bool hasPublicMembers = llvm::any_of(ED->getMembers(),
[](const Decl *member) -> bool {
auto *valueMember = dyn_cast<ValueDecl>(member);
if (!valueMember)
return false;
return !shouldSkipChecking(valueMember);
});

if (hasPublicMembers) {
checkType(ED->getExtendedTypeLoc(), ED,
getDiagnoseCallback(ED, Reason::ExtensionWithPublicMembers),
getDiagnoseCallback(ED, Reason::ExtensionWithPublicMembers));
}

if (hasPublicMembers || !ED->getInherited().empty()) {
Reason reason =
hasPublicMembers ? Reason::ExtensionWithPublicMembers
: Reason::ExtensionWithConditionalConformances;
checkConstrainedExtensionRequirements(ED, reason);
}
}

void checkPrecedenceGroup(const PrecedenceGroupDecl *PGD,
Expand All @@ -1883,6 +1914,7 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {

auto diag = TC.diagnose(diagLoc, diag::decl_from_implementation_only_module,
PGD->getDescriptiveKind(), PGD->getName(),
static_cast<unsigned>(Reason::General),
M->getName());
if (refRange.isValid())
diag.highlight(refRange);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3678,7 +3678,7 @@ void ConformanceChecker::ensureRequirementsAreSatisfied(
conformanceBeingChecked->getLoc(),
diag::conformance_from_implementation_only_module,
rootConformance->getType(),
rootConformance->getProtocol()->getName(), M->getName());
rootConformance->getProtocol()->getName(), 0, M->getName());
Copy link
Contributor

Choose a reason for hiding this comment

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

As above.

} else {
ctx.Diags.diagnose(
conformanceBeingChecked->getLoc(),
Expand Down
20 changes: 14 additions & 6 deletions test/Sema/implementation-only-import-in-decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,35 @@ public var (testBadTypeTuplePartlyInferred3, testBadTypeTuplePartlyInferred4): (
public var testMultipleBindings1: Int? = nil, testMultipleBindings2: BadStruct? = nil // expected-error {{cannot use struct 'BadStruct' here; 'BADLibrary' has been imported as implementation-only}}
public var testMultipleBindings3: BadStruct? = nil, testMultipleBindings4: Int? = nil // expected-error {{cannot use struct 'BadStruct' here; 'BADLibrary' has been imported as implementation-only}}

extension BadStruct { // expected-error {{cannot use struct 'BadStruct' here; 'BADLibrary' has been imported as implementation-only}}
public func testExtensionOfBadType() {} // FIXME: Should complain here instead of at the extension decl.
extension BadStruct { // expected-error {{cannot use struct 'BadStruct' in an extension with public or '@usableFromInline' members; 'BADLibrary' has been imported as implementation-only}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Excellent.

public func testExtensionOfBadType() {}
public var testExtensionVarBad: Int { 0 }
public subscript(bad _: Int) -> Int { 0 }
}
extension BadStruct {
func testExtensionOfOkayType() {}
var testExtensionVarOkay: Int { 0 }
subscript(okay _: Int) -> Int { 0 }
}

extension Array where Element == BadStruct { // expected-error {{cannot use struct 'BadStruct' here; 'BADLibrary' has been imported as implementation-only}}
public func testExtensionWithBadRequirement() {} // FIXME: Should complain here instead of at the extension decl.
extension Array where Element == BadStruct { // expected-error {{cannot use struct 'BadStruct' in an extension with public or '@usableFromInline' members; 'BADLibrary' has been imported as implementation-only}}
public func testExtensionWithBadRequirement() {}
public var testExtensionVarBad: Int { 0 }
public subscript(bad _: Int) -> Int { 0 }
}

extension Array where Element == BadStruct {
func testExtensionWithOkayRequirement() {} // okay
var testExtensionVarOkay: Int { 0 } // okay
subscript(okay _: Int) -> Int { 0 } // okay
}

extension Int: BadProto {} // expected-error {{cannot use protocol 'BadProto' here; 'BADLibrary' has been imported as implementation-only}}
struct TestExtensionConformanceOkay {}
extension TestExtensionConformanceOkay: BadProto {} // okay

public protocol TestConstrainedExtensionProto {}
extension Array: TestConstrainedExtensionProto where Element == BadStruct { // expected-error {{cannot use struct 'BadStruct' here; 'BADLibrary' has been imported as implementation-only}}
extension Array: TestConstrainedExtensionProto where Element == BadStruct { // expected-error {{cannot use struct 'BadStruct' in an extension with conditional conformances; 'BADLibrary' has been imported as implementation-only}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be allowed if TestConstrainedExtensionProto was internal or less? If so, I wish the message conveyed that...but as I started writing an example, I realized that fully describing it gets pretty long and complicated. I guess this Is fine for now; we can introduce notes pointing to the relevant declarations when we package this.

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 think we don't actually allow that right now even though we probably should.

}


Expand Down Expand Up @@ -165,7 +173,7 @@ public class SubclassOfNormalClass: NormalClass {}
public func testInheritedConformance(_: NormalProtoAssocHolder<SubclassOfNormalClass>) {} // expected-error {{cannot use conformance of 'NormalClass' to 'NormalProto' here; 'BADLibrary' has been imported as implementation-only}}
public func testSpecializedConformance(_: NormalProtoAssocHolder<GenericStruct<Int>>) {} // expected-error {{cannot use conformance of 'GenericStruct<T>' to 'NormalProto' here; 'BADLibrary' has been imported as implementation-only}}

extension Array where Element == NormalProtoAssocHolder<NormalStruct> { // expected-error {{cannot use conformance of 'NormalStruct' to 'NormalProto' here; 'BADLibrary' has been imported as implementation-only}}
extension Array where Element == NormalProtoAssocHolder<NormalStruct> { // expected-error {{cannot use conformance of 'NormalStruct' to 'NormalProto' in an extension with public or '@usableFromInline' members; 'BADLibrary' has been imported as implementation-only}}
public func testConstrainedExtensionUsingBadConformance() {}
}

Expand Down