AstGen: add error for invalid string comparisons

These operations are allowed because the string literals are just
pointers but they produce unexpected results. These errors prevent
beginners from shooting themselves in the foot while still allowing
advanced users to circumvent them if they desire to do so.

Closes #8290
This commit is contained in:
Veikka Tuominen
2022-12-02 20:16:47 +02:00
parent 0e38cc16d5
commit e2509ddbe6
3 changed files with 43 additions and 1 deletions

View File

@@ -0,0 +1,29 @@
comptime {
var a = "foo";
if (a == "foo") unreachable;
}
comptime {
var a = "foo";
if (a == ("foo")) unreachable; // intentionally allow
}
comptime {
var a = "foo";
switch (a) {
"foo" => unreachable,
else => {},
}
}
comptime {
var a = "foo";
switch (a) {
("foo") => unreachable, // intentionally allow
else => {},
}
}
// error
// backend=stage2
// target=native
//
// :3:11: error: cannot compare strings with ==
// :12:9: error: cannot switch on strings