Skip to content

Commit 74e5da2

Browse files
authored
Merge pull request #66997 from DougGregor/observation-shadowing
2 parents 4331d2f + 81acd82 commit 74e5da2

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

include/swift/AST/KnownIdentifiers.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ IDENTIFIER(objectAtIndexedSubscript)
125125
IDENTIFIER(objectForKeyedSubscript)
126126
IDENTIFIER(ObjectiveC)
127127
IDENTIFIER_(ObjectiveCType)
128+
IDENTIFIER(Observation)
129+
IDENTIFIER_WITH_NAME(Observation_, "_Observation")
128130
IDENTIFIER(oldValue)
129131
IDENTIFIER(Optional)
130132
IDENTIFIER_(OptionalNilComparisonType)

lib/AST/NameLookup.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,25 @@ static void recordShadowedDeclsAfterTypeMatch(
680680
}
681681
}
682682

683+
// Next, prefer any other module over the (_)Observation module.
684+
auto obsModule = ctx.getLoadedModule(ctx.Id_Observation);
685+
if (!obsModule)
686+
obsModule = ctx.getLoadedModule(ctx.Id_Observation_);
687+
if (obsModule) {
688+
if ((firstModule == obsModule) != (secondModule == obsModule)) {
689+
// If second module is (_)Observation, then it is shadowed by
690+
// first.
691+
if (secondModule == obsModule) {
692+
shadowed.insert(secondDecl);
693+
continue;
694+
}
695+
696+
// Otherwise, the first declaration is shadowed by the second.
697+
shadowed.insert(firstDecl);
698+
break;
699+
}
700+
}
701+
683702
// The Foundation overlay introduced Data.withUnsafeBytes, which is
684703
// treated as being ambiguous with SwiftNIO's Data.withUnsafeBytes
685704
// extension. Apply a special-case name shadowing rule to use the
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public enum Observable<Value> {
2+
case just(Value)
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@_exported import _Observation
2+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/HasObservable.swift
3+
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/ReexportsObservation.swift
4+
// RUN: %target-swift-frontend -typecheck %s -I %t -verify
5+
// RUN: %target-swift-frontend -typecheck %s -I %t -verify -DVIA_REEXPORT
6+
7+
// REQUIRES: observation
8+
9+
import HasObservable
10+
11+
#if VIA_REEXPORT
12+
import ReexportsObservation
13+
#else
14+
import _Observation
15+
#endif
16+
17+
func foo() -> Observable<Int> {
18+
return .just(42)
19+
}

0 commit comments

Comments
 (0)