-
Notifications
You must be signed in to change notification settings - Fork 375
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
nil can be converted to invalid types (ie. int) #1341
Comments
I have written the following test: package gnolang
func TestNilTypeConversion(t *testing.T) {
m := NewMachine("test", nil)
testCases := []struct {
name string
code string
}{
{
name: "NilToInt",
code: `package test
func main(){
println(int(nil))
}`,
},
// converting to bool, and other types
}
for _, tc := range testCases {
t.Run(tc.name, function(t *testing.T) {
n := MustParseFile("main.go", tc.code)
defer func() {
if r := recover(); r != nil {
t.Logf("Recovered from panic as expected: %v", r)
} else {
t.Errorf("Expected a panic for invalid type conversion")
}
}()
m.RunFiles(n)
m.RunMain()
if err := m.CheckEmpty(); err != nil {
t.Fatal(err)
}
})
}
} If the code is not changed, no error occurs and the test fails. However, if you make a panic in ConvertTo as below, the test passes. if tv.IsUndefined() {
+ panic(fmt.Sprintf(
+ "error: cannot convert undefined to %s",
+ t.String()))
- tv.T = t
- return
} It seems like the problem arises when a But I'm still not sure, so I'll have to dig more 🤔🤔 |
This is a particular case of Go append builtin where the array type is inferred from the first argument. We detect an untyped const passed as argument, then we convert it to the array type prior to proceed further. It fixes further consistency checks and value generation. Fixes gnolang#1136, fixes gnolang#1149, fixes gnolang#1341. <!-- please provide a detailed description of the changes made in this pull request. --> <details><summary>Contributors' checklist...</summary> - [x] Added new tests, or not needed, or not feasible - [x] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [x] Updated the official documentation or not needed - [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [x] Added references to related issues and PRs - [ ] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md). </details> --------- Co-authored-by: Morgan <git@howl.moe> Co-authored-by: Morgan <morgan@morganbaz.com>
First discovered in #1177 (comment) , I think I have forgotten to make an issue to track it separately.
Sample program:
int(nil)
makes no sense and should not be accepted.The text was updated successfully, but these errors were encountered: