Files
@ c87205ed6292
Branch filter:
Location: CSY/reowolf/src/protocol/tests/parser_inference.rs
c87205ed6292
8.6 KiB
application/rls-services+xml
cleanup consume_type function name and rename ParseError
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | /// parser_inference.rs
///
/// Simple tests for the type inferences
use super::*;
#[test]
fn test_integer_inference() {
Tester::new_single_source_expect_ok(
"by arguments",
"
int call(byte b, short s, int i, long l) {
auto b2 = b;
auto s2 = s;
auto i2 = i;
auto l2 = l;
return i2;
}
"
).for_function("call", |f| { f
.for_variable("b2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("s2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("short");
})
.for_variable("i2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("int");
})
.for_variable("l2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("long");
});
});
Tester::new_single_source_expect_ok(
"by assignment",
"
int call() {
byte b1 = 0; short s1 = 0; int i1 = 0; long l1 = 0;
auto b2 = b1;
auto s2 = s1;
auto i2 = i1;
auto l2 = l1;
return 0;
}"
).for_function("call", |f| { f
.for_variable("b2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("s2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("short");
})
.for_variable("i2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("int");
})
.for_variable("l2", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("long");
});
});
}
#[test]
fn test_binary_expr_inference() {
Tester::new_single_source_expect_ok(
"compatible types",
"int call() {
byte b0 = 0;
byte b1 = 1;
short s0 = 0;
short s1 = 1;
int i0 = 0;
int i1 = 1;
long l0 = 0;
long l1 = 1;
auto b = b0 + b1;
auto s = s0 + s1;
auto i = i0 + i1;
auto l = l0 + l1;
return i;
}"
).for_function("call", |f| { f
.for_expression_by_source(
"b0 + b1", "+",
|e| { e.assert_concrete_type("byte"); }
)
.for_expression_by_source(
"s0 + s1", "+",
|e| { e.assert_concrete_type("short"); }
)
.for_expression_by_source(
"i0 + i1", "+",
|e| { e.assert_concrete_type("int"); }
)
.for_expression_by_source(
"l0 + l1", "+",
|e| { e.assert_concrete_type("long"); }
);
});
Tester::new_single_source_expect_err(
"incompatible types",
"int call() {
byte b = 0;
long l = 1;
auto r = b + l;
return 0;
}"
).error(|e| { e
.assert_ctx_has(0, "b + l")
.assert_msg_has(0, "cannot apply")
.assert_occurs_at(0, "+")
.assert_msg_has(1, "has type 'byte'")
.assert_msg_has(2, "has type 'long'");
});
}
#[test]
fn test_struct_inference() {
Tester::new_single_source_expect_ok(
"by function calls",
"
struct Pair<T1, T2>{ T1 first, T2 second }
Pair<T1, T2> construct<T1, T2>(T1 first, T2 second) {
return Pair{ first: first, second: second };
}
int fix_t1<T2>(Pair<byte, T2> arg) { return 0; }
int fix_t2<T1>(Pair<T1, int> arg) { return 0; }
int test() {
auto first = 0;
auto second = 1;
auto pair = construct(first, second);
fix_t1(pair);
fix_t2(pair);
return 0;
}
"
).for_function("test", |f| { f
.for_variable("first", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("second", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("int");
})
.for_variable("pair", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("Pair<byte,int>");
});
});
Tester::new_single_source_expect_ok(
"by field access",
"
struct Pair<T1, T2>{ T1 first, T2 second }
Pair<T1, T2> construct<T1, T2>(T1 first, T2 second) {
return Pair{ first: first, second: second };
}
int test() {
auto first = 0;
auto second = 1;
auto pair = construct(first, second);
byte assign_first = 0;
long assign_second = 1;
pair.first = assign_first;
pair.second = assign_second;
return 0;
}
"
).for_function("test", |f| { f
.for_variable("first", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("byte");
})
.for_variable("second", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("long");
})
.for_variable("pair", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("Pair<byte,long>");
});
});
Tester::new_single_source_expect_ok(
"by nested field access",
"
struct Node<T1, T2>{ T1 l, T2 r }
Node<T1, T2> construct<T1, T2>(T1 l, T2 r) { return Node{ l: l, r: r }; }
int fix_poly<T>(Node<T, T> a) { return 0; }
int test() {
byte assigned = 0;
auto thing = construct(assigned, construct(0, 1));
fix_poly(thing.r);
thing.r.r = assigned;
return 0;
}
",
).for_function("test", |f| { f
.for_variable("thing", |v| { v
.assert_parser_type("auto")
.assert_concrete_type("Node<byte,Node<byte,byte>>");
});
});
}
#[test]
fn test_failed_polymorph_inference() {
Tester::new_single_source_expect_err(
"function call inference mismatch",
"
int poly<T>(T a, T b) { return 0; }
int call() {
byte first_arg = 5;
long second_arg = 2;
return poly(first_arg, second_arg);
}
"
).error(|e| { e
.assert_num(3)
.assert_ctx_has(0, "poly(first_arg, second_arg)")
.assert_occurs_at(0, "poly")
.assert_msg_has(0, "Conflicting type for polymorphic variable 'T'")
.assert_occurs_at(1, "second_arg")
.assert_msg_has(1, "inferred it to 'long'")
.assert_occurs_at(2, "first_arg")
.assert_msg_has(2, "inferred it to 'byte'");
});
Tester::new_single_source_expect_err(
"struct literal inference mismatch",
"
struct Pair<T>{ T first, T second }
int call() {
byte first_arg = 5;
long second_arg = 2;
auto pair = Pair{ first: first_arg, second: second_arg };
return 3;
}
"
).error(|e| { e
.assert_num(3)
.assert_ctx_has(0, "Pair{ first: first_arg, second: second_arg }")
.assert_occurs_at(0, "Pair{")
.assert_msg_has(0, "Conflicting type for polymorphic variable 'T'")
.assert_occurs_at(1, "second_arg")
.assert_msg_has(1, "inferred it to 'long'")
.assert_occurs_at(2, "first_arg")
.assert_msg_has(2, "inferred it to 'byte'");
});
Tester::new_single_source_expect_err(
"field access inference mismatch",
"
struct Holder<Shazam>{ Shazam a }
int call() {
byte to_hold = 0;
auto holder = Holder{ a: to_hold };
return holder.a;
}
"
).error(|e| { e
.assert_num(3)
.assert_ctx_has(0, "holder.a")
.assert_occurs_at(0, ".")
.assert_msg_has(0, "Conflicting type for polymorphic variable 'Shazam'")
.assert_msg_has(1, "inferred it to 'byte'")
.assert_msg_has(2, "inferred it to 'int'");
});
// TODO: Needs better error messages anyway, but this failed before
Tester::new_single_source_expect_err(
"by nested field access",
"
struct Node<T1, T2>{ T1 l, T2 r }
Node<T1, T2> construct<T1, T2>(T1 l, T2 r) { return Node{ l: l, r: r }; }
int fix_poly<T>(Node<T, T> a) { return 0; }
int test() {
byte assigned = 0;
long another = 1;
auto thing = construct(assigned, construct(another, 1));
fix_poly(thing.r);
thing.r.r = assigned;
return 0;
}
",
);
}
|