-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
checker: disallow indexing generic struct receivers #19949
Conversation
…ath { sin, cos }`" This reverts commit ddf0468.
s := typ_sym.info as ast.Struct | ||
if s.is_generic && typ.has_flag(.generic) && !node.is_map && !node.is_array | ||
&& !node.is_farray && !node.is_gated { | ||
c.error('cannot index a generic', node.pos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c.error('cannot index a generic', node.pos) | |
c.error('cannot index a generic struct', node.pos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imho, adding a test like this, will also be good:
struct Number[T] {
value T
}
fn f[T](numbers &Number[T], idx int) T {
return unsafe { numbers[idx].value }
}
fn test_indexing_a_pointer_to_generic_instances() {
numbers := [10]Number[int]{init: Number[int]{
value: index * 10
}}
assert f(&numbers[0], 3) == 30
assert f(&numbers[0], 9) == 90
}
It is a useful thing to have, and it compiles and runs on master, but fails with the current implementation of this PR.
Fixes #18707
🤖[deprecated] Generated by Copilot at 5ce04ba
Add a check and a test case for invalid index expressions on generic types. This fixes a compiler error or a runtime panic when trying to access a generic type by index that is not a map, array, fixed array, or gated type.
🤖[deprecated] Generated by Copilot at 5ce04ba