mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fix missing check on foreach indexing.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
27
test/test_suite/statements/foreach_wrong_index.c3
Normal file
27
test/test_suite/statements/foreach_wrong_index.c3
Normal 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);
|
||||
}
|
||||
}
|
||||
27
test/test_suite2/statements/foreach_wrong_index.c3
Normal file
27
test/test_suite2/statements/foreach_wrong_index.c3
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user