Files
swift-mirror/test/Sema/value_generics.swift
Hamish Knight f8ab391737 Introduce type sugar for InlineArray (#80087)
* [CS] Decline to handle InlineArray in shrink

Previously we would try the contextual type `(<int>, <element>)`,
which is wrong. Given we want to eliminate shrink, let's just bail.

* [Sema] Sink `ValueMatchVisitor` into `applyUnboundGenericArguments`

Make sure it's called for sugar code paths too. Also let's just always
run it since it should be a pretty cheap check.

* [Sema] Diagnose passing integer to non-integer type parameter

This was previously missed, though would have been diagnosed later
as a requirement failure.

* [Parse] Split up `canParseType` 

While here, address the FIXME in `canParseTypeSimpleOrComposition`
and only check to see if we can parse a type-simple, including
`each`, `some`, and `any` for better recovery.

* Introduce type sugar for InlineArray

Parse e.g `[3 x Int]` as type sugar for InlineArray. Gated behind
an experimental feature flag for now.
2025-03-23 15:31:37 -07:00

122 lines
5.1 KiB
Swift

// RUN: %target-typecheck-verify-swift -disable-availability-checking
protocol P {}
func invalid<let N>() {} // expected-error {{value generic 'N' must have an explicit value type declared}}
// expected-error@-1 {{generic parameter 'N' is not used in function signature}}
func invalid<let N>(_: A<N>) {} // expected-error {{value generic 'N' must have an explicit value type declared}}
struct A<let N: Int> {
var int: Int {
N // OK
}
var uint: UInt {
N // expected-error {{cannot convert return expression of type 'Int' to return type 'UInt'}}
}
var goodUInt: UInt {
UInt(N) // OK
}
var intSelf: Int {
N.self // OK
}
var n: N { // expected-error {{using value generic 'N' here is not allowed}}
fatalError()
}
var nType: N.Type { // expected-error {{using value generic 'N' here is not allowed}}
fatalError()
}
var a: A<N> { // OK
fatalError()
}
}
extension A where N: P {} // expected-error {{value generic type 'N' cannot conform to protocol 'P'}}
extension A where N == Int {} // expected-error {{cannot constrain value parameter 'N' to be type 'Int'}}
extension A where N == 123 {
func thing() -> Int {
N // OK (this used to crash the compiler in a concrete case 'where N == 123')
}
}
func b(with a: A<123>) {} // OK
func c<let M: Int>(with a: A<M>) {} // OK
func d<T>(with a: A<T>) {} // expected-error {{cannot pass type 'T' as a value for generic value 'N'}}
func e(with a: A<Int>) {} // expected-error {{cannot pass type 'Int' as a value for generic value 'N'}}
struct Generic<T: ~Copyable & ~Escapable> {}
struct GenericWithIntParam<T: ~Copyable & ~Escapable, let N: Int> {}
extension Generic where T == 123 {} // expected-error {{cannot constrain type parameter 'T' to be integer '123'}}
extension Generic where T == 123.Type {} // expected-error {{cannot constrain type parameter 'T' to be integer '123'}}
// expected-error@-1 {{expected '{' in extension}}
extension Generic where T == 123? {} // expected-error {{cannot constrain type parameter 'T' to be integer '123'}}
// expected-error@-1 {{expected '{' in extension}}
func f(_: Generic<123>) {} // expected-error {{integer unexpectedly used in a type position}}
func g<let N: Int>(_: Generic<N>) {} // expected-error {{cannot use value type 'N' for generic argument 'T'}}
func h(_: (Int, 123)) {} // expected-error {{expected type}}
func i(_: () -> 123) {} // expected-error {{expected type}}
func j(_: (A<123>) -> ()) {} // OK
func k(_: some 123) {} // expected-error {{expected parameter type following ':'}}
func l(_: GenericWithIntParam<123, Int>) {}
// expected-error@-1 {{cannot pass type 'Int' as a value for generic value 'N'}}
// expected-error@-2 {{cannot use value type '123' for generic argument 'T'}}
func m(_: GenericWithIntParam<Int, 123>) {} // OK
typealias One = 1 // expected-error {{expected type in type alias declaration}}
struct B<let N: UInt8> {} // expected-error {{'UInt8' is not a supported value type for 'N'}}
struct C<let N: Int, let M: Int> {}
extension C where N == 123 { // expected-note {{where 'N' = '0'}}
// expected-note@-1 {{where 'N' = '0'}}
// expected-note@-2 {{where 'N' = 'T'}}
func nIs123() {}
}
extension C where M == 321 { // expected-note {{where 'M' = '0'}}
// expected-note@-1 {{where 'M' = '0'}}
// expected-note@-2 {{where 'M' = 'T'}}
func mIs123() {}
}
extension C where N == M { // expected-note {{where 'N' = '123', 'M' = '0'}}
// expected-note@-1 {{where 'N' = '0', 'M' = '321'}}
func nAndMAreBothEqual() {}
}
func testC1(with c: C<0, 0>) {
c.nIs123() // expected-error {{referencing instance method 'nIs123()' on 'C' requires the types '0' and '123' be equivalent}}
c.mIs123() // expected-error {{referencing instance method 'mIs123()' on 'C' requires the types '0' and '321' be equivalent}}
c.nAndMAreBothEqual() // OK
}
func testC2(with c: C<123, 0>) {
c.nIs123() // OK
c.mIs123() // expected-error {{referencing instance method 'mIs123()' on 'C' requires the types '0' and '321' be equivalent}}
c.nAndMAreBothEqual() // expected-error {{referencing instance method 'nAndMAreBothEqual()' on 'C' requires the types '123' and '0' be equivalent}}
}
func testC3(with c: C<0, 321>) {
c.nIs123() // expected-error {{referencing instance method 'nIs123()' on 'C' requires the types '0' and '123' be equivalent}}
c.mIs123() // OK
c.nAndMAreBothEqual() // expected-error {{referencing instance method 'nAndMAreBothEqual()' on 'C' requires the types '0' and '321' be equivalent}}
}
func testC4<let T: Int>(with c: C<T, T>) {
c.nIs123() // expected-error {{referencing instance method 'nIs123()' on 'C' requires the types 'T' and '123' be equivalent}}
c.mIs123() // expected-error {{referencing instance method 'mIs123()' on 'C' requires the types 'T' and '321' be equivalent}}
c.nAndMAreBothEqual() // OK
}
struct D<let N: Int & P> {} // expected-error {{non-protocol, non-class type 'Int' cannot be used within a protocol-constrained type}}