diff --git a/rlp/rlpgen/gen.go b/rlp/rlpgen/gen.go index 150797c7aa5..7ec38a4c38f 100644 --- a/rlp/rlpgen/gen.go +++ b/rlp/rlpgen/gen.go @@ -673,7 +673,7 @@ func (op sliceOp) genDecode(ctx *genContext) (string, string) { } func (bctx *buildContext) makeOp(name *types.Named, typ types.Type, tags rlpstruct.Tags) (op, error) { - switch typ := typ.(type) { + switch typ := types.Unalias(typ).(type) { case *types.Named: if isBigInt(typ) { return bigIntOp{}, nil diff --git a/rlp/rlpgen/gen_test.go b/rlp/rlpgen/gen_test.go index 3b4f5df2876..be439902610 100644 --- a/rlp/rlpgen/gen_test.go +++ b/rlp/rlpgen/gen_test.go @@ -47,7 +47,7 @@ func init() { } } -var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256"} +var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256", "alias"} func TestOutput(t *testing.T) { for _, test := range tests { diff --git a/rlp/rlpgen/testdata/alias.in.txt b/rlp/rlpgen/testdata/alias.in.txt new file mode 100644 index 00000000000..c7aa8a3106f --- /dev/null +++ b/rlp/rlpgen/testdata/alias.in.txt @@ -0,0 +1,22 @@ +// -*- mode: go -*- + +package test + +import ( + "math/big" + "github.com/holiman/uint256" +) + +// Alias types chosen because their originals have special handling that is easy +// to spot when inspecting generated output. +type ( + Big = big.Int + // Demonstrate recursive unaliasing + intermediate = uint256.Int + Uint256 = intermediate +) + +type Test struct { + BigAlias Big + Uint256Alias Uint256 +} diff --git a/rlp/rlpgen/testdata/alias.out.txt b/rlp/rlpgen/testdata/alias.out.txt new file mode 100644 index 00000000000..0746f974946 --- /dev/null +++ b/rlp/rlpgen/testdata/alias.out.txt @@ -0,0 +1,43 @@ +package test + +import "github.com/ava-labs/libevm/rlp" +import "github.com/holiman/uint256" +import "io" + +func (obj *Test) EncodeRLP(_w io.Writer) error { + w := rlp.NewEncoderBuffer(_w) + _tmp0 := w.List() + if obj.BigAlias.Sign() == -1 { + return rlp.ErrNegativeBigInt + } + w.WriteBigInt(&obj.BigAlias) + w.WriteUint256(&obj.Uint256Alias) + w.ListEnd(_tmp0) + return w.Flush() +} + +func (obj *Test) DecodeRLP(dec *rlp.Stream) error { + var _tmp0 Test + { + if _, err := dec.List(); err != nil { + return err + } + // BigAlias: + _tmp1, err := dec.BigInt() + if err != nil { + return err + } + _tmp0.BigAlias = (*_tmp1) + // Uint256Alias: + var _tmp2 uint256.Int + if err := dec.ReadUint256(&_tmp2); err != nil { + return err + } + _tmp0.Uint256Alias = _tmp2 + if err := dec.ListEnd(); err != nil { + return err + } + } + *obj = _tmp0 + return nil +}