Skip to content

[6.2] MemberImportVisibility bug fixes #80567

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
40 changes: 33 additions & 7 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,8 @@ static bool isAcceptableLookupResult(const DeclContext *dc, NLOptions options,
ValueDecl *decl,
bool onlyCompleteObjectInits,
bool requireImport) {
auto &ctx = dc->getASTContext();

// Filter out designated initializers, if requested.
if (onlyCompleteObjectInits) {
if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
Expand All @@ -2405,19 +2407,43 @@ static bool isAcceptableLookupResult(const DeclContext *dc, NLOptions options,
}

// Check access.
if (!(options & NL_IgnoreAccessControl) &&
!dc->getASTContext().isAccessControlDisabled()) {
if (!(options & NL_IgnoreAccessControl) && !ctx.isAccessControlDisabled()) {
bool allowUsableFromInline = options & NL_IncludeUsableFromInline;
if (!decl->isAccessibleFrom(dc, /*forConformance*/ false,
allowUsableFromInline))
return false;
}

// Check that there is some import in the originating context that makes this
// decl visible.
if (requireImport && !(options & NL_IgnoreMissingImports))
if (!dc->isDeclImported(decl))
return false;
if (requireImport) {
// Check that there is some import in the originating context that makes
// this decl visible.
if (!(options & NL_IgnoreMissingImports)) {
if (!dc->isDeclImported(decl))
return false;
}

// Unlike in Swift, Obj-C allows method overrides to be declared in
// extensions (categories), even outside of the module that defines the
// type that is being extended. When MemberImportVisibility is enabled,
// if these overrides are not filtered out they can hijack name
// lookup and cause the compiler to insist that the module that defines
// the extension be imported, contrary to developer expectations.
//
// Filter results belonging to these extensions out, even when ignoring
// missing imports, if we're in a context that requires imports to access
// member declarations.
if (decl->getOverriddenDecl()) {
if (auto *extension = dyn_cast<ExtensionDecl>(decl->getDeclContext())) {
if (auto *nominal = extension->getExtendedNominal()) {
auto extensionMod = extension->getModuleContext();
auto nominalMod = nominal->getModuleContext();
if (!extensionMod->isSameModuleLookingThroughOverlays(nominalMod) &&
!dc->isDeclImported(extension))
return false;
}
}
}
}

// Check that it has the appropriate ABI role.
if (!ABIRoleInfo(decl).matchesOptions(options))
Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,7 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
new (Context) UnresolvedDotExpr(InterpolationVarRef,
/*dotloc=*/SourceLoc(),
appendLiteral,
/*nameloc=*/DeclNameLoc(),
/*nameloc=*/DeclNameLoc(TokenLoc),
/*Implicit=*/true);
auto *ArgList = ArgumentList::forImplicitUnlabeled(Context, {Literal});
auto AppendLiteralCall =
Expand Down
15 changes: 9 additions & 6 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4602,8 +4602,9 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
FuncDecl *makeIterator = isAsync ? ctx.getAsyncSequenceMakeAsyncIterator()
: ctx.getSequenceMakeIterator();

auto *makeIteratorRef = UnresolvedDotExpr::createImplicit(
ctx, sequenceExpr, makeIterator->getName());
auto *makeIteratorRef = new (ctx) UnresolvedDotExpr(
sequenceExpr, SourceLoc(), DeclNameRef(makeIterator->getName()),
DeclNameLoc(stmt->getForLoc()), /*implicit=*/true);
makeIteratorRef->setFunctionRefInfo(FunctionRefInfo::singleBaseNameApply());

Expr *makeIteratorCall =
Expand Down Expand Up @@ -4666,11 +4667,13 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
TinyPtrVector<Identifier> labels;
if (nextFn && nextFn->getParameters()->size() == 1)
labels.push_back(ctx.Id_isolation);
auto *nextRef = UnresolvedDotExpr::createImplicit(
ctx,
auto *makeIteratorVarRef =
new (ctx) DeclRefExpr(makeIteratorVar, DeclNameLoc(stmt->getForLoc()),
/*Implicit=*/true),
nextId, labels);
/*Implicit=*/true);
auto *nextRef = new (ctx)
UnresolvedDotExpr(makeIteratorVarRef, SourceLoc(),
DeclNameRef(DeclName(ctx, nextId, labels)),
DeclNameLoc(stmt->getForLoc()), /*implicit=*/true);
nextRef->setFunctionRefInfo(FunctionRefInfo::singleBaseNameApply());

ArgumentList *nextArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ enum Tree : Differentiable & AdditiveArithmetic {
// (`Collection.makeIterator` and `IteratorProtocol.next`).
// expected-error @+1 {{function is not differentiable}}
@differentiable(reverse)
// expected-note @+2 {{when differentiating this function definition}}
// expected-note @+1 {{cannot differentiate through a non-differentiable result; do you want to use 'withoutDerivative(at:)'?}} {{+2:12-12=withoutDerivative(at: }} {{+2:17-17=)}}
func loop_array(_ array: [Float]) -> Float {
// expected-note@-1 {{when differentiating this function definition}}
var result: Float = 1
for x in array {
// expected-note@-1 {{cannot differentiate through a non-differentiable result; do you want to use 'withoutDerivative(at:)'}}
result = result * x
}
return result
Expand Down
3 changes: 3 additions & 0 deletions test/Concurrency/async_sequence_existential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

// REQUIRES: concurrency

// https://github.com/swiftlang/swift/issues/80582
// UNSUPPORTED: OS=windows-msvc

extension Error {
func printMe() { }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
@import Foundation;

@interface X
@interface Base
- (void)overriddenInOverlayForA __attribute__((deprecated("Categories_A.h")));
- (void)overriddenInOverlayForB __attribute__((deprecated("Categories_A.h")));
- (void)overriddenInOverlayForC __attribute__((deprecated("Categories_A.h")));
- (void)overriddenInSubclassInOverlayForC __attribute__((deprecated("Categories_A.h")));
@end

@interface X : Base
@end

@interface X (A)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
extension X {
public func fromOverlayForA() {}
@objc public func fromOverlayForAObjC() {}

@available(*, deprecated, message: "Categories_A.swift")
public override func overriddenInOverlayForA() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
extension X {
public func fromOverlayForB() {}
@objc public func fromOverlayForBObjC() {}

@available(*, deprecated, message: "Categories_B.swift")
public override func overriddenInOverlayForB() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
@interface X (C)
- (void)fromC;
@end

@interface SubclassFromC : X
- (instancetype)init;
@end
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
extension X {
public func fromOverlayForC() {}
@objc public func fromOverlayForCObjC() {}

@available(*, deprecated, message: "Categories_C.swift")
public override func overriddenInOverlayForC() {}
}

extension SubclassFromC {
@available(*, deprecated, message: "Categories_C.swift")
public override func overriddenInSubclassInOverlayForC() {}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import Categories_C
import Categories_D.Submodule

public func makeSubclassFromC() -> SubclassFromC { SubclassFromC() }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import Root;

@interface BranchObject : RootObject
- (void)overridden1 __attribute__((deprecated("Branch.h")));
@end

@interface BranchObject (Branch)
- (void)overridden3 __attribute__((deprecated("Branch.h")));
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import Branch;

@interface FruitObject : BranchObject
- (void)overridden1 __attribute__((deprecated("Fruit.h")));
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@import Branch;

@interface LeafObject : BranchObject
- (void)overridden1 __attribute__((deprecated("Leaf.h")));
@end

@interface BranchObject (Leaf)
- (void)overridden2 __attribute__((deprecated("Leaf.h")));
@end

@interface LeafObject (Leaf)
- (void)overridden3 __attribute__((deprecated("Leaf.h")));
@end

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@interface RootObject
- (instancetype)init;
- (void)overridden1 __attribute__((deprecated("Root.h")));
- (void)overridden2 __attribute__((deprecated("Root.h")));
@end

@interface RootObject (Root)
- (void)overridden3 __attribute__((deprecated("Root.h")));
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Root {
header "Root.h"
export *
}

module Branch {
header "Branch.h"
export *
}

module Leaf {
header "Leaf.h"
export *
}

module Fruit {
header "Fruit.h"
export *
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public enum EnumInA {
}

open class BaseClassInA {
public init() {}
open func methodInA() {}
open func overriddenMethod() {}
}

public protocol ProtocolInA {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ package enum EnumInB_package {

open class DerivedClassInB: BaseClassInA {
open func methodInB() {}
open override func overriddenMethod() {}
}

extension ProtocolInA {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public enum EnumInC {

open class DerivedClassInC: DerivedClassInB {
open func methodInC() {}
open override func overriddenMethod() {}
public func asDerivedClassInB() -> DerivedClassInB { return self }
}

extension ProtocolInA {
Expand Down
24 changes: 19 additions & 5 deletions test/NameLookup/members_transitive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// REQUIRES: swift_feature_MemberImportVisibility

import members_C
// expected-member-visibility-note 16{{add import of module 'members_B'}}{{1-1=internal import members_B\n}}
// expected-member-visibility-note 18{{add import of module 'members_B'}}{{1-1=internal import members_B\n}}


func testExtensionMembers(x: X, y: Y<Z>) {
Expand Down Expand Up @@ -96,8 +96,22 @@ class DerivedFromClassInC: DerivedClassInC {

struct ConformsToProtocolInA: ProtocolInA {} // expected-member-visibility-error{{type 'ConformsToProtocolInA' does not conform to protocol 'ProtocolInA'}} expected-member-visibility-note {{add stubs for conformance}}

func testDerivedMethodAccess() {
DerivedClassInC().methodInC()
DerivedClassInC().methodInB() // expected-member-visibility-error{{instance method 'methodInB()' is not available due to missing import of defining module 'members_B'}}
DerivedFromClassInC().methodInB()
func testInheritedMethods(
a: BaseClassInA,
c: DerivedClassInC,
) {
let b = c.asDerivedClassInB()

a.methodInA()
b.methodInA()
c.methodInA()

b.methodInB() // expected-member-visibility-error{{instance method 'methodInB()' is not available due to missing import of defining module 'members_B'}}
c.methodInB() // expected-member-visibility-error{{instance method 'methodInB()' is not available due to missing import of defining module 'members_B'}}

c.methodInC()

a.overriddenMethod()
b.overriddenMethod() // expected-member-visibility-error{{instance method 'overriddenMethod()' is not available due to missing import of defining module 'members_B'}}
c.overriddenMethod()
}
Loading