Skip to content

x/tools/internal/refactor/inline: allow conversions to be considered duplicable #67589

@vladvalkov

Description

@vladvalkov

A subtask of #63352.

At the moment, inliner does not consider type conversions as duplicable operations.
For example, inlining a function f

func f(a int) {
    print(a, a)
}

func main() {
    var a int8 = 1
    f(int(a))
}

generates this code:

func main() {
    var a int8 = 1
    var s int = int(a)
    print(s, s)
}

Instead, inliner should consider some conversions (those that do not change semantics or involve additional allocations) to be duplicable:

func main() {
    var a int8 = 1
    print(int(s), int(s))
}

One way to implement this is by changing duplicable function to cover the type conversion cases, which are parsed as *ast.CallExpr with the Fun field being one of *ast.Ident, *ast.ArrayType, *ast.StructType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, or *ast.ChanType.

As per spec, string([]byte) and []byte(string) conversions may involve additional allocations and should not be considered duplicable:

Specific rules apply to (non-constant) conversions between numeric types or to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions