Skip to content

Commit eb3cbde

Browse files
authored
Merge pull request #64131 from DougGregor/macro-generic-interface-type
Make the interface type of a generic macro into a GenericFunctionType.
2 parents 7bfa061 + 1ba67b9 commit eb3cbde

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

lib/IDE/CompletionLookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,7 @@ void CompletionLookup::addMacroExpansion(const MacroDecl *MD,
18081808
Type macroType = MD->getInterfaceType();
18091809
if (MD->parameterList && MD->parameterList->size() > 0) {
18101810
Builder.addLeftParen();
1811-
addCallArgumentPatterns(Builder, macroType->castTo<FunctionType>(),
1811+
addCallArgumentPatterns(Builder, macroType->castTo<AnyFunctionType>(),
18121812
MD->parameterList,
18131813
MD->getGenericSignature());
18141814
Builder.addRightParen();

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,14 +1676,13 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
16761676

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

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

1685-
Type openedType = openType(macroType, replacements);
1686-
16871686
// FIXME: Should we use replaceParamErrorTypeByPlaceholder() here?
16881687

16891688
return { openedType, openedType, openedType, openedType };

lib/Sema/TypeCheckDecl.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,8 +2595,15 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
25952595

25962596
SmallVector<AnyFunctionType::Param, 4> paramTypes;
25972597
macro->parameterList->getParams(paramTypes);
2598-
FunctionType::ExtInfo info;
2599-
return FunctionType::get(paramTypes, resultType, info);
2598+
2599+
if (auto genericSig = macro->getGenericSignature()) {
2600+
GenericFunctionType::ExtInfo info;
2601+
return GenericFunctionType::get(
2602+
genericSig, paramTypes, resultType, info);
2603+
} else {
2604+
FunctionType::ExtInfo info;
2605+
return FunctionType::get(paramTypes, resultType, info);
2606+
}
26002607
}
26012608
}
26022609
llvm_unreachable("invalid decl kind");

test/Index/index_macros.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func test(x: Int) {
1515

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

2222
// CHECK: 11:8 | macro/Swift | myLine() | s:14swift_ide_test6myLineSiycfm | Ref,RelCont | rel: 1
23-
// CHECK: 12:20 | macro/Swift | myFilename() | s:14swift_ide_test10myFilenamexycfm | Ref,RelCont | rel: 1
24-
// CHECK: 13:8 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxcfm | Ref,RelCont | rel: 1
23+
// CHECK: 12:20 | macro/Swift | myFilename() | s:14swift_ide_test10myFilenamexycs26ExpressibleByStringLiteralRzlufm | Ref,RelCont | rel: 1
24+
// CHECK: 13:8 | macro/Swift | myStringify(_:) | s:14swift_ide_test11myStringifyyx_SStxclufm | Ref,RelCont | rel: 1
2525
// CHECK: 13:20 | param/Swift | x | s:14swift_ide_test0C01xySi_tFACL_Sivp | Ref,Read,RelCont | rel: 1
2626
// CHECK: 13:22 | static-method/infix-operator/Swift | +(_:_:) | s:Si1poiyS2i_SitFZ | Ref,Call,RelCall,RelCont | rel: 1
2727
// CHECK: 13:24 | param/Swift | x | s:14swift_ide_test0C01xySi_tFACL_Sivp | Ref,Read,RelCont | rel: 1

test/Macros/macros_diagnostics.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ func overloaded1(_ p: Any) { }
6363
@freestanding(expression) macro notOverloaded1(_ p: P) = #externalMacro(module: "MissingModule", type: "MissingOtherType") // expected-error{{invalid redeclaration of 'notOverloaded1'}}
6464
// expected-warning@-1{{external macro implementation type}}
6565

66+
// Overloading based on generic constraint.
67+
public protocol ResultBuilder {
68+
}
69+
70+
@freestanding(expression) public macro ApplyBuilder<R: ResultBuilder>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> String) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro")
71+
// expected-warning@-1{{external macro implementation type}}
72+
@freestanding(expression) public macro ApplyBuilder<R>(resultBuilder: R.Type, to closure: () -> Void) -> (() -> String) = #externalMacro(module: "MacroExamplesPlugin", type: "ResultBuilderMacro2")
73+
// expected-warning@-1{{external macro implementation type}}
74+
6675
@freestanding(expression) macro intIdentity(value: Int, _: Float) -> Int = #externalMacro(module: "MissingModule", type: "MissingType")
6776
// expected-note@-1{{'intIdentity(value:_:)' declared here}}
6877
// expected-warning@-2{{external macro implementation type}}

test/SourceKit/Macros/macro_basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct S4 { }
9393
// CURSOR_MACRONAME-LABEL: SYMBOL GRAPH BEGIN
9494
// CURSOR_MACRONAME: "identifier": {
9595
// CURSOR_MACRONAME-NEXT: "interfaceLanguage": "swift",
96-
// CURSOR_MACRONAME-NEXT: "precise": "s:9MacroUser9stringifyyx_SStxcfm"
96+
// CURSOR_MACRONAME-NEXT: "precise": "s:9MacroUser9stringifyyx_SStxclufm"
9797
// CURSOR_MACRONAME-NEXT: },
9898
// CURSOR_MACRONAME-NEXT: "kind": {
9999
// CURSOR_MACRONAME-NEXT: "displayName": "Macro",

0 commit comments

Comments
 (0)