Skip to content

Give a proper error when a member type is inaccessible. #4033

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
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
1 change: 1 addition & 0 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,7 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,

case MemberLookupResult::UR_Inaccessible: {
auto decl = result.UnviableCandidates[0].first;
// FIXME: What if the unviable candidates have different levels of access?
diagnose(nameLoc, diag::candidate_inaccessible, decl->getName(),
decl->getFormalAccess());
for (auto cand : result.UnviableCandidates)
Expand Down
30 changes: 28 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ static Type diagnoseUnknownType(TypeChecker &tc, DeclContext *dc,
SourceRange parentRange,
ComponentIdentTypeRepr *comp,
TypeResolutionOptions options,
NameLookupOptions lookupOptions,
GenericTypeResolver *resolver,
UnsatisfiedDependency *unsatisfiedDependency) {
// Unqualified lookup case.
Expand Down Expand Up @@ -785,6 +786,30 @@ static Type diagnoseUnknownType(TypeChecker &tc, DeclContext *dc,
}

// Qualified lookup case.

// Try ignoring access control.
NameLookupOptions relookupOptions = lookupOptions;
relookupOptions |= NameLookupFlags::KnownPrivate;
relookupOptions |= NameLookupFlags::IgnoreAccessibility;
auto inaccessibleMembers = tc.lookupMemberType(dc, parentType,
comp->getIdentifier(),
relookupOptions);
if (inaccessibleMembers) {
// FIXME: What if the unviable candidates have different levels of access?
const TypeDecl *first = inaccessibleMembers.front().first;
tc.diagnose(comp->getIdLoc(), diag::candidate_inaccessible,
comp->getIdentifier(), first->getFormalAccess());

// FIXME: If any of the candidates (usually just one) are in the same module
// we could offer a fix-it.
for (auto lookupResult : inaccessibleMembers)
tc.diagnose(lookupResult.first, diag::type_declared_here);

// Don't try to recover here; we'll get more access-related diagnostics
// downstream if we do.
return ErrorType::get(tc.Context);
}

// FIXME: Typo correction!

// Lookup into a type.
Expand Down Expand Up @@ -986,7 +1011,7 @@ resolveTopLevelIdentTypeComponent(TypeChecker &TC, DeclContext *DC,
return ErrorType::get(TC.Context);

return diagnoseUnknownType(TC, DC, nullptr, SourceRange(), comp, options,
resolver, unsatisfiedDependency);
lookupOptions, resolver, unsatisfiedDependency);
}

comp->setValue(currentDecl);
Expand Down Expand Up @@ -1150,7 +1175,8 @@ static Type resolveNestedIdentTypeComponent(
}

Type ty = diagnoseUnknownType(TC, DC, parentTy, parentRange, comp, options,
resolver, unsatisfiedDependency);
lookupOptions, resolver,
unsatisfiedDependency);
if (!ty || ty->is<ErrorType>()) {
return ErrorType::get(TC.Context);
}
Expand Down
21 changes: 18 additions & 3 deletions test/Sema/accessibility_private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension Container {

// FIXME: Why do these errors happen twice?
var extensionInner: PrivateInner? { return nil } // FIXME expected-error 2 {{use of undeclared type 'PrivateInner'}}
var extensionInnerQualified: Container.PrivateInner? { return nil } // FIXME expected-error 2 {{'PrivateInner' is not a member type of 'Container'}}
var extensionInnerQualified: Container.PrivateInner? { return nil } // expected-error 2 {{'PrivateInner' is inaccessible due to 'private' protection level}}
}

extension Container.Inner {
Expand All @@ -87,7 +87,7 @@ extension Container.Inner {

// FIXME: Why do these errors happen twice?
var inner: PrivateInner? { return nil } // FIXME expected-error 2 {{use of undeclared type 'PrivateInner'}}
var innerQualified: Container.PrivateInner? { return nil } // FIXME expected-error 2 {{'PrivateInner' is not a member type of 'Container'}}
var innerQualified: Container.PrivateInner? { return nil } // expected-error 2 {{'PrivateInner' is inaccessible due to 'private' protection level}}
}

class Sub : Container {
Expand All @@ -108,7 +108,7 @@ class Sub : Container {
}

var subInner: PrivateInner? // FIXME expected-error {{use of undeclared type 'PrivateInner'}}
var subInnerQualified: Container.PrivateInner? // FIXME expected-error {{'PrivateInner' is not a member type of 'Container'}}
var subInnerQualified: Container.PrivateInner? // expected-error {{'PrivateInner' is inaccessible due to 'private' protection level}}
}


Expand Down Expand Up @@ -145,3 +145,18 @@ private class VIPPrivateSetPropBase {
private class VIPPrivateSetPropSub : VIPPrivateSetPropBase, VeryImportantProto {
typealias Assoc = Int
}

extension Container {
private typealias ExtensionConflictingType = Int // expected-note * {{declared here}}
}
extension Container {
private typealias ExtensionConflictingType = Double // expected-note * {{declared here}}
}
extension Container {
func test() {
let a: ExtensionConflictingType? = nil // FIXME expected-error {{use of undeclared type 'ExtensionConflictingType'}}
let b: Container.ExtensionConflictingType? = nil // expected-error {{'ExtensionConflictingType' is inaccessible due to 'private' protection level}}
_ = ExtensionConflictingType() // FIXME expected-error {{use of unresolved identifier 'ExtensionConflictingType'}}
_ = Container.ExtensionConflictingType() // expected-error {{'ExtensionConflictingType' is inaccessible due to 'private' protection level}}
}
}