-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Fixed error handling when appending empty array literal to 2D array #24002
base: master
Are you sure you want to change the base?
Conversation
Connected to Huly®: V_0.6-22395 |
I do not get it - why would one want to append an empty array to another array? |
An empty array might be used as a placeholder or for dynamically constructing a 2D array (matrix). Currently, the compiler attempts to infer the type of an empty array when no type is specified and then attempts to append it to the matrix. This does not result in a no-op (NOP) but rather causes a 'C error' if type inference fails. With this fix, the compiler now errors explicitly due to a typing issue and provides a clear, actionable error message rather than just a C error. |
Alternatively, why doesn't the compiler infer correctly? When appending |
In the case of a 2D array, For adding empty [], that distinction is moot though, since in both cases, it should either not cgen error (since adding an empty array should be a NOP), or it should error at the checker stage (which this PR does). |
i.e. I do not question the value of turning the cgen error into a more actionable checker error. I am just not convinced, that the compiler should not just ignore the statement |
Placeholder as in adding an empty array to the array of arrays, which can be filled in later. Using a variation of the original example, doing mut rows := [][]string{}
println(rows)
rows << []
println(rows)
rows << []
println(rows)
rows[0] << 's'
println(rows) would be expected to give the same results as mut rows := [][]string{}
println(rows)
rows << []string{}
println(rows)
rows << []string{}
println(rows)
rows[0] << 's'
println(rows) which is
|
I.e. modify cgen to consider That can work too. |
Resolves #23854
This PR improves error handling when appending an empty array literal
[]
to a 2D array such as[][]string
.Currently, doing this results in a cryptic C error because the compiler fails to infer the correct type of the empty literal. The operation is not marked invalid, but compilation fails downstream.
This fix adds a graceful error message during type checking, guiding the user to use an explicitly typed empty array like
[]string{}
.Example:
Before:
After:
error: cannot infer type for empty array literal `[]`. Use `[]string{}` instead.
Test: