Skip to content

Commit 96d49b8

Browse files
committed
reviews
1 parent 9334bde commit 96d49b8

File tree

6 files changed

+63
-38
lines changed

6 files changed

+63
-38
lines changed

rlp/rlpgen/gen.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -373,29 +373,6 @@ func (op uint256Op) genDecode(ctx *genContext) (string, string) {
373373
return result, b.String()
374374
}
375375

376-
func (bctx *buildContext) makeNamedBasicOp(named *types.Named) (op, error) {
377-
underlying := named.Underlying()
378-
basic, ok := underlying.(*types.Basic)
379-
if !ok {
380-
return nil, fmt.Errorf("expected basic type, got %T", underlying)
381-
}
382-
383-
// Use the existing makeBasicOp function to get the base operation
384-
baseOp, err := bctx.makeBasicOp(basic)
385-
if err != nil {
386-
return nil, err
387-
}
388-
389-
// Cast to basicOp and modify the typ field to use the named type
390-
op, ok := baseOp.(basicOp)
391-
if !ok {
392-
return nil, fmt.Errorf("expected basicOp, got %T", baseOp)
393-
}
394-
op.typ = named // Use the named type as the main type instead of the underlying basic type
395-
396-
return op, nil
397-
}
398-
399376
// encoderDecoderOp handles rlp.Encoder and rlp.Decoder.
400377
// In order to be used with this, the type must implement both interfaces.
401378
// This restriction may be lifted in the future by creating separate ops for
@@ -707,12 +684,15 @@ func (bctx *buildContext) makeOp(name *types.Named, typ types.Type, tags rlpstru
707684
if typ == bctx.rawValueType {
708685
return bctx.makeRawValueOp(), nil
709686
}
710-
if isNamedWithBasicUnderlying(typ) {
711-
return bctx.makeNamedBasicOp(typ)
712-
}
713687
if bctx.isDecoder(typ) {
714688
return nil, fmt.Errorf("type %v implements rlp.Decoder with non-pointer receiver", typ)
715689
}
690+
// libevm: named types are reduced to their underlying basic type in this loop.
691+
// We're handling named types here by passing the named type as the main type.
692+
// See [named.libevm.go] for more details.
693+
if hasBasicUnderlying(typ) {
694+
return bctx.makeNamedBasicOp(typ)
695+
}
716696
// TODO: same check for encoder?
717697
return bctx.makeOp(typ, typ.Underlying(), tags)
718698
case *types.Pointer:

rlp/rlpgen/gen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func init() {
4747
}
4848
}
4949

50-
var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256", "alias", "named"}
50+
var tests = []string{"uints", "nil", "rawvalue", "optional", "bigint", "uint256", "alias", "named.libevm"}
5151

5252
func TestOutput(t *testing.T) {
5353
for _, test := range tests {
@@ -66,7 +66,7 @@ func TestOutput(t *testing.T) {
6666

6767
// Set this environment variable to regenerate the test outputs.
6868
if os.Getenv("WRITE_TEST_FILES") != "" {
69-
os.WriteFile(outputFile, output, 0o644)
69+
os.WriteFile(outputFile, output, 0644)
7070
}
7171

7272
// Check if output matches.

rlp/rlpgen/named.libevm.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"fmt"
21+
"go/types"
22+
)
23+
24+
// makeNamedBasicOp is a convenience wrapper for basicOp.
25+
// It returns a basicOp with the named type as the main type instead of the underlying basic type.
26+
func (bctx *buildContext) makeNamedBasicOp(named *types.Named) (op, error) {
27+
underlying := named.Underlying()
28+
basic, ok := underlying.(*types.Basic)
29+
if !ok {
30+
return nil, fmt.Errorf("expected basic type, got %T", underlying)
31+
}
32+
33+
// We use basic op because it actually supports necessary conversions (through writeNeedsConversion and decodeNeedsConversion)
34+
// for named types.
35+
// The only problem with that is it does not support the named type as the main type.
36+
// So we use the named type as the main type instead of the underlying basic type.
37+
baseOp, err := bctx.makeBasicOp(basic)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
op, ok := baseOp.(basicOp)
43+
if !ok {
44+
return nil, fmt.Errorf("expected basicOp, got %T", baseOp)
45+
}
46+
op.typ = named
47+
48+
return op, nil
49+
}
50+
51+
// hasBasicUnderlying checks whether `named` has an underlying basic type.
52+
func hasBasicUnderlying(named *types.Named) bool {
53+
_, ok := named.Underlying().(*types.Basic)
54+
return ok
55+
}
File renamed without changes.
File renamed without changes.

rlp/rlpgen/types.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ func isByte(typ types.Type) bool {
113113
return ok && basic.Kind() == types.Uint8
114114
}
115115

116-
// isNamedWithBasicUnderlying checks whether 'typ' is a named type with an underlying basic type.
117-
func isNamedWithBasicUnderlying(typ types.Type) bool {
118-
named, ok := typ.(*types.Named)
119-
if !ok {
120-
return false
121-
}
122-
_, ok = named.Underlying().(*types.Basic)
123-
return ok
124-
}
125-
126116
func resolveUnderlying(typ types.Type) types.Type {
127117
for {
128118
t := typ.Underlying()

0 commit comments

Comments
 (0)