@@ -543,6 +543,67 @@ static Identifier makeIdentifier(ASTContext &ctx, std::nullptr_t) {
543
543
return Identifier ();
544
544
}
545
545
546
+ static void diagnoseInvalidDecl (Decl *decl,
547
+ MacroDecl *macro,
548
+ llvm::function_ref<bool (DeclName)> coversName) {
549
+ auto &ctx = decl->getASTContext ();
550
+
551
+ // Diagnose invalid declaration kinds.
552
+ if (isa<ImportDecl>(decl) ||
553
+ isa<OperatorDecl>(decl) ||
554
+ isa<PrecedenceGroupDecl>(decl) ||
555
+ isa<MacroDecl>(decl) ||
556
+ isa<ExtensionDecl>(decl)) {
557
+ decl->diagnose (diag::invalid_decl_in_macro_expansion,
558
+ decl->getDescriptiveKind ());
559
+ decl->setInvalid ();
560
+
561
+ if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
562
+ extension->setExtendedNominal (nullptr );
563
+ }
564
+
565
+ return ;
566
+ }
567
+
568
+ // Diagnose `@main` types.
569
+ if (auto *mainAttr = decl->getAttrs ().getAttribute <MainTypeAttr>()) {
570
+ ctx.Diags .diagnose (mainAttr->getLocation (),
571
+ diag::invalid_main_type_in_macro_expansion);
572
+ mainAttr->setInvalid ();
573
+ }
574
+
575
+ // Diagnose default literal type overrides.
576
+ if (auto *typeAlias = dyn_cast<TypeAliasDecl>(decl)) {
577
+ auto name = typeAlias->getBaseIdentifier ();
578
+ #define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME (_, __, typeName, \
579
+ supportsOverride) \
580
+ if (supportsOverride && name == makeIdentifier (ctx, typeName)) { \
581
+ typeAlias->diagnose (diag::literal_type_in_macro_expansion, \
582
+ makeIdentifier (ctx, typeName)); \
583
+ typeAlias->setInvalid (); \
584
+ return ; \
585
+ }
586
+ #include " swift/AST/KnownProtocols.def"
587
+ #undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
588
+ }
589
+
590
+ // Diagnose value decls with names not covered by the macro
591
+ if (auto *value = dyn_cast<ValueDecl>(decl)) {
592
+ auto name = value->getName ();
593
+
594
+ // Unique names are always permitted.
595
+ if (MacroDecl::isUniqueMacroName (name.getBaseName ().userFacingName ()))
596
+ return ;
597
+
598
+ if (coversName (name)) {
599
+ return ;
600
+ }
601
+
602
+ value->diagnose (diag::invalid_macro_introduced_name,
603
+ name, macro->getBaseName ());
604
+ }
605
+ }
606
+
546
607
// / Diagnose macro expansions that produce any of the following declarations:
547
608
// / - Import declarations
548
609
// / - Operator and precedence group declarations
@@ -559,75 +620,33 @@ static void validateMacroExpansion(SourceFile *expansionBuffer,
559
620
llvm::SmallVector<DeclName, 2 > introducedNames;
560
621
macro->getIntroducedNames (role, attachedTo, introducedNames);
561
622
562
- llvm::SmallDenseSet<DeclName, 2 > coversName (introducedNames. begin (),
563
- introducedNames.end ());
623
+ llvm::SmallDenseSet<DeclName, 2 > introducedNameSet (
624
+ introducedNames. begin (), introducedNames.end ());
564
625
565
- for (auto *decl : expansionBuffer->getTopLevelDecls ()) {
566
- auto &ctx = decl->getASTContext ();
626
+ auto coversName = [&](DeclName name) -> bool {
627
+ return (introducedNameSet.count (name) ||
628
+ introducedNameSet.count (name.getBaseName ()) ||
629
+ introducedNameSet.count (MacroDecl::getArbitraryName ()));
630
+ };
567
631
632
+ for (auto *decl : expansionBuffer->getTopLevelDecls ()) {
568
633
// Certain macro roles can generate special declarations.
569
634
if ((isa<AccessorDecl>(decl) && role == MacroRole::Accessor) ||
570
- (isa<ExtensionDecl>(decl) && role == MacroRole::Conformance) ||
571
- (isa<ExtensionDecl>(decl) && role == MacroRole::Extension)) { // FIXME: Check extension for generated names.
635
+ (isa<ExtensionDecl>(decl) && role == MacroRole::Conformance)) {
572
636
continue ;
573
637
}
574
638
575
- // Diagnose invalid declaration kinds.
576
- if (isa<ImportDecl>(decl) ||
577
- isa<OperatorDecl>(decl) ||
578
- isa<PrecedenceGroupDecl>(decl) ||
579
- isa<MacroDecl>(decl) ||
580
- isa<ExtensionDecl>(decl)) {
581
- decl->diagnose (diag::invalid_decl_in_macro_expansion,
582
- decl->getDescriptiveKind ());
583
- decl->setInvalid ();
584
-
585
- if (auto *extension = dyn_cast<ExtensionDecl>(decl)) {
586
- extension->setExtendedNominal (nullptr );
639
+ if (role == MacroRole::Extension) {
640
+ auto *extension = dyn_cast<ExtensionDecl>(decl);
641
+
642
+ for (auto *member : extension->getMembers ()) {
643
+ diagnoseInvalidDecl (member, macro, coversName);
587
644
}
588
645
589
646
continue ;
590
647
}
591
648
592
- // Diagnose `@main` types.
593
- if (auto *mainAttr = decl->getAttrs ().getAttribute <MainTypeAttr>()) {
594
- ctx.Diags .diagnose (mainAttr->getLocation (),
595
- diag::invalid_main_type_in_macro_expansion);
596
- mainAttr->setInvalid ();
597
- }
598
-
599
- // Diagnose default literal type overrides.
600
- if (auto *typeAlias = dyn_cast<TypeAliasDecl>(decl)) {
601
- auto name = typeAlias->getBaseIdentifier ();
602
- #define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME (_, __, typeName, \
603
- supportsOverride) \
604
- if (supportsOverride && name == makeIdentifier (ctx, typeName)) { \
605
- typeAlias->diagnose (diag::literal_type_in_macro_expansion, \
606
- makeIdentifier (ctx, typeName)); \
607
- typeAlias->setInvalid (); \
608
- continue ; \
609
- }
610
- #include " swift/AST/KnownProtocols.def"
611
- #undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
612
- }
613
-
614
- // Diagnose value decls with names not covered by the macro
615
- if (auto *value = dyn_cast<ValueDecl>(decl)) {
616
- auto name = value->getName ();
617
-
618
- // Unique names are always permitted.
619
- if (MacroDecl::isUniqueMacroName (name.getBaseName ().userFacingName ()))
620
- continue ;
621
-
622
- if (coversName.count (name) ||
623
- coversName.count (name.getBaseName ()) ||
624
- coversName.count (MacroDecl::getArbitraryName ())) {
625
- continue ;
626
- }
627
-
628
- value->diagnose (diag::invalid_macro_introduced_name,
629
- name, macro->getBaseName ());
630
- }
649
+ diagnoseInvalidDecl (decl, macro, coversName);
631
650
}
632
651
}
633
652
0 commit comments