| 1 | // Copyright 2025 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package typesinternal |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/token" |
| 10 | "go/types" |
| 11 | ) |
| 12 | |
| 13 | // NoEffects reports whether the expression has no side effects, i.e., it |
| 14 | // does not modify the memory state. This function is conservative: it may |
| 15 | // return false even when the expression has no effect. |
| 16 | func NoEffects(info *types.Info, expr ast.Expr) bool { |
| 17 | noEffects := true |
| 18 | ast.Inspect(expr, func(n ast.Node) bool { |
| 19 | switch v := n.(type) { |
| 20 | case nil, *ast.Ident, *ast.BasicLit, *ast.BinaryExpr, *ast.ParenExpr, |
| 21 | *ast.SelectorExpr, *ast.IndexExpr, *ast.SliceExpr, *ast.TypeAssertExpr, |
| 22 | *ast.StarExpr, *ast.CompositeLit, |
| 23 | // non-expressions that may appear within expressions |
| 24 | *ast.KeyValueExpr, |
| 25 | *ast.FieldList, |
| 26 | *ast.Field, |
| 27 | *ast.Ellipsis, |
| 28 | *ast.IndexListExpr: |
| 29 | // No effect. |
| 30 | |
| 31 | case *ast.ArrayType, |
| 32 | *ast.StructType, |
| 33 | *ast.ChanType, |
| 34 | *ast.FuncType, |
| 35 | *ast.MapType, |
| 36 | *ast.InterfaceType: |
| 37 | // Type syntax: no effects, recursively. |
| 38 | // Prune descent. |
| 39 | return false |
| 40 | |
| 41 | case *ast.UnaryExpr: |
| 42 | // Channel send <-ch has effects. |
| 43 | if v.Op == token.ARROW { |
| 44 | noEffects = false |
| 45 | } |
| 46 | |
| 47 | case *ast.CallExpr: |
| 48 | // Type conversion has no effects. |
| 49 | if !info.Types[v.Fun].IsType() { |
| 50 | if CallsPureBuiltin(info, v) { |
| 51 | // A call such as len(e) has no effects of its |
| 52 | // own, though the subexpression e might. |
| 53 | } else { |
| 54 | noEffects = false |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | case *ast.FuncLit: |
| 59 | // A FuncLit has no effects, but do not descend into it. |
| 60 | return false |
| 61 | |
| 62 | default: |
| 63 | // All other expressions have effects |
| 64 | noEffects = false |
| 65 | } |
| 66 | |
| 67 | return noEffects |
| 68 | }) |
| 69 | return noEffects |
| 70 | } |
| 71 | |
| 72 | // CallsPureBuiltin reports whether call is a call of a built-in |
| 73 | // function that is a pure computation over its operands (analogous to |
| 74 | // a + operator). Because it does not depend on program state, it may |
| 75 | // be evaluated at any point--though not necessarily at multiple |
| 76 | // points (consider new, make). |
| 77 | func CallsPureBuiltin(info *types.Info, call *ast.CallExpr) bool { |
| 78 | if id, ok := ast.Unparen(call.Fun).(*ast.Ident); ok { |
| 79 | if b, ok := info.ObjectOf(id).(*types.Builtin); ok { |
| 80 | switch b.Name() { |
| 81 | case "len", "cap", "complex", "imag", "real", "make", "new", "max", "min": |
| 82 | return true |
| 83 | } |
| 84 | // Not: append clear close copy delete panic print println recover |
| 85 | } |
| 86 | } |
| 87 | return false |
| 88 | } |