Fix missing check on foreach indexing.

This commit is contained in:
Christoffer Lerno
2022-11-14 15:05:01 +01:00
parent 5ff726d8d1
commit c093f16fd0
3 changed files with 60 additions and 0 deletions

View File

@@ -1242,7 +1242,13 @@ static inline bool sema_analyse_foreach_stmt(SemaContext *context, Ast *statemen
return false;
}
index_macro = value_by_ref ? by_ref : by_val;
assert(index_macro);
index_type = index_macro->func_decl.signature.params[1]->type;
if (!type_is_integer(index_type))
{
SEMA_ERROR(enumerator, "Only integer indexed types may be used with foreach.");
return false;
}
TypeInfoId rtype = index_macro->func_decl.signature.rtype;
value_type = rtype ? type_infoptr(rtype)->type : NULL;
}

View File

@@ -0,0 +1,27 @@
import std::io;
struct Foo
{
int[3] elements;
}
fn int Foo.at(Foo *vector, float index) @operator([])
{
return 1;
}
fn int Foo.len(Foo *vector) @operator(len)
{
return 3;
}
fn void main()
{
Foo f;
io::printfln("%s", f[12.2]);
foreach (int i, value : f) // #error: Only integer
{
io::printfln("v[%s] = %s", i, value);
}
}

View File

@@ -0,0 +1,27 @@
import std::io;
struct Foo
{
int[3] elements;
}
fn int Foo.at(Foo *vector, float index) @operator([])
{
return 1;
}
fn int Foo.len(Foo *vector) @operator(len)
{
return 3;
}
fn void main()
{
Foo f;
io::printfln("%s", f[12.2]);
foreach (int i, value : f) // #error: Only integer
{
io::printfln("v[%s] = %s", i, value);
}
}