Skip to content

Make the interface type of a generic macro into a GenericFunctionType. #64131

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 2 commits into from
Mar 6, 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
2 changes: 1 addition & 1 deletion lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ void CompletionLookup::addMacroExpansion(const MacroDecl *MD,
Type macroType = MD->getInterfaceType();
if (MD->parameterList && MD->parameterList->size() > 0) {
Builder.addLeftParen();
addCallArgumentPatterns(Builder, macroType->castTo<FunctionType>(),
addCallArgumentPatterns(Builder, macroType->castTo<AnyFunctionType>(),
MD->parameterList,
MD->getGenericSignature());
Builder.addRightParen();
Expand Down
7 changes: 3 additions & 4 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1673,14 +1673,13 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,

// Open any the generic types.
OpenedTypeMap replacements;
openGeneric(macro->getParentModule(), macro->getGenericSignature(),
locator, replacements);
Type openedType = openFunctionType(
macroType->castTo<AnyFunctionType>(), locator, replacements,
macro->getDeclContext());

// If we opened up any type variables, record the replacements.
recordOpenedTypes(locator, replacements);

Type openedType = openType(macroType, replacements);

// FIXME: Should we use replaceParamErrorTypeByPlaceholder() here?

return { openedType, openedType, openedType, openedType };
Expand Down
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2595,8 +2595,15 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {

SmallVector<AnyFunctionType::Param, 4> paramTypes;
macro->parameterList->getParams(paramTypes);
FunctionType::ExtInfo info;
return FunctionType::get(paramTypes, resultType, info);

if (auto genericSig = macro->getGenericSignature()) {
GenericFunctionType::ExtInfo info;
return GenericFunctionType::get(
genericSig, paramTypes, resultType, info);
} else {
FunctionType::ExtInfo info;
return FunctionType::get(paramTypes, resultType, info);
}
}
}
llvm_unreachable("invalid decl kind");
Expand Down
8 changes: 4 additions & 4 deletions test/Index/index_macros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func test(x: Int) {

// CHECK: 6:33 | macro/Swift | myLine() | s:14swift_ide_test6myLineSiycfm | Def | rel: 0
// CHECK: 6:45 | struct/Swift | Int | s:Si | Ref | rel: 0
// CHECK: 7:33 | macro/Swift | myFilename() | s:14swift_ide_test10myFilenamexycfm | Def | rel: 0
// CHECK: 7:33 | macro/Swift | myFilename() | s:14swift_ide_test10myFilenamexycs26ExpressibleByStringLiteralRzlufm | Def | rel: 0
// CHECK: 7:47 | protocol/Swift | ExpressibleByStringLiteral | s:s26ExpressibleByStringLiteralP | Ref | rel: 0
// CHECK: 8:33 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxcfm | Def | rel: 0
// CHECK: 8:33 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxclufm | Def | rel: 0

// CHECK: 11:8 | macro/Swift | myLine() | s:14swift_ide_test6myLineSiycfm | Ref,RelCont | rel: 1
// CHECK: 12:20 | macro/Swift | myFilename() | s:14swift_ide_test10myFilenamexycfm | Ref,RelCont | rel: 1
// CHECK: 13:8 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxcfm | Ref,RelCont | rel: 1
// CHECK: 12:20 | macro/Swift | myFilename() | s:14swift_ide_test10myFilenamexycs26ExpressibleByStringLiteralRzlufm | Ref,RelCont | rel: 1
// CHECK: 13:8 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxclufm | Ref,RelCont | rel: 1
// CHECK: 13:20 | param/Swift | x | s:14swift_ide_test0C01xySi_tFACL_Sivp | Ref,Read,RelCont | rel: 1
// CHECK: 13:22 | static-method/infix-operator/Swift | +(_:_:) | s:Si1poiyS2i_SitFZ | Ref,Call,RelCall,RelCont | rel: 1
// CHECK: 13:24 | param/Swift | x | s:14swift_ide_test0C01xySi_tFACL_Sivp | Ref,Read,RelCont | rel: 1
9 changes: 9 additions & 0 deletions test/Macros/macros_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func overloaded1(_ p: Any) { }
@freestanding(expression) macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingOtherType") // expected-error{{invalid redeclaration of 'notOverloaded1'}}
// expected-warning@-1{{external macro implementation type}}

// Overloading based on generic constraint.
public protocol ResultBuilder {
}

@freestanding(expression) public macro ApplyBuilder<R: ResultBuilder>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> String) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro")
// expected-warning@-1{{external macro implementation type}}
@freestanding(expression) public macro ApplyBuilder<R>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> String) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro2")
// expected-warning@-1{{external macro implementation type}}

@freestanding(expression) macro intIdentity(value: Int, _: Float) -> Int = #externalMacro(module: "MissingModule", type: "MissingType")
// expected-note@-1{{'intIdentity(value:_:)' declared here}}
// expected-warning@-2{{external macro implementation type}}
Expand Down
2 changes: 1 addition & 1 deletion test/SourceKit/Macros/macro_basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct S4 { }
// CURSOR_MACRONAME-LABEL: SYMBOL GRAPH BEGIN
// CURSOR_MACRONAME: "identifier": {
// CURSOR_MACRONAME-NEXT: "interfaceLanguage": "swift",
// CURSOR_MACRONAME-NEXT: "precise": "s:9MacroUser9stringifyyx_SStxcfm"
// CURSOR_MACRONAME-NEXT: "precise": "s:9MacroUser9stringifyyx_SStxclufm"
// CURSOR_MACRONAME-NEXT: },
// CURSOR_MACRONAME-NEXT: "kind": {
// CURSOR_MACRONAME-NEXT: "displayName": "Macro",
Expand Down