Skip to content

[Macros] Diagnose undocumented conformances in extension macro expansions. #67383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7246,6 +7246,9 @@ ERROR(literal_type_in_macro_expansion,none,
ERROR(invalid_macro_introduced_name,none,
"declaration name %0 is not covered by macro %1",
(DeclName, DeclName))
ERROR(undocumented_conformance_in_expansion,none,
"conformance to %0 is not covered by macro %1",
(Type, DeclName))
ERROR(invalid_macro_role_for_macro_syntax,none,
"invalid macro role for %{a freestanding|an attached}0 macro",
(unsigned))
Expand Down
44 changes: 44 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,50 @@ swift::expandExtensions(CustomAttr *attr, MacroDecl *macro,
if (auto file = dyn_cast<FileUnit>(
decl->getDeclContext()->getModuleScopeContext()))
file->getOrCreateSynthesizedFile().addTopLevelDecl(extension);

// Don't validate documented conformances for the 'conformance' role.
if (role == MacroRole::Conformance)
continue;

// Extension macros can only add conformances that are documented by
// the `@attached(extension)` attribute.
for (auto inherited : extension->getInherited()) {
auto constraint =
TypeResolution::forInterface(
extension->getDeclContext(),
TypeResolverContext::GenericRequirement,
/*unboundTyOpener*/ nullptr,
/*placeholderHandler*/ nullptr,
/*packElementOpener*/ nullptr)
.resolveType(inherited.getTypeRepr());

// Already diagnosed or will be diagnosed later.
if (constraint->is<ErrorType>() || !constraint->isConstraintType())
continue;

std::function<bool(Type)> isUndocumentedConformance =
[&](Type constraint) -> bool {
if (auto *proto = constraint->getAs<ParameterizedProtocolType>())
return !llvm::is_contained(potentialConformances,
proto->getProtocol());

if (auto *proto = constraint->getAs<ProtocolType>())
return !llvm::is_contained(potentialConformances,
proto->getDecl());

return llvm::any_of(
constraint->castTo<ProtocolCompositionType>()->getMembers(),
isUndocumentedConformance);
};

if (isUndocumentedConformance(constraint)) {
extension->diagnose(
diag::undocumented_conformance_in_expansion,
constraint, macro->getBaseName());

extension->setInvalid();
}
}
}

return macroSourceFile->getBufferID();
Expand Down
46 changes: 46 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,52 @@ public struct DelegatedConformanceViaExtensionMacro: ExtensionMacro {
}
}

public struct AlwaysAddConformance: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo decl: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
let decl: DeclSyntax =
"""
extension \(raw: type.trimmedDescription): P where Element: P {
static func requirement() {
Element.requirement()
}
}

"""

return [
decl.cast(ExtensionDeclSyntax.self)
]
}
}

public struct AlwaysAddCodable: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo decl: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
let decl: DeclSyntax =
"""
extension \(raw: type.trimmedDescription): Codable {
}

"""

return [
decl.cast(ExtensionDeclSyntax.self)
]
}
}


public struct ExtendableEnum: MemberMacro {
public static func expansion(
of node: AttributeSyntax,
Expand Down
28 changes: 28 additions & 0 deletions test/Macros/macro_expand_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,32 @@ struct S<Element> {}
// expected-note@-1 {{in expansion of macro 'UndocumentedNamesInExtension' here}}

// CHECK-DIAGS: error: declaration name 'requirement()' is not covered by macro 'UndocumentedNamesInExtension'

@attached(extension, names: named(requirement))
macro UndocumentedConformanceInExtension() = #externalMacro(module: "MacroDefinition", type: "AlwaysAddConformance")

@UndocumentedConformanceInExtension
struct InvalidConformance<Element> {}
// expected-note@-1 {{in expansion of macro 'UndocumentedConformanceInExtension' here}}

// CHECK-DIAGS: error: conformance to 'P' is not covered by macro 'UndocumentedConformanceInExtension'

@attached(extension)
macro UndocumentedCodable() = #externalMacro(module: "MacroDefinition", type: "AlwaysAddCodable")

@UndocumentedCodable
struct TestUndocumentedCodable {}
// expected-note@-1 {{in expansion of macro 'UndocumentedCodable' here}}

// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedCodable'

@attached(extension, conformances: Decodable)
macro UndocumentedEncodable() = #externalMacro(module: "MacroDefinition", type: "AlwaysAddCodable")

@UndocumentedEncodable
struct TestUndocumentedEncodable {}
// expected-note@-1 {{in expansion of macro 'UndocumentedEncodable' here}}

// CHECK-DIAGS: error: conformance to 'Codable' (aka 'Decodable & Encodable') is not covered by macro 'UndocumentedEncodable'

#endif