Skip to content

Commit af3b416

Browse files
committed
Make the interface type of a generic macro into a GenericFunctionType.
This correctly models their type in the type system, and fixes the redeclaration issue reported in rdar://104183961.
1 parent 6a9e9d6 commit af3b416

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
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
@@ -1673,14 +1673,13 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
16731673

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

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

1682-
Type openedType = openType(macroType, replacements);
1683-
16841683
// FIXME: Should we use replaceParamErrorTypeByPlaceholder() here?
16851684

16861685
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/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}}

0 commit comments

Comments
 (0)