[CSSimplify] Detect and diagnose attempts to specialize non-generic types/aliases

This commit is contained in:
Pavel Yaskevich
2023-06-27 15:49:19 -07:00
parent 40169c74ee
commit 2bbda09043
2 changed files with 16 additions and 2 deletions

View File

@@ -13658,8 +13658,15 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
} }
} }
if (openedGenericParams.empty()) if (openedGenericParams.empty()) {
return SolutionKind::Error; if (!shouldAttemptFixes())
return SolutionKind::Error;
return recordFix(AllowConcreteTypeSpecialization::create(
*this, type1, getConstraintLocator(locator)))
? SolutionKind::Error
: SolutionKind::Solved;
}
assert(openedGenericParams.size() == genericParams->size()); assert(openedGenericParams.size() == genericParams->size());

View File

@@ -6,6 +6,7 @@
@freestanding(expression) public macro Print<each Value>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro") @freestanding(expression) public macro Print<each Value>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
@freestanding(expression) public macro OtherPrint<each Value>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro") @freestanding(expression) public macro OtherPrint<each Value>(_ value: repeat each Value) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
@freestanding(expression) public macro ConcretePrint(_ value: Any) = #externalMacro(module: "MacroDefinition", type: "PrintMacro")
public struct Printer<Value> { public struct Printer<Value> {
init(_: (Value) -> Void) {} init(_: (Value) -> Void) {}
@@ -13,6 +14,7 @@ public struct Printer<Value> {
typealias Print = Printer typealias Print = Printer
typealias OtherPrint<T> = Printer<T> typealias OtherPrint<T> = Printer<T>
typealias ConcretePrint = Printer<Any>
struct Test { struct Test {
struct Object { struct Object {
@@ -27,6 +29,11 @@ struct Test {
let _ = OtherPrint<Object> { // Ok let _ = OtherPrint<Object> { // Ok
compute(root: $0, \.prop) compute(root: $0, \.prop)
} }
let _ = ConcretePrint<Object> { // expected-error {{cannot specialize non-generic type 'ConcretePrint' (aka 'Printer<Any>')}}
compute(root: $0, \.prop) // expected-error {{value of type 'Any' has no member 'prop'}}
// expected-note@-1 {{cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members}}
}
} }
func compute<R, V>(root: R, _: KeyPath<R, V>) {} func compute<R, V>(root: R, _: KeyPath<R, V>) {}