Skip to content

Commit 2bbda09

Browse files
committed
[CSSimplify] Detect and diagnose attempts to specialize non-generic types/aliases
1 parent 40169c7 commit 2bbda09

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13658,8 +13658,15 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1365813658
}
1365913659
}
1366013660

13661-
if (openedGenericParams.empty())
13662-
return SolutionKind::Error;
13661+
if (openedGenericParams.empty()) {
13662+
if (!shouldAttemptFixes())
13663+
return SolutionKind::Error;
13664+
13665+
return recordFix(AllowConcreteTypeSpecialization::create(
13666+
*this, type1, getConstraintLocator(locator)))
13667+
? SolutionKind::Error
13668+
: SolutionKind::Solved;
13669+
}
1366313670

1366413671
assert(openedGenericParams.size() == genericParams->size());
1366513672

test/Macros/macro_and_typealias.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
@freestanding(expression) public macro Print<each Value>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
88
@freestanding(expression) public macro OtherPrint<each Value>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
9+
@freestanding(expression) public macro ConcretePrint(_ value: Any) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
910

1011
public struct Printer<Value> {
1112
init(_: (Value) -> Void) {}
1213
}
1314

1415
typealias Print = Printer
1516
typealias OtherPrint<T> = Printer<T>
17+
typealias ConcretePrint = Printer<Any>
1618

1719
struct Test {
1820
struct Object {
@@ -27,6 +29,11 @@ struct Test {
2729
let _ = OtherPrint<Object> { // Ok
2830
compute(root: $0, \.prop)
2931
}
32+
33+
let _ = ConcretePrint<Object> { // expected-error {{cannot specialize non-generic type 'ConcretePrint' (aka 'Printer<Any>')}}
34+
compute(root: $0, \.prop) // expected-error {{value of type 'Any' has no member 'prop'}}
35+
// expected-note@-1 {{cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members}}
36+
}
3037
}
3138

3239
func compute<R, V>(root: R, _: KeyPath<R, V>) {}

0 commit comments

Comments
 (0)