Skip to content

Commit b35aaef

Browse files
committed
Handle module selectors in macro lookups
1 parent f43bd72 commit b35aaef

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8010,7 +8010,7 @@ ERROR(expected_macro_expansion_expr,PointsToFirstBadToken,
80108010
ERROR(expected_macro_expansion_decls,PointsToFirstBadToken,
80118011
"expected macro expansion to produce a declaration", ())
80128012
ERROR(macro_undefined,PointsToFirstBadToken,
8013-
"no macro named %0", (DeclName))
8013+
"no macro named %0", (DeclNameRef))
80148014
ERROR(external_macro_not_found,none,
80158015
"external macro implementation type '%0.%1' could not be found for "
80168016
"macro %2; %3", (StringRef, StringRef, DeclName, StringRef))

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ namespace {
14401440
if (!protocol)
14411441
return Type();
14421442

1443-
auto macroIdent = ctx.getIdentifier(kind);
1443+
auto macroIdent = DeclNameRef(ctx.getIdentifier(kind));
14441444
auto macros = lookupMacros(Identifier(), macroIdent,
14451445
FunctionRefInfo::unappliedBaseName(),
14461446
MacroRole::Expression);
@@ -4111,12 +4111,12 @@ namespace {
41114111

41124112
/// Lookup all macros with the given macro name.
41134113
SmallVector<OverloadChoice, 1> lookupMacros(DeclName moduleName,
4114-
DeclName macroName,
4114+
DeclNameRef macroName,
41154115
FunctionRefInfo functionRefInfo,
41164116
MacroRoles roles) {
41174117
SmallVector<OverloadChoice, 1> choices;
41184118
auto results = namelookup::lookupMacros(CurDC, DeclNameRef(moduleName),
4119-
DeclNameRef(macroName), roles);
4119+
macroName, roles);
41204120
for (const auto &result : results) {
41214121
// Ignore invalid results. This matches the OverloadedDeclRefExpr
41224122
// logic.
@@ -4143,7 +4143,7 @@ namespace {
41434143

41444144
// Look up the macros with this name.
41454145
auto moduleIdent = expr->getModuleName().getBaseName();
4146-
auto macroIdent = expr->getMacroName().getBaseName();
4146+
auto macroIdent = expr->getMacroName().withoutArgumentLabels(ctx);
41474147
FunctionRefInfo functionRefInfo = FunctionRefInfo::singleBaseNameApply();
41484148
auto macros = lookupMacros(moduleIdent, macroIdent, functionRefInfo,
41494149
expr->getMacroRoles());

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,8 +1495,11 @@ static Type diagnoseUnknownType(const TypeResolution &resolution,
14951495
if (parentType.isNull()) {
14961496
// Tailored diagnostic for custom attributes.
14971497
if (resolution.getOptions().is(TypeResolverContext::CustomAttr)) {
1498-
diags.diagnose(repr->getNameLoc(), diag::unknown_attr_name,
1499-
repr->getNameRef().getBaseIdentifier().str());
1498+
SmallString<64> scratch;
1499+
llvm::raw_svector_ostream scratchOS(scratch);
1500+
repr->getNameRef().printPretty(scratchOS);
1501+
1502+
diags.diagnose(repr->getNameLoc(), diag::unknown_attr_name, scratch);
15001503

15011504
return ErrorType::get(ctx);
15021505
}
@@ -5171,9 +5174,13 @@ TypeResolver::resolveDeclRefTypeRepr(DeclRefTypeRepr *repr,
51715174
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
51725175
// Tailored diagnostic for custom attributes.
51735176
if (options.is(TypeResolverContext::CustomAttr)) {
5177+
SmallString<64> scratch;
5178+
llvm::raw_svector_ostream scratchOS(scratch);
5179+
repr->getNameRef().printPretty(scratchOS);
5180+
51745181
auto &ctx = resolution.getASTContext();
51755182
ctx.Diags.diagnose(repr->getNameLoc(), diag::unknown_attr_name,
5176-
repr->getNameRef().getBaseIdentifier().str());
5183+
scratch);
51775184

51785185
return ErrorType::get(ctx);
51795186
}

test/NameLookup/Inputs/ModuleSelectorTestingKit.swiftinterface

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ public struct available {
4040
public struct MyBuilder {
4141
public static func buildBlock()
4242
}
43+
44+
@freestanding(expression) public macro ExprMacro() -> String = #file
45+
@attached(peer) public macro PeerMacro() = #externalMacro(module: "Fnord", type: "PeerMacro")

test/NameLookup/module_selector.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// * Cross-import overlays
1212
// * Key path dynamic member lookup
1313
// * Custom type attributes (and coverage of type attrs generally is sparse)
14-
// * Macros
1514
//
1615
// It also might not cover all combinations of name lookup paths and inputs.
1716

@@ -58,6 +57,8 @@ extension A: @retroactive Swift::Equatable {
5857

5958
_ = \ModuleSelectorTestingKit::A.magnitude
6059
_ = \A.ModuleSelectorTestingKit::magnitude
60+
61+
_ = #ModuleSelectorTestingKit::ExprMacro
6162
}
6263

6364
// FIXME: Can we test @convention(witness_method:)?
@@ -124,8 +125,14 @@ extension B: @retroactive main::Equatable {
124125
// FIXME improve: expected-error@-1 {{'main::A' in scope}} -- different diagnostic wording for legacy parser vs. ASTGen
125126
_ = \A.main::magnitude
126127
// FIXME improve: expected-error@-1 {{value of type 'A' has no member 'main::magnitude'}}
128+
129+
_ = #main::ExprMacro
130+
// expected-error@-1 {{no macro named 'main::ExprMacro'}}
127131
}
128132

133+
@main::PeerMacro func thingy() {}
134+
// expected-error@-1 {{unknown attribute 'main::PeerMacro'}}
135+
129136
// FIXME: Can we test @convention(witness_method:)?
130137
}
131138

@@ -184,8 +191,13 @@ extension C: @retroactive ModuleSelectorTestingKit::Equatable {
184191

185192
_ = \ModuleSelectorTestingKit::A.magnitude
186193
_ = \A.ModuleSelectorTestingKit::magnitude
194+
195+
_ = #ModuleSelectorTestingKit::ExprMacro
187196
}
188197

198+
@ModuleSelectorTestingKit::PeerMacro func thingy() {}
199+
// expected-error@-1 {{external macro implementation type 'Fnord.PeerMacro' could not be found for macro 'PeerMacro()'; plugin for module 'Fnord' not found}}
200+
189201
// FIXME: Can we test @convention(witness_method:)?
190202
}
191203

@@ -244,8 +256,14 @@ extension D: @retroactive Swift::Equatable {
244256
// FIXME improve: expected-error@-1 {{'Swift::A' in scope}} -- different diagnostic wording for legacy parser vs. ASTGen
245257
_ = \A.Swift::magnitude
246258
// FIXME improve: expected-error@-1 {{value of type 'A' has no member 'Swift::magnitude'}}
259+
260+
_ = #Swift::ExprMacro
261+
// expected-error@-1 {{no macro named 'Swift::ExprMacro'}}
247262
}
248263

264+
@Swift::PeerMacro func thingy() {}
265+
// expected-error@-1 {{unknown attribute 'Swift::PeerMacro'}}
266+
249267
// FIXME: Can we test @convention(witness_method:)?
250268
}
251269

@@ -262,25 +280,25 @@ struct AvailableUser {
262280
@available(macOS 10.15, *) var use1: String { "foo" }
263281

264282
@main::available() var use2
265-
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
283+
// FIXME improve: expected-error@-1 {{unknown attribute 'main::available'}}
266284
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
267285

268286
@ModuleSelectorTestingKit::available() var use4
269287
// no-error
270288

271289
@Swift::available() var use5
272-
// FIXME improve: expected-error@-1 {{unknown attribute 'available'}}
290+
// FIXME improve: expected-error@-1 {{unknown attribute 'Swift::available'}}
273291
// FIXME suppress: expected-error@-2 {{type annotation missing in pattern}}
274292
}
275293

276294
func builderUser2(@main::MyBuilder fn: () -> Void) {}
277-
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
295+
// FIXME improve: expected-error@-1 {{unknown attribute 'main::MyBuilder'}}
278296

279297
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
280298
// no-error
281299

282300
func builderUser4(@Swift::MyBuilder fn: () -> Void) {}
283-
// FIXME improve: expected-error@-1 {{unknown attribute 'MyBuilder'}}
301+
// FIXME improve: expected-error@-1 {{unknown attribute 'Swift::MyBuilder'}}
284302

285303
// Error cases
286304

0 commit comments

Comments
 (0)