Skip to content

Commit a6b091a

Browse files
feat(gnovm): simplify redundant conversions (#4131)
Closes [4119](#4119)
1 parent 099d586 commit a6b091a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Diff for: gnovm/pkg/gnolang/gno_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,27 @@ func main() {
466466
}
467467
}
468468

469+
func TestOptimizeConversion(t *testing.T) {
470+
t.Parallel()
471+
472+
m := NewMachine("test", nil)
473+
c := `package test
474+
func main() {}
475+
476+
func foo(a int) {
477+
b := int(a)
478+
println(b)
479+
}`
480+
n := MustParseFile("main.go", c)
481+
m.RunFiles(n)
482+
fn := n.Decls[1].(*FuncDecl)
483+
as := fn.Body[0].(*AssignStmt)
484+
ne := as.Rhs[0].(*NameExpr)
485+
if ne.Name != "a" {
486+
t.Fatalf("expecting optimized 'a', got %v", ne.String())
487+
}
488+
}
489+
469490
func BenchmarkIfStatement(b *testing.B) {
470491
m := NewMachine("test", nil)
471492
c := `package test

Diff for: gnovm/pkg/gnolang/preprocess.go

+6
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,12 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
12061206
ct := evalStaticType(store, last, n.Func)
12071207
at := evalStaticTypeOf(store, last, n.Args[0])
12081208

1209+
// OPTIMIZATION: Skip redundant type conversions when source and target types are identical
1210+
if at != nil && ct.TypeID() == at.TypeID() && !isUntyped(at) {
1211+
n.SetAttribute(ATTR_TYPEOF_VALUE, ct)
1212+
return n.Args[0], TRANS_CONTINUE
1213+
}
1214+
12091215
if _, isIface := baseOf(ct).(*InterfaceType); isIface {
12101216
assertAssignableTo(n, at, ct, false)
12111217
}

0 commit comments

Comments
 (0)