Skip to content

[5.9] [Macros] Fix existential diagnostics for declaration macro expansions #66152

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
May 26, 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
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5335,9 +5335,15 @@ void TypeChecker::checkExistentialTypes(Decl *decl) {
} else if (auto *macroDecl = dyn_cast<MacroDecl>(decl)) {
checkExistentialTypes(ctx, macroDecl->getGenericParams());
checkExistentialTypes(ctx, macroDecl->getTrailingWhereClause());
} else if (auto *macroExpansionDecl = dyn_cast<MacroExpansionDecl>(decl)) {
ExistentialTypeVisitor visitor(ctx, /*checkStatements=*/false);
macroExpansionDecl->getArgs()->walk(visitor);
for (auto *genArg : macroExpansionDecl->getGenericArgs())
genArg->walk(visitor);
}

if (isa<TypeDecl>(decl) || isa<ExtensionDecl>(decl))
if (isa<TypeDecl>(decl) || isa<ExtensionDecl>(decl) ||
isa<MacroExpansionDecl>(decl))
return;

ExistentialTypeVisitor visitor(ctx, /*checkStatements=*/false);
Expand Down
5 changes: 4 additions & 1 deletion test/Macros/Inputs/freestanding_macro_library.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
public macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")

@freestanding(declaration)
public macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
public macro anonymousTypes(public: Bool = false, causeErrors: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")

@freestanding(declaration)
public macro introduceTypeCheckingErrors() = #externalMacro(module: "MacroDefinition", type: "IntroduceTypeCheckingErrorsMacro")

@freestanding(declaration)
public macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
Expand Down
37 changes: 36 additions & 1 deletion test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
} else {
accessSpecifier = ""
}
return [
var results: [DeclSyntax] = [
"""
\(raw:accessSpecifier)class \(context.makeUniqueName("name")) {
Expand All @@ -1360,6 +1360,41 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
\(body.statements)
}
}
""",
"""
struct \(context.makeUniqueName("name")): Equatable {
static func == (lhs: Self, rhs: Self) -> Bool { false }
}
"""
]

if let _ = node.argumentList.first(labeled: "causeErrors") {

results += ["""
struct \(context.makeUniqueName("name"))<T> where T == Equatable { // expect error: need 'any'
#introduceTypeCheckingErrors // make sure we get nested errors
}
"""]
}

return results
}
}

public struct IntroduceTypeCheckingErrorsMacro: DeclarationMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> [DeclSyntax] {
return [
"""
struct \(context.makeUniqueName("name")) {
struct \(context.makeUniqueName("name"))<T> where T == Hashable { // expect error: need 'any'
}
}
"""
]
}
Expand Down
11 changes: 11 additions & 0 deletions test/Macros/macros_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,14 @@ struct MyStruct<T: MyProto> {
#undefinedMacro { definitelyNotDefined }
// expected-error@-1{{cannot find 'definitelyNotDefined' in scope}}
// expected-error@-2{{no macro named 'undefinedMacro'}}

@freestanding(declaration) macro genericUnary<T>(_: T) = #externalMacro(module: "A", type: "B")
// expected-warning@-1{{external macro implementation type}}
// expected-note@-2{{'genericUnary' declared here}}

struct SomeType {
#genericUnary<Equatable>(0 as Hashable)
// expected-error@-1{{use of protocol 'Equatable' as a type must be written 'any Equatable'}}
// expected-error@-2{{use of protocol 'Hashable' as a type must be written 'any Hashable'}}
// expected-error@-3{{external macro implementation type}}
}
26 changes: 22 additions & 4 deletions test/Macros/top_level_freestanding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// RUN: %target-typecheck-verify-swift -swift-version 5 -parse-as-library -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -DIMPORT_MACRO_LIBRARY -swift-version 5 %S/Inputs/top_level_freestanding_other.swift -I %t

// Check diagnostic buffer names
// RUN: %target-swift-frontend -typecheck -swift-version 5 -parse-as-library -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 %s %S/Inputs/top_level_freestanding_other.swift 2> %t.diags
// RUN: not %target-swift-frontend -typecheck -swift-version 5 -parse-as-library -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS -swift-version 5 %s %S/Inputs/top_level_freestanding_other.swift 2> %t.diags
// RUN: %FileCheck -check-prefix DIAG_BUFFERS %s < %t.diags

// Execution testing
Expand All @@ -27,7 +27,9 @@ import freestanding_macro_library
@freestanding(declaration, names: named(StructWithUnqualifiedLookup))
macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")
@freestanding(declaration)
macro anonymousTypes(public: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
macro anonymousTypes(public: Bool = false, causeErrors: Bool = false, _: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
@freestanding(declaration)
macro introduceTypeCheckingErrors() = #externalMacro(module: "MacroDefinition", type: "IntroduceTypeCheckingErrorsMacro")
@freestanding(declaration)
macro freestandingWithClosure<T>(_ value: T, body: (T) -> T) = #externalMacro(module: "MacroDefinition", type: "EmptyDeclarationMacro")
@freestanding(declaration, names: arbitrary) macro bitwidthNumberedStructs(_ baseName: String) = #externalMacro(module: "MacroDefinition", type: "DefineBitwidthNumberedStructsMacro")
Expand Down Expand Up @@ -78,11 +80,27 @@ func testArbitraryAtGlobal() {
_ = MyIntGlobal16()
}

// DIAG_BUFFERS: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf1_{{.*}}warning: 'deprecated()' is deprecated
// DIAG_BUFFERS: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf2_{{.*}}warning: 'deprecated()' is deprecated
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf1_{{.*}}warning: 'deprecated()' is deprecated
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser33_{{.*}}9stringifyfMf2_{{.*}}warning: 'deprecated()' is deprecated

#varValue

func testGlobalVariable() {
_ = value
}

#if TEST_DIAGNOSTICS

// expected-note @+1 6 {{in expansion of macro 'anonymousTypes' here}}
#anonymousTypes(causeErrors: true) { "foo" }
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser33{{.*}}anonymousTypesfMf2_{{.*}}error: use of protocol 'Equatable' as a type must be written 'any Equatable'
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser03{{.*}}anonymousTypes{{.*}}introduceTypeCheckingErrorsfMf0_{{.*}}error: use of protocol 'Hashable' as a type must be written 'any Hashable'

// expected-note @+1 2 {{in expansion of macro 'anonymousTypes' here}}
#anonymousTypes { () -> String in
// expected-error @+1 {{use of protocol 'Equatable' as a type must be written 'any Equatable'}}
_ = 0 as Equatable
return "foo"
}

#endif
5 changes: 5 additions & 0 deletions test/SourceKit/Macros/macro_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition"
// EXPAND_MACRO_DECL-NEXT: func hello() -> String {
// EXPAND_MACRO_DECL-NEXT: "hello"
// EXPAND_MACRO_DECL-NEXT: }
// EXPAND_MACRO_DECL-NEXT: }
// EXPAND_MACRO_DECL-NEXT: struct $s9MacroUser33_70D4178875715FB9B8B50C58F66F8D53Ll14anonymousTypesfMf0_4namefMu1_: Equatable {
// EXPAND_MACRO_DECL-NEXT: static func == (lhs: Self, rhs: Self) -> Bool {
// EXPAND_MACRO_DECL-NEXT: false
// EXPAND_MACRO_DECL-NEXT: }
// EXPAND_MACRO_DECL-NEXT: }"

//##-- cursor-info on attached macro
Expand Down