Skip to content

Commit 4a319b2

Browse files
committed
[Macros] Diagnose default literal type overrides in macro expansions.
1 parent cd62c6d commit 4a319b2

File tree

4 files changed

+66
-18
lines changed

4 files changed

+66
-18
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6913,8 +6913,11 @@ ERROR(invalid_decl_in_macro_expansion,none,
69136913
"macro expansion cannot introduce %0",
69146914
(DescriptiveDeclKind))
69156915
ERROR(invalid_main_type_in_macro_expansion,none,
6916-
"macro expansion cannot introduce '@main' type'",
6916+
"macro expansion cannot introduce '@main' type",
69176917
())
6918+
ERROR(literal_type_in_macro_expansion,none,
6919+
"macro expansion cannot introduce default literal type %0",
6920+
(Identifier))
69186921
ERROR(invalid_macro_introduced_name,none,
69196922
"declaration name %0 is not covered by macro %1",
69206923
(DeclName, DeclName))

lib/Sema/TypeCheckMacros.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@ ExpandPeerMacroRequest::evaluate(Evaluator &evaluator, Decl *decl) const {
476476
return decl->getASTContext().AllocateCopy(bufferIDs);
477477
}
478478

479+
static Identifier makeIdentifier(ASTContext &ctx, StringRef name) {
480+
return ctx.getIdentifier(name);
481+
}
482+
483+
static Identifier makeIdentifier(ASTContext &ctx, std::nullptr_t) {
484+
return Identifier();
485+
}
486+
479487
/// Diagnose macro expansions that produce any of the following declarations:
480488
/// - Import declarations
481489
/// - Operator and precedence group declarations
@@ -530,7 +538,17 @@ static void validateMacroExpansion(SourceFile *expansionBuffer,
530538

531539
// Diagnose default literal type overrides.
532540
if (auto *typeAlias = dyn_cast<TypeAliasDecl>(decl)) {
533-
// TODO
541+
auto name = typeAlias->getBaseIdentifier();
542+
#define EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME(_, __, typeName, \
543+
supportsOverride) \
544+
if (supportsOverride && name == makeIdentifier(ctx, typeName)) { \
545+
typeAlias->diagnose(diag::literal_type_in_macro_expansion, \
546+
makeIdentifier(ctx, typeName)); \
547+
typeAlias->setInvalid(); \
548+
continue; \
549+
}
550+
#include "swift/AST/KnownProtocols.def"
551+
#undef EXPRESSIBLE_BY_LITERAL_PROTOCOL_WITH_NAME
534552
}
535553

536554
// Diagnose value decls with names not covered by the macro

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,17 @@ public struct InvalidMacro: PeerMacro {
781781
static func main() {}
782782
}
783783
""",
784+
"typealias Array = Void",
785+
"typealias Dictionary = Void",
786+
"typealias BooleanLiteralType = Void",
787+
"typealias ExtendedGraphemeClusterType = Void",
788+
"typealias FloatLiteralType = Void",
789+
"typealias IntegerLiteralType = Void",
790+
"typealias StringLiteralType = Void",
791+
"typealias UnicodeScalarType = Void",
792+
"typealias _ColorLiteralType = Void",
793+
"typealias _ImageLiteralType = Void",
794+
"typealias _FileReferenceLiteralType = Void",
784795
]
785796
}
786797
}

test/Macros/macro_expand.swift

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,47 @@ macro Invalid() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro"
3232

3333
@Invalid
3434
struct Bad {}
35-
// expected-note@-1 7 {{in expansion of macro 'Invalid' here}}
35+
// expected-note@-1 18 {{in expansion of macro 'Invalid' here}}
3636

3737
// CHECK-DIAGS: error: macro expansion cannot introduce import
38-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
39-
// CHECK-DIAGS: import Swift
40-
4138
// CHECK-DIAGS: error: macro expansion cannot introduce precedence group
42-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
43-
// CHECK-DIAGS: precedencegroup MyPrecedence {}
44-
4539
// CHECK-DIAGS: error: macro expansion cannot introduce macro
46-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
47-
// CHECK-DIAGS: @attached(member) macro myMacro()
48-
4940
// CHECK-DIAGS: error: macro expansion cannot introduce extension
50-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
51-
// CHECK-DIAGS: extension Int {}
41+
// CHECK-DIAGS: error: macro expansion cannot introduce '@main' type
42+
// CHECK-DIAGS: error: declaration name 'MyMain' is not covered by macro 'Invalid'
43+
// CHECK-DIAGS: error: declaration name 'Array' is not covered by macro 'Invalid'
44+
// CHECK-DIAGS: error: declaration name 'Dictionary' is not covered by macro 'Invalid'
45+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'BooleanLiteralType'
46+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'ExtendedGraphemeClusterType'
47+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'FloatLiteralType'
48+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'IntegerLiteralType'
49+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'StringLiteralType'
50+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type 'UnicodeScalarType'
51+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_ColorLiteralType'
52+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_ImageLiteralType'
53+
// CHECK-DIAGS: error: macro expansion cannot introduce default literal type '_FileReferenceLiteralType'
5254

53-
// CHECK-DIAGS: error: macro expansion cannot introduce '@main' type'
5455
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
56+
// CHECK-DIAGS: import Swift
57+
// CHECK-DIAGS: precedencegroup MyPrecedence {}
58+
// CHECK-DIAGS: @attached(member) macro myMacro()
59+
// CHECK-DIAGS: extension Int {}
5560
// CHECK-DIAGS: @main
56-
57-
// CHECK-DIAGS: error: declaration name 'MyMain' is not covered by macro 'Invalid'
58-
// CHECK-DIAGS: CONTENTS OF FILE @__swiftmacro_9MacroUser3BadV7InvalidfMp_.swift
5961
// CHECK-DIAGS: struct MyMain {
62+
// CHECK-DIAGS: static func main() {}
63+
// CHECK-DIAGS: }
64+
// CHECK-DIAGS: typealias Array = Void
65+
// CHECK-DIAGS: typealias Dictionary = Void
66+
// CHECK-DIAGS: typealias BooleanLiteralType = Void
67+
// CHECK-DIAGS: typealias ExtendedGraphemeClusterType = Void
68+
// CHECK-DIAGS: typealias FloatLiteralType = Void
69+
// CHECK-DIAGS: typealias IntegerLiteralType = Void
70+
// CHECK-DIAGS: typealias StringLiteralType = Void
71+
// CHECK-DIAGS: typealias UnicodeScalarType = Void
72+
// CHECK-DIAGS: typealias _ColorLiteralType = Void
73+
// CHECK-DIAGS: typealias _ImageLiteralType = Void
74+
// CHECK-DIAGS: typealias _FileReferenceLiteralType = Void
75+
// CHECK-DIAGS: END CONTENTS OF FILE
6076
#endif
6177

6278
@freestanding(expression) macro customFileID() -> String = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")

0 commit comments

Comments
 (0)