compiler: disallow named types redeclaration via contract config

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
This commit is contained in:
Anna Shaleva 2023-05-29 18:50:36 +03:00
parent 865bd6c9cc
commit 44dfe8342d
4 changed files with 34 additions and 1 deletions

View file

@ -547,4 +547,7 @@ func TestCompile_GuessEventTypes(t *testing.T) {
t.Run("extended types mismatch", func(t *testing.T) { t.Run("extended types mismatch", func(t *testing.T) {
check(t, filepath.Join("testdata", "invalid8"), "inconsistent usages of event `SomeEvent`: extended type of param #0 mismatch") check(t, filepath.Join("testdata", "invalid8"), "inconsistent usages of event `SomeEvent`: extended type of param #0 mismatch")
}) })
t.Run("named types redeclare", func(t *testing.T) {
check(t, filepath.Join("testdata", "invalid9"), "configured declared named type intersects with the contract's one: `invalid9.NamedStruct`")
})
} }

View file

@ -0,0 +1,12 @@
package invalid9
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
type NamedStruct struct {
SomeInt int
}
func Main() NamedStruct {
runtime.Notify("SomeEvent", []interface{}{123})
return NamedStruct{SomeInt: 123}
}

View file

@ -0,0 +1,16 @@
name: Test undeclared event
events:
- name: SomeEvent
parameters:
- name: p1
type: Array
extendedtype:
base: Array
name: invalid9.NamedStruct
namedtypes:
invalid9.NamedStruct:
base: Array
name: invalid9.NamedStruct
fields:
- field: SomeInt
base: Integer

View file

@ -340,7 +340,9 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
cfg.NamedTypes = di.NamedTypes cfg.NamedTypes = di.NamedTypes
} }
for name, et := range o.DeclaredNamedTypes { for name, et := range o.DeclaredNamedTypes {
// TODO: handle name conflict (it can happen due to invalid user input e.g.) if _, ok := cfg.NamedTypes[name]; ok {
return nil, fmt.Errorf("configured declared named type intersects with the contract's one: `%s`", name)
}
cfg.NamedTypes[name] = et cfg.NamedTypes[name] = et
} }
for _, e := range o.ContractEvents { for _, e := range o.ContractEvents {