Skip to content

Commit cb444b6

Browse files
authored
Merge pull request #80567 from tshortli/module-import-visibility-fixes-6.2
[6.2] `MemberImportVisibility` bug fixes
2 parents 65e0f65 + 82abf98 commit cb444b6

23 files changed

+573
-26
lines changed

lib/AST/NameLookup.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,8 @@ static bool isAcceptableLookupResult(const DeclContext *dc, NLOptions options,
23882388
ValueDecl *decl,
23892389
bool onlyCompleteObjectInits,
23902390
bool requireImport) {
2391+
auto &ctx = dc->getASTContext();
2392+
23912393
// Filter out designated initializers, if requested.
23922394
if (onlyCompleteObjectInits) {
23932395
if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
@@ -2405,19 +2407,43 @@ static bool isAcceptableLookupResult(const DeclContext *dc, NLOptions options,
24052407
}
24062408

24072409
// Check access.
2408-
if (!(options & NL_IgnoreAccessControl) &&
2409-
!dc->getASTContext().isAccessControlDisabled()) {
2410+
if (!(options & NL_IgnoreAccessControl) && !ctx.isAccessControlDisabled()) {
24102411
bool allowUsableFromInline = options & NL_IncludeUsableFromInline;
24112412
if (!decl->isAccessibleFrom(dc, /*forConformance*/ false,
24122413
allowUsableFromInline))
24132414
return false;
24142415
}
24152416

2416-
// Check that there is some import in the originating context that makes this
2417-
// decl visible.
2418-
if (requireImport && !(options & NL_IgnoreMissingImports))
2419-
if (!dc->isDeclImported(decl))
2420-
return false;
2417+
if (requireImport) {
2418+
// Check that there is some import in the originating context that makes
2419+
// this decl visible.
2420+
if (!(options & NL_IgnoreMissingImports)) {
2421+
if (!dc->isDeclImported(decl))
2422+
return false;
2423+
}
2424+
2425+
// Unlike in Swift, Obj-C allows method overrides to be declared in
2426+
// extensions (categories), even outside of the module that defines the
2427+
// type that is being extended. When MemberImportVisibility is enabled,
2428+
// if these overrides are not filtered out they can hijack name
2429+
// lookup and cause the compiler to insist that the module that defines
2430+
// the extension be imported, contrary to developer expectations.
2431+
//
2432+
// Filter results belonging to these extensions out, even when ignoring
2433+
// missing imports, if we're in a context that requires imports to access
2434+
// member declarations.
2435+
if (decl->getOverriddenDecl()) {
2436+
if (auto *extension = dyn_cast<ExtensionDecl>(decl->getDeclContext())) {
2437+
if (auto *nominal = extension->getExtendedNominal()) {
2438+
auto extensionMod = extension->getModuleContext();
2439+
auto nominalMod = nominal->getModuleContext();
2440+
if (!extensionMod->isSameModuleLookingThroughOverlays(nominalMod) &&
2441+
!dc->isDeclImported(extension))
2442+
return false;
2443+
}
2444+
}
2445+
}
2446+
}
24212447

24222448
// Check that it has the appropriate ABI role.
24232449
if (!ABIRoleInfo(decl).matchesOptions(options))

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
19721972
new (Context) UnresolvedDotExpr(InterpolationVarRef,
19731973
/*dotloc=*/SourceLoc(),
19741974
appendLiteral,
1975-
/*nameloc=*/DeclNameLoc(),
1975+
/*nameloc=*/DeclNameLoc(TokenLoc),
19761976
/*Implicit=*/true);
19771977
auto *ArgList = ArgumentList::forImplicitUnlabeled(Context, {Literal});
19781978
auto AppendLiteralCall =

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4602,8 +4602,9 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
46024602
FuncDecl *makeIterator = isAsync ? ctx.getAsyncSequenceMakeAsyncIterator()
46034603
: ctx.getSequenceMakeIterator();
46044604

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

46094610
Expr *makeIteratorCall =
@@ -4666,11 +4667,13 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
46664667
TinyPtrVector<Identifier> labels;
46674668
if (nextFn && nextFn->getParameters()->size() == 1)
46684669
labels.push_back(ctx.Id_isolation);
4669-
auto *nextRef = UnresolvedDotExpr::createImplicit(
4670-
ctx,
4670+
auto *makeIteratorVarRef =
46714671
new (ctx) DeclRefExpr(makeIteratorVar, DeclNameLoc(stmt->getForLoc()),
4672-
/*Implicit=*/true),
4673-
nextId, labels);
4672+
/*Implicit=*/true);
4673+
auto *nextRef = new (ctx)
4674+
UnresolvedDotExpr(makeIteratorVarRef, SourceLoc(),
4675+
DeclNameRef(DeclName(ctx, nextId, labels)),
4676+
DeclNameLoc(stmt->getForLoc()), /*implicit=*/true);
46744677
nextRef->setFunctionRefInfo(FunctionRefInfo::singleBaseNameApply());
46754678

46764679
ArgumentList *nextArgs;

test/AutoDiff/SILOptimizer/differentiation_control_flow_diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ enum Tree : Differentiable & AdditiveArithmetic {
168168
// (`Collection.makeIterator` and `IteratorProtocol.next`).
169169
// expected-error @+1 {{function is not differentiable}}
170170
@differentiable(reverse)
171-
// expected-note @+2 {{when differentiating this function definition}}
172-
// 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=)}}
173171
func loop_array(_ array: [Float]) -> Float {
172+
// expected-note@-1 {{when differentiating this function definition}}
174173
var result: Float = 1
175174
for x in array {
175+
// expected-note@-1 {{cannot differentiate through a non-differentiable result; do you want to use 'withoutDerivative(at:)'}}
176176
result = result * x
177177
}
178178
return result

test/Concurrency/async_sequence_existential.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
// REQUIRES: concurrency
66

7+
// https://github.com/swiftlang/swift/issues/80582
8+
// UNSUPPORTED: OS=windows-msvc
9+
710
extension Error {
811
func printMe() { }
912
}

test/NameLookup/Inputs/MemberImportVisibility/Categories_A.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
@import Foundation;
22

3-
@interface X
3+
@interface Base
4+
- (void)overriddenInOverlayForA __attribute__((deprecated("Categories_A.h")));
5+
- (void)overriddenInOverlayForB __attribute__((deprecated("Categories_A.h")));
6+
- (void)overriddenInOverlayForC __attribute__((deprecated("Categories_A.h")));
7+
- (void)overriddenInSubclassInOverlayForC __attribute__((deprecated("Categories_A.h")));
8+
@end
9+
10+
@interface X : Base
411
@end
512

613
@interface X (A)

test/NameLookup/Inputs/MemberImportVisibility/Categories_A.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
extension X {
44
public func fromOverlayForA() {}
55
@objc public func fromOverlayForAObjC() {}
6+
7+
@available(*, deprecated, message: "Categories_A.swift")
8+
public override func overriddenInOverlayForA() {}
69
}

test/NameLookup/Inputs/MemberImportVisibility/Categories_B.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
extension X {
44
public func fromOverlayForB() {}
55
@objc public func fromOverlayForBObjC() {}
6+
7+
@available(*, deprecated, message: "Categories_B.swift")
8+
public override func overriddenInOverlayForB() {}
69
}

test/NameLookup/Inputs/MemberImportVisibility/Categories_C.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
@interface X (C)
44
- (void)fromC;
55
@end
6+
7+
@interface SubclassFromC : X
8+
- (instancetype)init;
9+
@end

test/NameLookup/Inputs/MemberImportVisibility/Categories_C.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@
33
extension X {
44
public func fromOverlayForC() {}
55
@objc public func fromOverlayForCObjC() {}
6+
7+
@available(*, deprecated, message: "Categories_C.swift")
8+
public override func overriddenInOverlayForC() {}
9+
}
10+
11+
extension SubclassFromC {
12+
@available(*, deprecated, message: "Categories_C.swift")
13+
public override func overriddenInSubclassInOverlayForC() {}
614
}

0 commit comments

Comments
 (0)