code: func reorderBodyForClosures(arity func(Ref) int, globals VarSet, body Body) (Body, unsafeVars) {
  
  	reordered := Body{}
  	unsafe := unsafeVars{}
  
  	for {
  		n := len(reordered)
  
  		for _, e := range body {
  			if reordered.Contains(e) {
  				continue
  			}
  
  			// Collect vars that are contained in closures within this
  			// expression.
  			vs := VarSet{}
  			WalkClosures(e, func(x interface{}) bool {
  				vis := &VarVisitor{vars: vs}
  				Walk(vis, x)
  				return true
  			})
  
  			// Compute vars that are closed over from the body but not yet
  			// contained in the output position of an expression in the reordered
  			// body. These vars are considered unsafe.
  			cv := vs.Intersect(body.Vars(safetyCheckVarVisitorParams)).Diff(globals)
  			uv := cv.Diff(outputVarsForBody(reordered, arity, globals))
  
  			if len(uv) == 0 {
  				reordered = append(reordered, e)
  				delete(unsafe, e)
  			} else {
  				unsafe.Set(e, uv)
  			}
  		}
  
  		if len(reordered) == n {
  			break
  		}
  	}
  
  	return reordered, unsafe
  }
docs: // reorderBodyForClosures returns a copy of the body ordered such that
  // expressions (such as array comprehensions) that close over variables are ordered
  // after other expressions that contain the same variable in an output position.