Skip to content

Commit a82ca01

Browse files
committed
[Module-scope lookup] Don't expand macros associated with members of types
The module-scope lookup tables use the same code for adding module-scope declarations as for adding member operators, which are found via "global" operator lookup. This requires us to expand macros that can produce members of types, which violates the outside-in expansion rule described in the proposals. Stop recording member-producing macros, whether they are peer macros applied to member declarations or are freestanding declaration macros within a member context. This re-establishes the outside-in expansion rule. It also means that member operators introduced by macro expansion won't be found by global operator lookup, which is a (necessary) semantic change. (cherry picked from commit 343947a)
1 parent 84fba88 commit a82ca01

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

lib/AST/Module.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
249249
// Cache the value under both its compound name and its full name.
250250
TopLevelValues.add(VD);
251251

252-
if (VD->getAttrs().hasAttribute<CustomAttr>()) {
252+
if (!onlyOperators && VD->getAttrs().hasAttribute<CustomAttr>()) {
253253
MayHaveAuxiliaryDecls.push_back(VD);
254254
}
255255
}
@@ -279,8 +279,10 @@ void SourceLookupCache::addToUnqualifiedLookupCache(Range decls,
279279
else if (auto *PG = dyn_cast<PrecedenceGroupDecl>(D))
280280
PrecedenceGroups[PG->getName()].push_back(PG);
281281

282-
else if (auto *MED = dyn_cast<MacroExpansionDecl>(D))
283-
MayHaveAuxiliaryDecls.push_back(MED);
282+
else if (auto *MED = dyn_cast<MacroExpansionDecl>(D)) {
283+
if (!onlyOperators)
284+
MayHaveAuxiliaryDecls.push_back(MED);
285+
}
284286
}
285287
}
286288

@@ -403,13 +405,6 @@ void SourceLookupCache::populateAuxiliaryDeclCache() {
403405
for (auto macroNames : introducedNames) {
404406
auto macroRef = macroNames.getFirst();
405407
for (auto name : macroNames.getSecond()) {
406-
407-
// If this macro isn't in a module-scope context, and the introduced
408-
// name isn't an operator, we shouldn't be able to see it.
409-
if (!decl->getDeclContext()->isModuleScopeContext() &&
410-
!name.getBaseName().isOperator())
411-
continue;
412-
413408
auto *placeholder = MissingDecl::forUnexpandedMacro(macroRef, decl);
414409
name.addToLookupTable(TopLevelAuxiliaryDecls, placeholder);
415410
}
@@ -492,12 +487,6 @@ void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
492487
for (auto *unexpandedDecl : unexpandedDecls) {
493488
unexpandedDecl->forEachMacroExpandedDecl(
494489
[&](ValueDecl *decl) {
495-
// If the declaration is not a module-scope declaration, and
496-
// isn't an operator, ignore it.
497-
if (!decl->getDeclContext()->isModuleScopeContext() &&
498-
!decl->getName().getBaseName().isOperator())
499-
return;
500-
501490
if (decl->getName().matchesRef(Name)) {
502491
if (macroExpandedDecls.insert(decl).second)
503492
Result.push_back(decl);

test/Macros/macro_expand.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,15 +467,11 @@ struct HasEqualsSelf4 {
467467
func testHasEqualsSelf(
468468
x: HasEqualsSelf, y: HasEqualsSelf2, z: HasEqualsSelf3, w: HasEqualsSelf4
469469
) {
470-
_ = (x == true)
471-
_ = (y == true)
472470
#if TEST_DIAGNOSTICS
473-
// FIXME: This is technically a bug, because we should be able to find the
474-
// == operator introduced through a member operator. However, we might
475-
// want to change the rule rather than implement this.
476-
_ = (z == true) // expected-error{{binary operator '==' cannot be applied to operands}}
477-
// expected-note@-1{{overloads for '==' exist with these partially matching parameter lists}}
478-
_ = (w == true) // expected-error{{binary operator '==' cannot be applied to operands}}
479-
// expected-note@-1{{overloads for '==' exist with these partially matching parameter lists}}
471+
// Global operator lookup doesn't find member operators introduced by macros.
472+
_ = (x == true) // expected-error{{cannot convert value of type}}
473+
_ = (y == true) // expected-error{{cannot convert value of type}}
474+
_ = (z == true) // expected-error{{cannot convert value of type}}
475+
_ = (w == true) // expected-error{{cannot convert value of type}}
480476
#endif
481477
}

0 commit comments

Comments
 (0)