# # Test of generic types (negative tests) # # Copyright © 2023-2024 Samuel Lidén Borell # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # # ERROR .18: error: Empty type parameter definition type BadGeneric1<> = struct { } # ERROR .18: error: Bad type parameter definition type BadGeneric2<*> = struct { } # ERROR +2.20: error: Identifier already in use # ERROR .18: <-- other definition: T type BadGeneric3 = struct { } # ERROR .21: error: Expected an identifier here type BadGeneric4 = struct { } # ERROR .22: error: Expected an identifier here type BadGeneric5 = struct { } # ERROR .24: error: Missing , or > after type parameter name type BadGeneric6 = struct { } # ERROR .20: error: Empty type parameter definition type BadGeneric7 = struct { } # ERROR .18: error: Bad type parameter definition type BadGeneric8<,T> = struct { } # FIXME get rid of the first error # ERROR +2.20: error: Expected an identifier here # ERROR .19: error: Only reference types can be optional type BadGeneric9 = struct { } # ERROR .19: <-- definition: T type BadGeneric10 = struct { # FIXME get rid of the first error # ERROR +2.14: error: Type 'T' does not exist # ERROR .14: error: Type parameters can only be used within "slot" types Generic2 x } type BadGeneric11 = struct { # FIXME this should give an error! Generic2 x } type NonGeneric1 = struct { int x } type NonGeneric2 = struct { bool b int y } type NonGeneric3 = struct { int a int x } type Generic1 = struct { Generic2 same Generic2 fixed Generic2 also_same slot T elem } type Generic2 = struct { slot T elem } type Generic3 = struct { ref Generic2 a ref Generic2 b [2]Generic1 arr } type Generic4 = struct { Generic3 tt_ut Generic3 tt_uu Generic3 tt_uv Generic3 tu_ut Generic3 tu_uu Generic3 tu_uv Generic3 tv_ut Generic3 tv_uu Generic3 tv_uv } func generics_test1() { NonGeneric1 ng1 = (.x=1100) NonGeneric2 ng2 = (.b=true, .y=2200) NonGeneric3 ng3 = (.a=3300, .x=3400) # TODO test and attempting to assume specific types (should fail) # TODO test and assigning a non-ref type (should fail) # TODO test and assigning a non-enum type (should fail) # FIXME this should give an error! Generic2 bad_ng2 Generic2 g2_int = (.elem = 111) Generic2 g2 = (.elem = refto ng1) Generic1 g1 = ( .same = (.elem = refto ng1), # TODO get rid of repeated error # ERROR +2.27: error: Mismatch of named types # ERROR .33: error: Mismatch of named types .fixed = (.elem = refto ng2), .also_same = g2, # TODO get rid of repeated error # ERROR +2.17: error: Mismatch of named types # ERROR .23: error: Mismatch of named types .elem = refto ng2 ) var Generic2 g2_int_b = g2_int # ERROR .21: error: Incompatible types g2_int_b.elem = true # ERROR .21: error: Incompatible types assert g2_int_b.elem var Generic1 g1_b g1_b = g1 # ERROR +2.22: error: Mismatch of named types # ERROR .28: error: Mismatch of named types g1_b.same.elem = refto ng3 # ERROR +2.23: error: Mismatch of named types # ERROR .29: error: Mismatch of named types g1_b.fixed.elem = refto ng1 # ERROR +2.17: error: Mismatch of named types # ERROR .23: error: Mismatch of named types g1_b.elem = refto ng3 var Generic1 g1_bool = ( # ERROR .26: error: Incompatible types .same = (.elem = 123), # ERROR +2.27: error: Mismatch of named types # ERROR .33: error: Mismatch of named types .fixed = (.elem = refto ng2), # ERROR .31: error: Incompatible types .also_same = (.elem = g1), # ERROR .17: error: Incompatible types .elem = 123, ) # ERROR .15: error: Incompatible types. Nested type does not match g1_bool = g1_b # ERROR +2.26: error: Mismatch of named types # ERROR .32: error: Mismatch of named types g1_bool.fixed.elem = refto ng1 # ERROR .30: error: Incompatible types g1_bool.also_same.elem = 123 var Generic2 g2_bool = (.elem = true) var Generic2> g2_nested = ( # ERROR +2.17: error: Incompatible types. Nested types must match exactly # ERROR .23: error: Incompatible types. Nested types must match exactly .elem = refto g2_bool ) # ERROR +2.22: error: Incompatible types. Nested types must match exactly # ERROR .28: error: Incompatible types. Nested types must match exactly g2_nested.elem = refto g2_bool # ERROR .27: error: Incompatible types g2_nested.elem.elem = true # ERROR .17: error: Incompatible types. Nested type does not match g2_nested = g2_bool var Generic2> g2_nested_bool = ( # ERROR +2.17: error: Mismatch of named types # ERROR .23: error: Mismatch of named types .elem = refto g1 ) # ERROR .15: error: Incompatible types. Nested type does not match g2_bool = g2_nested_bool # ERROR .22: error: Incompatible types. Nested type does not match g2_nested_bool = g2_bool var Generic2>> g2_doublenested = ( # ERROR +2.17: error: Incompatible types. Nested types must match exactly # ERROR .23: error: Incompatible types. Nested types must match exactly .elem = refto g2_nested_bool ) # ERROR +2.28: error: Incompatible types. Nested types must match exactly # ERROR .34: error: Incompatible types. Nested types must match exactly g2_doublenested.elem = refto g2_nested_bool # ERROR +2.33: error: Incompatible types. Nested types must match exactly # ERROR .39: error: Incompatible types. Nested types must match exactly g2_doublenested.elem.elem = refto g2_bool # ERROR .38: error: Incompatible types g2_doublenested.elem.elem.elem = true # FIXME this should give an error! #g2_doublenested.elem.elem[1] = true var Generic3 g3_intbool = ( .a = refto g2_int, .b = refto g2_bool, .arr = [g1_bool, g1_bool] ) # ERROR +2.20: error: Incompatible types. Nested types must match exactly # ERROR .26: error: Incompatible types. Nested types must match exactly g3_intbool.a = refto g2_bool # ERROR +2.20: error: Incompatible types. Nested types must match exactly # ERROR .26: error: Incompatible types. Nested types must match exactly g3_intbool.b = refto g2_int # ERROR .32: error: Mismatch of named types g3_intbool.arr = [g1_bool, g2] # ERROR .32: error: Incompatible types. Nested type does not match g3_intbool.arr = [g1_bool, g1] # ERROR .25: error: Incompatible types g3_intbool.arr[1] = g1 # ERROR .23: error: Field "oops" does not exist g3_intbool.arr[1].oops = g1 # ERROR .16: error: Field "oops" does not exist g3_intbool.oops = g1 # FIXME this should give an error! #g3_intbool[1].oops1 = g1 # ERROR .5: error: Identifier 'oops' does not exist oops[0] = g1 var Generic3>, ref Generic2> g3_nested = ( .a = refto g2_doublenested, .b = refto g2_nested_bool, .arr = [ ( .same = (.elem = refto g2_bool), .fixed = (.elem = refto ng3), .also_same = (.elem = refto g2_bool), .elem = refto g2_bool, ), ( # ERROR +2.34: error: Incompatible types. Nested types must match exactly # ERROR .40: error: Incompatible types. Nested types must match exactly .same = (.elem = refto g2_int), # ERROR +2.35: error: Mismatch of named types # ERROR .41: error: Mismatch of named types .fixed = (.elem = refto ng2), # ERROR +2.39: error: Incompatible types. Nested types must match exactly # ERROR .45: error: Incompatible types. Nested types must match exactly .also_same = (.elem = refto g2_int), # ERROR +2.25: error: Incompatible types. Nested types must match exactly # ERROR .31: error: Incompatible types. Nested types must match exactly .elem = refto g2_int, ), ] ) var int i = 123 var ref var int intref = refto i var Generic4 g4_bound = get_g4_bound() # ERROR .63: error: Incompatible types. Nested types must match exactly var Generic4 g4_bound_bad1 = get_g4_bound() # ERROR .65: error: Incompatible types. Nested types must match exactly var Generic4 g4_bound_bad2 = get_g4_bound() #var Generic4 g4_params = get_g4_params(i, intref) # ERROR .37: error: Incompatible types. Nested types must match exactly g2_bool = deref g3_intbool.get_a() i = g3_intbool.get_a_elem() # ERROR +2.22: error: Incompatible types. Nested types must match exactly # ERROR .28: error: Incompatible types. Nested types must match exactly g3_intbool.set_b(refto g2_int) # ERROR .26: error: Incompatible types g3_intbool.set_inner(123) } func get_g4_bound() -> Generic4 { while true { } } # TODO func get_g4_params(slot A x, slot B y) -> var Generic4 { slot B b = y # ERROR .20: error: Incompatible types var slot A a = y # ERROR .9: error: Incompatible types a = y #{{ # TODO these should give errors get_g4_params(x, y) # type params should not remain bound here a = 123 a = none a = [123] var Generic4 ret = ( .tt_ut=gen_cc, .tt_uu=gen_ce, .tt_uv=gen_cb, .tu_ut=gen_ec, .tu_uu=gen_ee, .tu_uv=gen_eb, .tv_ut=gen_bc, # TODO should give an error .tv_uu=gen_bc, # wrong! .tv_uv=gen_bb, ) return ret #}} while true {} } func Generic3.get_a() -> ref Generic2 { while true { } } func Generic3.get_a_elem() -> slot T { while true { } } func Generic3.set_b(ref Generic2 value) { } func Generic3.set_inner(slot U inner) { } type NonGenericType = struct { int x } func NonGenericType.some_method() { # ERROR .10: error: Type parameters are not applicable on this type slot T a } func NonGenericType2.some_method() { # ERROR .10: error: Type parameters are not applicable on this type slot T a } type NonGenericType2 = struct { int x } func Generic1.some_method() { # ERROR .10: error: Type parameter does not exist slot U a }