# # Test of type compatibility (negative tests). # See also testexec/mainapp/typecompat.slul for positive tests. # # Copyright © 2022-2023 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 .34: error: Incompatible types data bool typecompat_incompat1 = "" # ERROR .36: error: Incompatible types data string typecompat_incompat2 = true # ERROR .35: error: Incompatible types. Array lengths differ data [1]int typecompat_int_arr1 = typecompat_int_arr data [2]int typecompat_int_arr = [11,22] # ERROR .35: error: Incompatible types. Array lengths differ data [3]int typecompat_int_arr3 = typecompat_int_arr # ERROR .37: error: Incompatible types. Nested types must match exactly data [2]bool typecompat_bool_arr1 = typecompat_int_arr # ERROR .40: error: Incompatible types. Nested types must match exactly data [2]uint64 typecompat_uint64_arr = typecompat_int_arr # ERROR +2.38: error: Incompatible types # ERROR .40: error: Incompatible types data [2]bool typecompat_bool_arr2 = [1,2] data [3,2]int typecompat_arr_deep = [[1,2], [3,4], [5,6]] # ERROR +4.42: error: Incompatible types. Array lengths differ # ERROR +3.43: error: Incompatible types. Array lengths differ # ERROR +2.50: error: Incompatible types. Array lengths differ # ERROR .57: error: Incompatible types. Array lengths differ data [2,3]int typecompat_arr_mismatch1 = [[1,2], [3,4], [5,6]] # ERROR .42: error: Incompatible types. Array lengths differ data [2,3]int typecompat_arr_mismatch2 = typecompat_arr_deep # ERROR .40: error: Incompatible types. Array lengths differ data [3]int typecompat_arr_mismatch3 = typecompat_arr_deep[0] type TypeCompat1 = int # used in tests of reference types type TypeCompatA = int type TypeCompatB = int data TypeCompatA typecompat_named1 = 123 # ERROR .38: error: Mismatch of named types data TypeCompatB typecompat_named2 = typecompat_named3 data TypeCompatA typecompat_named3 = 123 # ERROR .38: error: Mismatch of named types data TypeCompatB typecompat_named4 = typecompat_named3 data struct { int a } exprchk_struct1 = (.a=123) # ERROR .35: error: Struct types have different number of fields data struct { } exprchk_struct2 = exprchk_struct1 # ERROR .42: error: Incompatible types. Nested types must match exactly data struct { bool a } exprchk_struct3 = exprchk_struct1 type ExprChkStructTypeA = struct { int x # ERROR .5: <-- target type: ExprChkStructTypeA ref bool y } type ExprChkStructTypeB = struct { int x # ERROR .5: <-- target type: ExprChkStructTypeB ref int y } type ExprChkStructTypeC = struct { int x int y } func typecompat_named() { TypeCompatA a = 1 # ERROR .21: error: Mismatch of named types TypeCompatB b = a # ERROR +2.21: error: Mismatch of named types # ERROR .25: error: Mismatch of named types TypeCompatB c = a + a # ERROR .31: error: Mismatch of named types TypeCompatB d = (1+2) + ((a+3) + (4+5)) } func typecompat_refto_deref() { int i = 123 var int vi = 123 bool b = true var bool vb = true # TODO get rid of second error # ERROR +2.26: error: Incompatible references. Types must match exactly # ERROR .32: error: Incompatible types var ref var int ip = refto vb # ERROR .28: error: Incompatible references. Qualifiers must be compatible var ref var bool vbp = refto b # TODO require "aliased" qualifier in cases like this #var ref bool bp = refto vb # TODO get rid of second error # ERROR +2.10: error: Incompatible references. Types must match exactly # ERROR .16: error: Incompatible types ip = refto vb # ERROR .16: error: Incompatible types deref ip = b # ERROR .16: error: Incompatible types vi = deref vbp } func typecompat_get_open_type() -> ref OpenType { while true { } } func typecompat_open_types() { # ERROR .14: error: Open types can only be used inside a "ref" OpenType ot1 # ERROR .17: error: Open types can only be used inside a "ref" [2]OpenType ot2 # ERROR .21: error: Open types can only be used inside a "ref" ref [2]OpenType ot3 ref OpenType ot_ref = typecompat_get_open_type() ClosedType ct = (.x=123) # ERROR .20: error: Mismatch of named types deref ot_ref = ct # ERROR .20: error: Struct cannot be created outside its implementation module deref ot_ref = (.x=123) } func typecompat_builtins_ranges() { var int i = 123 var int8 i8 = 123 var int16 i16 = 123 var int32 i32 = 123 var int64 i64 = 123 var uint ui = 123 var byte u8 = 123 var uint16 u16 = 123 var uint32 u32 = 123 var uint64 u64 = 123 var wuint wui = 123 var wuint8 wu8 = 123 var wuint16 wu16 = 123 var wuint32 wu32 = 123 var wuint64 wu64 = 123 var usize us = 123 var ssize ss = 123 var fileoffs fo = 123 # ERROR .9: error: Incompatible types. Target type has shorter range i = i64 # ERROR .9: error: Incompatible types. Target type has shorter range i = u32 # ERROR .9: error: Incompatible types. Target type has shorter range i = u64 # ERROR .9: error: Incompatible types. Target type has shorter range i = wu32 # ERROR .9: error: Incompatible types. Target type has shorter range i = wu64 # ERROR .9: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i = us # ERROR .9: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i = ss # ERROR .9: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i = fo # ERROR .10: error: Incompatible types. Target type has shorter range i8 = i # ERROR .10: error: Incompatible types. Target type has shorter range i8 = i16 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = i32 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = i64 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = ui # ERROR .10: error: Incompatible types. Target type has shorter range i8 = u8 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = u16 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = u32 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = u64 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = wui # ERROR .10: error: Incompatible types. Target type has shorter range i8 = wu8 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = wu16 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = wu32 # ERROR .10: error: Incompatible types. Target type has shorter range i8 = wu64 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i8 = us # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i8 = ss # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i8 = fo # ERROR .11: error: Incompatible types. Target type has shorter range i16 = i # ERROR .11: error: Incompatible types. Target type has shorter range i16 = i32 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = i64 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = ui # ERROR .11: error: Incompatible types. Target type has shorter range i16 = u16 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = u32 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = u64 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = wui # ERROR .11: error: Incompatible types. Target type has shorter range i16 = wu16 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = wu32 # ERROR .11: error: Incompatible types. Target type has shorter range i16 = wu64 # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i16 = us # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i16 = ss # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i16 = fo # ERROR .11: error: Incompatible types. Target type has shorter range i32 = i64 # ERROR .11: error: Incompatible types. Target type has shorter range i32 = ui # ERROR .11: error: Incompatible types. Target type has shorter range i32 = u32 # ERROR .11: error: Incompatible types. Target type has shorter range i32 = u64 # ERROR .11: error: Incompatible types. Target type has shorter range i32 = wu32 # ERROR .11: error: Incompatible types. Target type has shorter range i32 = wu64 # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i32 = us # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i32 = ss # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i32 = fo # ERROR .11: error: Incompatible types. Target type has shorter range i64 = u64 # ERROR .11: error: Incompatible types. Target type has shorter range i64 = wu64 # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i64 = us # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i64 = ss # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range i64 = fo # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = i # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = i8 # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = i16 # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = i32 # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = i64 # ERROR .10: error: Incompatible types. Target type has shorter range ui = u64 # ERROR .10: error: Incompatible types. Target type has shorter range ui = wu64 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range ui = us # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = ss # ERROR .10: error: Incompatible types. Target type does not accept signed types ui = fo # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = i # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = i8 # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = i16 # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = i32 # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = i64 # ERROR .10: error: Incompatible types. Target type has shorter range u8 = ui # ERROR .10: error: Incompatible types. Target type has shorter range u8 = u16 # ERROR .10: error: Incompatible types. Target type has shorter range u8 = u32 # ERROR .10: error: Incompatible types. Target type has shorter range u8 = u64 # ERROR .10: error: Incompatible types. Target type has shorter range u8 = wui # ERROR .10: error: Incompatible types. Target type has shorter range u8 = wu16 # ERROR .10: error: Incompatible types. Target type has shorter range u8 = wu32 # ERROR .10: error: Incompatible types. Target type has shorter range u8 = wu64 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range u8 = us # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = ss # ERROR .10: error: Incompatible types. Target type does not accept signed types u8 = fo # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = i # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = i8 # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = i16 # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = i32 # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = i64 # ERROR .11: error: Incompatible types. Target type has shorter range u16 = ui # ERROR .11: error: Incompatible types. Target type has shorter range u16 = u32 # ERROR .11: error: Incompatible types. Target type has shorter range u16 = u64 # ERROR .11: error: Incompatible types. Target type has shorter range u16 = wui # ERROR .11: error: Incompatible types. Target type has shorter range u16 = wu32 # ERROR .11: error: Incompatible types. Target type has shorter range u16 = wu64 # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range u16 = us # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = ss # ERROR .11: error: Incompatible types. Target type does not accept signed types u16 = fo # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = i # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = i8 # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = i16 # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = i32 # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = i64 # ERROR .11: error: Incompatible types. Target type has shorter range u32 = u64 # ERROR .11: error: Incompatible types. Target type has shorter range u32 = wu64 # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range u32 = us # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = ss # ERROR .11: error: Incompatible types. Target type does not accept signed types u32 = fo # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = i # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = i8 # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = i16 # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = i32 # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = i64 # ERROR .11: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range u64 = us # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = ss # ERROR .11: error: Incompatible types. Target type does not accept signed types u64 = fo # ERROR .12: error: Incompatible types. Target type does not accept signed types wu64 = i # ERROR .11: error: Incompatible types. Target type has shorter range wu8 = u16 # ERROR .12: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range wu64 = us # ERROR .12: error: Incompatible types. Target type does not accept signed types wu64 = ss # ERROR .12: error: Incompatible types. Target type does not accept signed types wu64 = fo # ERROR .10: error: Incompatible types. Target type does not accept signed types us = i # ERROR .10: error: Incompatible types. Target type does not accept signed types us = i8 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range (but at least 32 bits) us = u64 # ERROR .10: error: Incompatible types. Target type does not accept signed types us = ss # ERROR .10: error: Incompatible types. Target type does not accept signed types us = fo # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range (but at least 32 bits) ss = u32 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range (but at least 32 bits) ss = u64 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range ss = us # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range ss = fo # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range (but at least 32 bits) fo = u32 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range (but at least 32 bits) fo = u64 # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range fo = us # ERROR .10: error: Incompatible types. usize/ssize/fileoffs types have system-dependent range fo = ss } func get_ref() -> ref var TypeCompat1 { while true {} } func get_arena() -> arena var TypeCompat1 { while true {} } func get_own() -> own var TypeCompat1 { while true {} } func take_ref(ref TypeCompat1 x) { while true {} } func take_arena(arena TypeCompat1 x) { while true {} } func take_own(own TypeCompat1 x) { while true {} } func get_refref() -> ref ref var TypeCompat1 { while true {} } func take_refref(ref ref var TypeCompat1 x) { while true {} } func get_const_ref() -> ref TypeCompat1 { while true {} } func typecompat_refs() { # ERROR .23: error: Incompatible types take_arena(get_ref()) # ERROR .21: error: Incompatible types take_own(get_ref()) # ERROR .36: error: Incompatible types own TypeCompat1 owned = get_ref() take_own(owned) ref TypeCompat1 refd = get_ref() # ERROR .14: error: Incompatible types take_own(refd) # ERROR .33: error: Incompatible references. Types must match exactly ref bool wrongtype = get_ref() # ERROR .37: error: Incompatible nested reference types ref ref var int rr1 = get_refref() # ERROR .41: error: Incompatible nested reference types. Qualifiers must be exactly the same ref ref TypeCompat1 rr2 = get_refref() # ERROR .17: error: Incompatible nested reference types. Qualifiers must be exactly the same take_refref(rr2) # ERROR .47: error: Incompatible references. Qualifiers must be compatible ref var TypeCompat1 quals1 = get_const_ref() # ERROR .53: error: Incompatible nested reference types. Qualifiers must be exactly the same ref ref threaded TypeCompat1 quals2 = get_refref() } func typecompat_structs() { var struct { int x int y } ac = (.x=0, .y=0) # ERROR +4.12: error: Incompatible types. Nested type does not match var struct { int x ref int y } ab = ac # ERROR +4.12: error: Incompatible types. Nested types must match exactly var struct { int x ref bool y } aa = ab var ExprChkStructTypeC nc = (.x=0, .y=0) # ERROR .33: error: Incompatible types. Nested type does not match var ExprChkStructTypeA na = ac # ERROR .33: error: Incompatible types. Nested type does not match var ExprChkStructTypeB nb = ac # TODO allow anonymous enum types in local variables? and forbid anonymous struct types in parameters? # XXX related: add "," as a separator? e.g. type T = struct { int x, bool b } na = na nb = nb nc = nc aa = aa ab = ab ac = ac na = aa nb = ab nc = ac aa = na ab = nb ac = nc # ERROR .10: error: Mismatch of named types na = nb # ERROR .10: error: Mismatch of named types na = nc # ERROR .10: error: Mismatch of named types nb = na # ERROR .10: error: Mismatch of named types nb = nc # ERROR .10: error: Mismatch of named types nc = na # ERROR .10: error: Mismatch of named types nc = nb # ERROR .10: error: Incompatible types. Nested types must match exactly aa = ab # ERROR .10: error: Incompatible types. Nested type does not match aa = ac # ERROR .10: error: Incompatible types. Nested types must match exactly ab = aa # ERROR .10: error: Incompatible types. Nested type does not match ab = ac # ERROR .10: error: Incompatible types. Nested type does not match ac = aa # ERROR .10: error: Incompatible types. Nested type does not match ac = ab }