Skip to content

Commit 8ece214

Browse files
committed
Push GenericParamList check into function case.
1 parent 3b7131c commit 8ece214

File tree

2 files changed

+82
-62
lines changed

2 files changed

+82
-62
lines changed

include/swift/AST/NameLookup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ inline UnqualifiedLookup::Options operator|(UnqualifiedLookup::Flags flag1,
207207
DeclContext *BaseDC;
208208
DeclContext *MetaBaseDC;
209209
Optional<bool> isCascadingUse;
210-
GenericParamList *GenericParams;
211210

212211
void dump() const;
213212
};

lib/AST/NameLookup.cpp

Lines changed: 82 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,6 @@ namespace {
16531653

16541654
DeclContext *BaseDC = nullptr;
16551655
DeclContext *MetaBaseDC = nullptr;
1656-
GenericParamList *GenericParams = nullptr;
16571656
LookupDecls lookupDecls;
16581657
breadcrumbs.push_back(results);
16591658

@@ -1662,7 +1661,6 @@ namespace {
16621661
BaseDC = results.BaseDC;
16631662
MetaBaseDC = results.MetaBaseDC;
16641663
isCascadingUse = results.isCascadingUse;
1665-
GenericParams = results.GenericParams;
16661664
switch (results.result) {
16671665
case ScopeLookupResult::next:
16681666
break;
@@ -1673,17 +1671,6 @@ namespace {
16731671
}
16741672
// UP TO HERE
16751673

1676-
// If we're inside a function context, we've already moved to
1677-
// the parent DC, so we have to check the function's generic
1678-
// parameters first.
1679-
if (GenericParams) {
1680-
namelookup::FindLocalVal localVal(SM, Loc, Consumer);
1681-
localVal.checkGenericParams(GenericParams);
1682-
1683-
if (isFinishedWithLookupNowThatIsAboutToLookForOuterResults())
1684-
return std::make_pair(ScopeLookupResult::finished, DC);
1685-
}
1686-
16871674
// Check the generic parameters of our context.
16881675
GenericParamList *dcGenericParams = nullptr;
16891676
if (auto nominal = dyn_cast<NominalTypeDecl>(DC))
@@ -1784,25 +1771,29 @@ namespace {
17841771
Consumer.foundDecl(selfParam, DeclVisibilityKind::FunctionParameter);
17851772
if (isFinishedWithLookupNowThatIsAboutToLookForOuterResults())
17861773
return PerScopeLookupState{
1787-
ScopeLookupResult::finished, PBI, LookupDecls{}, nullptr, nullptr,
1774+
ScopeLookupResult::finished,
1775+
PBI,
1776+
LookupDecls{},
1777+
nullptr,
1778+
nullptr,
17881779
// FACTOR THIS LINE
17891780
isCascadingUse.hasValue() ? isCascadingUse.getValue()
1790-
: PBI->isCascadingContextForLookup(false),
1791-
nullptr};
1792-
1781+
: PBI->isCascadingContextForLookup(false)
1782+
};
17931783
DeclContext *const parent = PBI->getParent();
17941784

17951785
LookupDecls lookupDecls;
17961786
populateLookupDeclsFromContext(parent, lookupDecls);
1797-
return PerScopeLookupState{ScopeLookupResult::next,
1787+
return PerScopeLookupState{
1788+
ScopeLookupResult::next,
17981789
parent,
17991790
std::move(lookupDecls),
18001791
PBI,
18011792
parent,
18021793
isCascadingUse.hasValue()
18031794
? isCascadingUse.getValue()
1804-
: DC->isCascadingContextForLookup(false),
1805-
nullptr};
1795+
: DC->isCascadingContextForLookup(false)
1796+
};
18061797
}
18071798
// Initializers for stored properties of types perform static
18081799
// lookup into the surrounding context.
@@ -1816,22 +1807,23 @@ namespace {
18161807
std::move(lookupDecls),
18171808
surroundingContext,
18181809
surroundingContext,
1819-
surroundingContext->isCascadingContextForLookup(false),
1820-
nullptr};
1810+
surroundingContext->isCascadingContextForLookup(false)
1811+
};
18211812
}
18221813
// Otherwise, we have an initializer for a global or local property.
18231814
// There's not much to find here, we'll keep going up to a parent
18241815
// context.
18251816

1826-
return PerScopeLookupState{ScopeLookupResult::next,
1817+
return PerScopeLookupState{
1818+
ScopeLookupResult::next,
18271819
PBI,
18281820
LookupDecls{},
18291821
nullptr,
18301822
nullptr,
18311823
isCascadingUse.hasValue()
18321824
? isCascadingUse.getValue()
1833-
: PBI->isCascadingContextForLookup(false),
1834-
nullptr};
1825+
: PBI->isCascadingContextForLookup(false)
1826+
};
18351827
}
18361828

18371829
PerScopeLookupState lookupInFunctionDecl(AbstractFunctionDecl *AFD,
@@ -1846,12 +1838,16 @@ namespace {
18461838
localVal.visit(AFD->getBody());
18471839
if (isFinishedWithLookupNowThatIsAboutToLookForOuterResults())
18481840
return PerScopeLookupState{
1849-
ScopeLookupResult::finished, AFD, LookupDecls{}, nullptr, nullptr,
1841+
ScopeLookupResult::finished,
1842+
AFD,
1843+
LookupDecls{},
1844+
nullptr,
1845+
nullptr,
18501846
// FACTOR INTO A FN
18511847
isCascadingUse.hasValue()
18521848
? isCascadingUse.getValue()
1853-
: !SM.rangeContainsTokenLoc(AFD->getBodySourceRange(), Loc),
1854-
nullptr};
1849+
: !SM.rangeContainsTokenLoc(AFD->getBodySourceRange(), Loc)
1850+
};
18551851

18561852
if (auto *P = AFD->getImplicitSelfDecl())
18571853
localVal.checkValueDecl(P, DeclVisibilityKind::FunctionParameter);
@@ -1865,8 +1861,8 @@ namespace {
18651861
nullptr,
18661862
isCascadingUse.hasValue()
18671863
? isCascadingUse.getValue()
1868-
: !SM.rangeContainsTokenLoc(AFD->getBodySourceRange(), Loc),
1869-
nullptr};
1864+
: !SM.rangeContainsTokenLoc(AFD->getBodySourceRange(), Loc)
1865+
};
18701866
}
18711867
const bool returnValueForIsCascadingUse =
18721868
AFD->isCascadingContextForLookup(false) &&
@@ -1880,8 +1876,13 @@ namespace {
18801876
populateLookupDeclsFromContext(AFD->getDeclContext(), lookupDecls);
18811877
DeclContext *const MetaBaseDC = AFD->getDeclContext();
18821878

1879+
addGenericParameters(AFD);
18831880
return PerScopeLookupState{
1884-
ScopeLookupResult::next, AFD->getParent(), std::move(lookupDecls),
1881+
isFinishedWithLookupNowThatIsAboutToLookForOuterResults()
1882+
? ScopeLookupResult::finished
1883+
: ScopeLookupResult::next,
1884+
AFD->getParent(),
1885+
std::move(lookupDecls),
18851886
// If we're not in the body of the function (for example, we
18861887
// might be type checking a default argument expression and
18871888
// performing name lookup from there), the base declaration
@@ -1891,16 +1892,34 @@ namespace {
18911892
!SM.rangeContainsTokenLoc(AFD->getBodySourceRange(), Loc)
18921893
? MetaBaseDC
18931894
: AFD,
1894-
MetaBaseDC, returnValueForIsCascadingUse, AFD->getGenericParams()};
1895+
MetaBaseDC,
1896+
returnValueForIsCascadingUse
1897+
};
18951898
}
18961899
// Look in the generic parameters after checking our local declaration.
1897-
return PerScopeLookupState{ScopeLookupResult::next,
1900+
addGenericParameters(AFD);
1901+
return PerScopeLookupState{
1902+
isFinishedWithLookupNowThatIsAboutToLookForOuterResults()
1903+
? ScopeLookupResult::finished
1904+
: ScopeLookupResult::next,
18981905
AFD,
18991906
LookupDecls{},
19001907
nullptr,
19011908
nullptr,
1902-
returnValueForIsCascadingUse,
1903-
AFD->getGenericParams()};
1909+
returnValueForIsCascadingUse
1910+
};
1911+
}
1912+
1913+
/// Consume generic parameters
1914+
void addGenericParameters(AbstractFunctionDecl *AFD) {
1915+
// If we're inside a function context, we've already moved to
1916+
// the parent DC, so we have to check the function's generic
1917+
// parameters first.
1918+
GenericParamList *GenericParams = AFD->getGenericParams();
1919+
if (GenericParams) {
1920+
namelookup::FindLocalVal localVal(SM, Loc, Consumer);
1921+
localVal.checkGenericParams(GenericParams);
1922+
}
19041923
}
19051924

19061925
PerScopeLookupState lookupInClosure(AbstractClosureExpr *ACE,
@@ -1913,97 +1932,104 @@ namespace {
19131932
if (auto body = CE->getBody())
19141933
localVal.visit(body);
19151934
if (isFinishedWithLookupNowThatIsAboutToLookForOuterResults()) {
1916-
return PerScopeLookupState{ScopeLookupResult::finished,
1935+
return PerScopeLookupState{
1936+
ScopeLookupResult::finished,
19171937
ACE,
19181938
LookupDecls{},
19191939
nullptr,
19201940
nullptr,
1921-
isCascadingUse,
1922-
nullptr};
1941+
isCascadingUse
1942+
};
19231943
}
19241944
if (auto params = CE->getParameters())
19251945
localVal.checkParameterList(params);
19261946
if (isFinishedWithLookupNowThatIsAboutToLookForOuterResults()) {
1927-
return PerScopeLookupState{ScopeLookupResult::finished,
1947+
return PerScopeLookupState{
1948+
ScopeLookupResult::finished,
19281949
ACE,
19291950
LookupDecls{},
19301951
nullptr,
19311952
nullptr,
1932-
isCascadingUse,
1933-
nullptr};
1953+
isCascadingUse
1954+
};
19341955
}
19351956
}
19361957
}
1937-
return PerScopeLookupState{ScopeLookupResult::next,
1958+
return PerScopeLookupState{
1959+
ScopeLookupResult::next,
19381960
DC,
19391961
LookupDecls{},
19401962
nullptr,
19411963
nullptr,
19421964
isCascadingUse.hasValue()
19431965
? isCascadingUse.getValue()
1944-
: ACE->isCascadingContextForLookup(false),
1945-
nullptr};
1966+
: ACE->isCascadingContextForLookup(false)
1967+
};
19461968
}
19471969

19481970
PerScopeLookupState lookupInExtension(ExtensionDecl *ED,
19491971
Optional<bool> isCascadingUse) {
19501972
LookupDecls lookupDecls;
19511973
if (shouldLookupMembers(ED, Loc))
19521974
populateLookupDeclsFromContext(ED, lookupDecls);
1953-
return PerScopeLookupState{ScopeLookupResult::next,
1975+
return PerScopeLookupState{
1976+
ScopeLookupResult::next,
19541977
DC,
19551978
std::move(lookupDecls),
19561979
ED,
19571980
ED,
19581981
isCascadingUse.hasValue()
19591982
? isCascadingUse.getValue()
1960-
: ED->isCascadingContextForLookup(false),
1961-
nullptr};
1983+
: ED->isCascadingContextForLookup(false)
1984+
};
19621985
}
19631986

19641987
PerScopeLookupState lookupInNominalType(NominalTypeDecl *ND,
19651988
Optional<bool> isCascadingUse) {
19661989
LookupDecls lookupDecls;
19671990
if (shouldLookupMembers(ND, Loc))
19681991
populateLookupDeclsFromContext(ND, lookupDecls);
1969-
return PerScopeLookupState{ScopeLookupResult::next,
1992+
return PerScopeLookupState{
1993+
ScopeLookupResult::next,
19701994
DC,
19711995
std::move(lookupDecls),
19721996
DC,
19731997
DC,
19741998
isCascadingUse.hasValue()
19751999
? isCascadingUse.getValue()
1976-
: ND->isCascadingContextForLookup(false),
1977-
nullptr};
2000+
: ND->isCascadingContextForLookup(false)
2001+
};
19782002
}
19792003

19802004
PerScopeLookupState
19812005
lookupInDefaultArgumentInitializer(DefaultArgumentInitializer *I,
19822006
Optional<bool> isCascadingUse) {
19832007
// In a default argument, skip immediately out of both the
19842008
// initializer and the function.
1985-
return PerScopeLookupState{ScopeLookupResult::stop,
2009+
return PerScopeLookupState{
2010+
ScopeLookupResult::stop,
19862011
I->getParent()->getParent(),
19872012
LookupDecls{},
19882013
nullptr,
19892014
nullptr,
1990-
false,
1991-
nullptr};
2015+
false
2016+
};
19922017
}
19932018

19942019
PerScopeLookupState lookupInMiscContext(DeclContext *DC,
19952020
Optional<bool> isCascadingUse) {
19962021
assert(isa<TopLevelCodeDecl>(DC) || isa<Initializer>(DC) ||
19972022
isa<TypeAliasDecl>(DC) || isa<SubscriptDecl>(DC));
1998-
return PerScopeLookupState{ScopeLookupResult::next,
2023+
return PerScopeLookupState{
2024+
ScopeLookupResult::next,
19992025
DC,
20002026
LookupDecls{},
20012027
nullptr,
20022028
nullptr,
20032029
isCascadingUse.hasValue()
20042030
? isCascadingUse.getValue()
2005-
: DC->isCascadingContextForLookup(false),
2006-
nullptr};
2031+
: DC->isCascadingContextForLookup(false)
2032+
};
20072033
}
20082034

20092035
bool addLocalVariableResults() {
@@ -2172,11 +2198,6 @@ void ExpUnqualifiedLookup::PerScopeLookupState::dump() const {
21722198
for (const auto *D : foundDecls)
21732199
D->dump();
21742200
e << "\n";
2175-
if (GenericParams) {
2176-
e << "GenericParams: ";
2177-
GenericParams->print(e);
2178-
e << "\n";
2179-
}
21802201
}
21812202

21822203
#pragma mark Member lookup table

0 commit comments

Comments
 (0)