Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/fix: automate migrations for simple deprecations #32816

Open
bcmills opened this issue Jun 27, 2019 · 58 comments
Open

proposal: cmd/fix: automate migrations for simple deprecations #32816

bcmills opened this issue Jun 27, 2019 · 58 comments
Labels
FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Proposal Proposal-Hold
Milestone

Comments

@bcmills
Copy link
Contributor

bcmills commented Jun 27, 2019

(I've mentioned this in passing on #32014 and #27248, but I don't see an actual proposal filed yet — so here it is!)

Over time, the author of a package may notice certain patterns in the usage of their API that they want to make simpler or clearer for users. When that happens, they may suggest that users update their own code to use the new API.

In many cases, the process of updating is nearly trivial, so it would be nice if some tool in the standard distribution could apply those trivial updates mechanically. The go fix subcommand was used for similar updates to the language itself, so perhaps it could be adapted to work for at least the simple cases.

The very simplest sort of update rewrites a call to some function or use of some variable or constant to instead call some other function or use some other variable, constant, or expression.

To draw some examples from the standard library:

  • The comment for archive/tar.TypeRegA says to use TypeReg instead.
  • The comments for archive/zip.FileHeader.{Compressed,Uncompressed}Size say to use the corresponding *64 fields instead.
  • The comments for image.ZR and image.ZP say to use zero struct literals instead.
  • The comment for go/importer.For says to use ForCompiler, and is implemented as a trivial call to ForCompiler with an additional argument.
  • The comment for the io.SEEK_* constants says to use corresponding io.Seek* constants instead.
  • The comments for syscall.StringByte{Slice,Ptr} say to use the corresponding Byte*FromString functions, and are implemented as short wrappers around those functions.

Outside the standard library, this pattern can also occur when a package makes a breaking change: if the v3 API can express all of the same capabilities as v2, then v2 can be implemented as a wrapper around v3 — and references to the v2 identifiers can be replaced with direct references to the underlying v3 identifiers.


See the current proposal details in #32816 (comment) below.

Previous draft (prior to #32816 (comment)):

I propose that we establish a consistent convention to mark these kinds of mechanical replacements, and improve cmd/fix to be able to apply them.

I'm not particularly tied to any given convention, but in the spirit of having something concrete to discuss, I propose two tokens, //go:fix-to and //go:forward, as follows:

  • //go:fix-to EXPR on a constant or variable indicates that go fix should replace occurrences of that constant or variable with the expression EXPR. Free variables in the expression are interpreted as package names, and go fix may rewrite them to avoid collisions with non-packages.
  • //go:fix-to .EXPR on struct field indicates that go fix should replace references to that field with the given selector expression (which may be a field access OR a method call) on the same value. Free variables are again interpreted as package names. If the expression is a method call, it replaces only reads of the field.
  • //go:forward on an individual constant or variable indicates that go fix should replace the constant or variable with the expression from which it is initialized. If a constant is declared with a type but initialized from an untyped expression, the replacement is wrapped in a conversion to that type.
  • //go:forward on a const or var block indicates that every identifier within the block should be replaced by the expression from which it is initialized.
  • //go:forward on a method or function indicates that go fix should inline its body at the call site, renaming any local variables and labels as needed to avoid collisions. The function or method must have at most one return statement and no defer statements.
  • //go:forward on a type alias indicates that go fix should replace references to the alias name with the (immediate) type it aliases.

All of these rewrites must be acyclic: the result of a (transitively applied) rewrite must not refer to the thing being rewritten.

Note that there is a prototype example-based refactoring tool for Go in golang.org/x/tools/cmd/eg that supported a broader set of refactoring rules. It might be useful as a starting point.


For the above examples, the resulting declarations might look like:

package image

// Deprecated: Use a literal image.Point{} instead.
var ZP = Point{} //go:forward

// Deprecated: Use a literal image.Rectangle{} instead.
var ZR = Rectangle{} //go:forward
package importer

// Deprecated: Use ForCompiler, which populates a FileSet
// with the positions of objects created by the importer.
//
//go:forward
func For(compiler string, lookup Lookup) types.Importer {
	return ForCompiler(token.NewFileSet(), compiler, lookup)
}
package io

// Deprecated: Use io.SeekStart, io.SeekCurrent, and io.SeekEnd.
const (
	SEEK_SET int = 0 //go:fix-to io.SeekStart
	SEEK_CUR int = 1 //go:fix-to io.SeekCur
	SEEK_END int = 2 //go:fix-to io.SeekEnd
)
package syscall

// Deprecated: Use ByteSliceFromString instead.
//
//go:forward
func StringByteSlice(s string) []byte {
	a, err := ByteSliceFromString(s)
	if err != nil {
		panic("string with NUL passed to syscall.ByteSliceFromString")
	}
	return a
}

[…]

This proposal does not address the archive/tar use-case: callers producing archives might be able to transparently migrate from TypeRegA to TypeReg, but it isn't obvious that callers consuming archives can safely do so.

Nor does this proposal does not address the archive/zip.FileHeader use-case: the deprecation there indicates a loss of precision, and the caller should probably be prompted to make related fixes in the surrounding code.

Both of those cases might be addressable with a deeper example-based refactoring tool, but (I claim) are too subtle to handle directly in go fix.


CC @marwan-at-work @thepudds @jayconrod @ianthehat @myitcv @cep21

@bcmills bcmills added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. FeatureRequest labels Jun 27, 2019
@bcmills bcmills added this to the Proposal milestone Jun 27, 2019
@mvdan
Copy link
Member

mvdan commented Jun 27, 2019

How would this deal with Go versions? For example, importer.ForCompiler was added in 1.12, so go fix shouldn't make the change if a module still supports Go 1.11.

Perhaps it can grab the oldest supported Go version from the optional line in go.mod, defaulting to a version extremely conservative (such as 1.0) if it's missing.

@bcmills
Copy link
Contributor Author

bcmills commented Jun 27, 2019

@mvdan, good question. I'd say that it should not attempt to do anything special with Go versions. go fix is an explicit operation, and folks should look at the diffs (and run tests) after applying it to ensure that they're happy with the results.

For stuff in the standard library in particular, perhaps that implies that we shouldn't add //go:fix or //go:forward comments until two releases after the target of the fix, since that's our recommended support window.

@mvdan
Copy link
Member

mvdan commented Jun 27, 2019

until two releases after the target of the fix, since that's our recommended support window.

I personally think this is a losing battle. Certain libraries support older Go versions, for their own reasons. You can say "just don't use go fix", but that seems unnecessary if go.mod can give us that information.

An alternative is to use "two Go releases ago" as the fallback version, instead of 1.0. Then a module can specify an older or newer version as needed.

@thepudds
Copy link
Contributor

thepudds commented Jun 27, 2019

The "Automatic API Updates" section of the initial vgo blog series sketched out an alternative. It might be interesting to contrast this proposal with that earlier sketch.

One difference between that earlier sketch and the proposal here is that the earlier sketch I think had fewer comment variations, but I think relied on the v1 code being implemented as a shim around v2 (and perhaps it could in theory also support the reverse if v2 is implemented as a wrapper around v1).

Example snippet from that older sketch:

For example, suppose v1 of an API has functions EnableFoo and DisableFoo and v2 replaces 
the pair with a single SetFoo(enabled bool). After v2 is released, v1 can be implemented 
as a wrapper around v2:

package p // v1

import v2 "p/v2"

func EnableFoo() {
	//go:fix
	v2.SetFoo(true)
}

func DisableFoo() {
	//go:fix
	v2.SetFoo(false)
}

The fact that actual Go code is used and the earlier sketch might "use symbolic execution" might mean the earlier sketch is more expressive than this newer proposal. (edit: not sure if this is true; need to digest the new proposal more).

In contrast, this newer proposal also addresses deprecation use cases (without a need for a major version change).

@bcmills
Copy link
Contributor Author

bcmills commented Jun 27, 2019

@mvdan, I suppose we could use the annotations in api/go*.txt to figure out when various identifiers were added, but note that the go directive is more of a maximum than a minimum. (For example, you can write a go 1.13 module to get access to the features added in 1.13, but use build tags to notch out those source files when building with 1.12 and earlier.)

@bcmills
Copy link
Contributor Author

bcmills commented Jun 27, 2019

@thepudds, note that @rsc's sketch there is substantially more powerful than what I'm proposing here.

In particular, I'm not assuming that go fix can do any sort of control-flow analysis.

One of the examples there is:

func SetFoo(enabled bool) {
	if enabled {
		//go:fix
		v2.EnableFoo()
	} else {
		//go:fix
		v2.DisableFoo()
	}
}

but under this proposal that would be expressed as:

//go:forward
func SetFoo(enabled bool) {
	if enabled {
		v2.EnableFoo()
	} else {
		v2.DisableFoo()
	}
}

Of course, once go fix does the inlining, you can always apply other (more powerful!) analyzers to prune out the inlined paths that are provably unreachable.

@mvdan
Copy link
Member

mvdan commented Jun 27, 2019

note that the go directive is more of a maximum than a minimum

Ah, noted.

@cep21
Copy link

cep21 commented Jun 28, 2019

A few questions:

  • Does go:forward imply deprecated? Is the intent to place both comments in my code?
  • Can you give an example of go:fix-to .EXPR
  • The go:forward inline case seems the most magical of the bunch. Probably worth extra consideration.

Trying to get a scope of what you're proposing.

  • Fixing net.Dialer.Cancel is way out of scope (?)
  • Fixing net.Dialer.DualStack ... does that work somehow with .Expr?
  • Can net.Dialer.Dial be replaced with net.Dialer.DialContext(context.TODO(), ...) ?
  • There's probably a whole class of current and future context API migrations

@bcmills
Copy link
Contributor Author

bcmills commented Jun 28, 2019

Does go:forward imply deprecated? Is the intent to place both comments in my code?

I would generally expect so, yes: go:forward implies that there is a simpler, more direct, or more robust alternative.

However, I think we still want both comments in general. //go:forward tells go fix that the fix should be automated, and // Deprecated: tells humans why the alternative is preferred.

Can you give an example of go:fix-to .EXPR

Sure: you could envision using it for (*net.URL).RawPath, since “In general, code should call EscapedPath instead of reading u.RawPath directly.”

package url

type URL struct {
	[…]
	// encoded path hint
	RawPath string //go:fix-to .EscapedPath()
	[…]
}

The go:forward inline case seems the most magical of the bunch. Probably worth extra consideration.

Agreed. It's the most magical, but also arguably the most useful. 🙂

I think it will require particular care around how we map := and return, since we have to make the return statement mesh with the caller code. Perhaps as a first past we should restrict it to functions whose single return statement is at the end of the function.

Fixing net.Dialer.Cancel is way out of scope (?)

Correct. I could envision some ways to use example-based refactoring to convert uses of the Cancel field to use DialContext, but they would require a much more sophisticated algorithm to match the call site against the refactoring pattern.

Fixing net.Dialer.DualStack ... does that work somehow with .Expr?

Sadly, no: .EXPR works for reads, but the thing we want to match there is the write operation.

I could imagine a //go:fix-write-to extension of this proposal that would handle it, though. Let _ stand for the field being fixed, and let //go:fix-write-to .EXPR be the rewrite rule for writes to a field. Then we can combine a //go:fix-write-to with a //go:forward to produce the intended refactoring:

package net

type Dialer {
	[…]
	DualStack bool //go:fix-write-to .setDualStack(_)
	[…]
}

//go:forward
func (d *Dialer) setDualStack(dualStack bool) {
	if !dualStack {
		d.FallbackDelay = -1
	}
}

Can net.Dialer.Dial be replaced with net.Dialer.DialContext(context.TODO(), ...) ?

Yes:

package net

//go:forward
func (d *Dialer) Dial(network, address string) (Conn, error) {
	return d.DialContext(context.TODO(), network, address)
}

There's probably a whole class of current and future context API migrations

Yep. Note that it's easy to introduce context.TODO() using this proposal, but converting those context.TODO() calls to use the actual context in the current scope is beyond its capability.

@cep21
Copy link

cep21 commented Jun 28, 2019

Sure: you could envision using it for (*net.URL).RawPath, since “In general, code should call EscapedPath instead of reading u.RawPath directly.”

Is go-fix intended for changes that aren't strictly backwards compatible?

@bcmills
Copy link
Contributor Author

bcmills commented Jun 28, 2019

I think that's an open question. This proposal leaves that decision up to package authors: nothing stops them from applying a //go:fix-to or //go:forward that isn't strictly backward compatible, although I would expect its use for incompatible changes to be rare in practice.

@bcmills
Copy link
Contributor Author

bcmills commented Jun 28, 2019

(And do note that a //go:forward inlining a function call, constant, or type alias is very unlikely to result in an incompatible change.)

@cep21
Copy link

cep21 commented Jun 28, 2019

  • Should there be two classes of go-fix runs? The 100% backwards compatible ones and the "probably" compatible ones?
  • Will there be any warning when fixes do things like point to a constant with a changed value?
  • If I had reassigned image.ZR, does this show up when I eventually build my code or during the fix?

@bcmills
Copy link
Contributor Author

bcmills commented Jul 2, 2019

Should there be two classes of go-fix runs? The 100% backwards compatible ones and the "probably" compatible ones?

Maybe, but since go fix is an explicit action I'm not sure it's necessary to distinguish them. (Both should fall under the category of “changes that the package author believes to be appropriate 100% of the time”.)

Note that the proposed //go:forward inlining should ~always be backward-compatible (it works unless either the caller has a very subtle dependency on the runtime.Callers depth, or some package modifies a package-level variable that is meant not to be modified), whereas //go:fix-to may or may not be (depending on the chosen expression).

But that's an interesting idea, and I would not object to adding a variant of fix-to for the “maybe incompatible” case — perhaps //go:suggest?

Will there be any warning when fixes do things like point to a constant with a changed value?

That seems like a reasonable thing for apidiff and go release (#26420) to warn about.

@bcmills
Copy link
Contributor Author

bcmills commented Jul 2, 2019

If I had reassigned image.ZR, does this show up when I eventually build my code or during the fix?

In general I'm working on the assumption that folks don't go around monkey-patching variables in other packages. (For example, you should never swap out io.EOF either!)

I don't have a solution to that in general, but there are complementary proposals (such as #21130) that could help to detect those sorts of errors.

Note that you can also use the race detector to detect these sorts of modifications:

var ZR = Rectangle{}
func init() {
	go func() { _ = ZR }()  // Data race if anybody mutates ZR.
}()

@rsc
Copy link
Contributor

rsc commented Jul 2, 2019

I would like to see this simpified down to a single comment that means "this is 100% guaranteed semantically OK, because it is literally a definitional substitution", and because the tool is go fix the comment should also be //go:fix (not //go:forward).

For the io constants, os can do

//go:fix

const SEEK_SET = int(io.SeekSet)

Also the //go:fix comments should not be in the doc comment; they should be above them instead.

You want to be able to tell people "it is always OK to run go fix". A suggestion mechanism is fine as part of Deprecated comments but we should consider that in a separate proposal and separate tool.

@cep21
Copy link

cep21 commented Jul 16, 2019

Is there any worry about name squatting on //go:fix? Could there be many ways to fix things? Are there use cases where go:fix does not match with deprecated? An alternative may be to stick with deprecated formatting as the tag and use another tool that understands 1:1 mapping of deprecated APIs. For example, if a deprecated API is a type alias, it would replace the type.

@bcmills
Copy link
Contributor Author

bcmills commented Jul 16, 2019

Is there any worry about name squatting on //go:fix?

@cep21, did you have anything else in mind for it?
(After all, there are infinitely many other words we can suffix onto //go: if we discover other use-cases.)

Could there be many ways to fix things?

Yes. //go:fix specifically should be read as “fix it to this”.

Are there use cases where go:fix does not match with deprecated? An alternative may be to stick with deprecated formatting as the tag and use another tool that understands 1:1 mapping of deprecated APIs.

The intent of this proposal is specifically to mark that the mapping is 1:1, and furthermore that the mapping is straightforward enough that a human doesn't need to think about how to apply it.

Without such an annotation, I don't think it's feasible to build a tool that can decide when a deprecated comment should result in automatic code transformations.

@cep21
Copy link

cep21 commented Jul 16, 2019

Without such an annotation, I don't think it's feasible to build a tool that can decide when a deprecated comment should result in automatic code transformations.

Can you give an example that fits with rcs's simplified proposal? He mentions

"this is 100% guaranteed semantically OK, because it is literally a definitional substitution"

In those cases, I can't see how it wouldn't be expected and safe for a // deprecated flag on a type alias or a variable assignment, like const SEEK_SET = int(io.SeekSet), wouldn't contain the same information.

@cep21
Copy link

cep21 commented Jul 16, 2019

More concretely, if the proposal is limited to just type aliases (as an example), in which situation would I not want to fix this?

// deprecated: blah blah
type Circuit = newpackage.Circuit

@cep21
Copy link

cep21 commented Jul 17, 2019

Are there any surprising consequences of the following proposal:

type T1 = T2:
    go fix will replace all instances of T1 with T2 if T1 is marked deprecated.

const X = Y
    go fix will replace all instances of X with Y, if:
      * X is marked deprecated
      * Y is not marked deprecated
      * Y is not a literal (is literal the right word?  I don't want to replace things like const X = 3)

@bcmills
Copy link
Contributor Author

bcmills commented Jul 18, 2019

@cep21, the interesting use-cases for go fix are for function and method calls. Restricting the proposal to only constants and type-aliases doesn't help for things like “automatically migrating users of a v1 module to v2”.

@bcmills
Copy link
Contributor Author

bcmills commented Jul 18, 2019

I don't want to replace things like const X = 3

Note that one of the examples — image.ZRexplicitly does recommend that users switch from an identifier to a literal expression.

@cep21
Copy link

cep21 commented Jul 23, 2019

Note that one of the examples — image.ZR — explicitly does recommend that users switch from an identifier to a literal expression.

That's not allowed by rcs's simplification since it's not guaranteed to be backwards compatible: It's possible to reassign var types. I may be misunderstanding what "definitional substitution" and "suggestion mechanism" means. Can those terms be defined? Either way, it's not a big concern and happy this issue is getting noticed!

@bcmills
Copy link
Contributor Author

bcmills commented Jul 29, 2019

@cep21, I've been thinking some more about the interaction with deprecation. From discussions with @ianthehat, it seems that he would prefer something similar.

At the very least, I think a heuristic based solely on Deprecated comments would need to be transitive. Consider

// Deprecated: use T2.
type T1 = T2

// Deprecated: use T3
type T2 = T3

The arguably “correct” behavior is to inline uses of T1 all the way down to T3, and it would not be appropriate to stop that process just because T2 is itself deprecated.

@bcmills
Copy link
Contributor Author

bcmills commented Jul 29, 2019

The interaction with literal values is interesting, though. In the declaration

// Deprecated: Use a literal image.Rectangle{} instead.
var ZR = Rectangle{}

it would certainly appropriate to replace the value with a literal.

However, sometimes a value with an existing Deprecated: comment is initialized from something other than its intended substitute expression:

// Deprecated: Use io.SeekStart, io.SeekCurrent, and io.SeekEnd.
const (
	SEEK_SET int = 0 // seek relative to the origin of the file
	SEEK_CUR int = 1 // seek relative to the current offset
	SEEK_END int = 2 // seek relative to the end
)

Moreover, the problem is even worse if the initializer is not idempotent:

// Deprecated: use os.ErrTemporary directly.
var ErrFlaky = fmt.Errorf("%w: operation flaky", os.ErrTemporary)
// Deprecated: use a caller-allocated slice.
var Buffer = make([]byte, 1<<10)

The non-idempotent case, at least, seems easier to detect, although I'm not sure whether it's easy enough to be effective in general.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/523995 mentions this issue: internal/refactor/inline: an inliner for Go source

gopherbot pushed a commit to golang/tools that referenced this issue Aug 29, 2023
This change creates at new (internal) package that
implements an inlining algorithm for Go functions,
and an analyzer in the go/analysis framework that
uses it to perform automatic inlining of calls to
specially annotated ("inlineme") functions.

Run this command to invoke the analyzer and
apply any suggested fixes to the source tree:

  $ go run ./internal/refactor/inline/analyzer/main.go -fix packages...

The package is intended for use both in interactive tools
such as gopls and batch tools such as the analyzer
just mentioned and the tool proposed in the attached issue.

As noted in the code comments, correct inlining is a
surprisingly tricky problem, so for now we primarily
address the most general case in which a call f(args...)
has the function name f replaced by a literal copy of the
called function (func (...) {...})(args...).

Only in the simplest of special cases is the call
itself eliminated by replacing it with the function body.
There is much further work to do by careful analysis of cases.

The processing of the callee function occurs first,
and results in a serializable summary of the callee
that can be used for a later call to Inline, possibly
from a different process, thus enabling "separate analysis"
pipelines using the analysis.Fact mechanism.

Recommended reviewing order:

- callee.go, inline.go
- inline_test.go, testdata/*txtar
- analyzer/...

Updates golang/go#32816

Change-Id: If28e43a6ba9ab92639276c5b50b5a89a3b0c54c4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/519715
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Alan Donovan <adonovan@google.com>
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
@adonovan
Copy link
Member

adonovan commented Oct 5, 2023

The function inliner, which was the major technical obstacle to building a prototype of the proposed tool, is now ready for serious use.

"Inlining" references to annotated constants and vars could be squeezed into the existing logic by modeling them as calls to a zero-argument function:

const two = 1 + 1 // => func two() untyped int { return 1 + 1 }

@suzmue

@rsc
Copy link
Contributor

rsc commented Dec 4, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc rsc moved this from Incoming to Active in Proposals Dec 4, 2023
@rsc
Copy link
Contributor

rsc commented Dec 4, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Dec 9, 2023

The basic idea here is to allow a //go:fix annotation on a definition or perhaps at some point also inside a function (for selective inlining) to mean that go fix should inline the definition at the use site.

@bcmills wrote a more detailed description at #32816 (comment). I expect there will be minor adjustments as we implement it, and that's fine for the purpose of this proposal.

Are there any concerns or objections with the idea, or any aspects we may be overlooking that need discussion?

@adonovan
Copy link
Member

adonovan commented Dec 9, 2023

Are there any concerns or objections with the idea, or any aspects we may be overlooking that need discussion?

I have two questions, one about the implementation and one about annotation syntax.

The desired behavior could be implemented entirely within the go/analysis framework: for each function with a //go:fix annotation, the analyzer would export a fact consisting of the inliner's summary of the callee. For every call, the analyzer would report an "inlinable call" diagnostic with an associated SuggestedFix that does the inlining. Running a singlechecker for this analyzer with -fix would apply all the edits to a set of packages. (We have an implementation already.)

One advantage of this approach is that we can drop the analyzer into other analysis drivers such as gopls (for real-time feedback) and nogo (for Bazel, and Google's Go code base), and it will just work.

Q1. What are the pros and cons of adding this functionality within cmd/fix? Any good reasons not to develop only the analyzer approach?

The second question is the particular spelling of the annotation: this issue proposes //go:fix comments; the prototype implementation above uses // inlineme as a placeholder, but raises the question of some other syntax, perhaps based on a // Deprecated comment.

Another possibility is to use whatever general analysis annotation syntax we end up agreeing upon (if any), though we don't even have a proposal for that yet, so it will be a while before any decision is made. I imagine it might look something like a no-op call inlineannot.InlineMe() inside the function body, or a call inlineannot.Inline(Foo) or inlineannot.Inline((*T).Foo)` external to the function or method of interest. But perhaps I'm getting ahead of myself.

Q2. How should we spell it?

@znkr
Copy link
Contributor

znkr commented Dec 10, 2023

+1 to using the analyzer framework for this job. We use that framework internally at Google for large scale changes and it's been a very pleasant experience.

Using analyzers for the fix command has the other advantage that you can extend it quite easily. To illustrate a potential future scope increase for the go fix command: One deprecation I ran across a number of times is adding a function variant that takes a context:

// Deprecated: Use MyFunctionContext instead
func MyFunction(...) error {
  return MyFunctionContext(context.TODO(), ...)
}

func MyFunctionContext(ctx context.Context, ...) error { ... }

Inlining MyFunction would result in a proliferation of context.TODO(). Wouldn't it be nice if that TODO could be resolved immediately when there's a context available?

Another comment I have is about //go:fix. I think it's too implicit. I would prefer a more explicit annotation, that is understandable without looking up how the annotation works.

@rsc
Copy link
Contributor

rsc commented Dec 13, 2023

Perhaps //go:fix inline?
In the long term we might also want it to apply to non-funcs, like type aliases or constants.
I think it is fine to use go/analysis the same way go vet does.

@bcmills
Copy link
Contributor Author

bcmills commented Dec 13, 2023

The second question is the particular spelling of the annotation: this issue proposes //go:fix comments; the prototype implementation above uses // inlineme as a placeholder, but raises the question of some other syntax, perhaps based on a // Deprecated comment.

I'm fine with other syntactic variations, but heuristics based on // Deprecated comments seem like a non-starter: I expect that not all deprecated functions will be suitable for inlining, even if they happen to have very short implementations in terms of some potential replacement.

(For example, it may be that the deprecation is due to a common class of bug, and migrating from the function to the thing it wraps would perpetuate that bug instead of fixing it as intended.)

@ChrisHines
Copy link
Contributor

but heuristics based on // Deprecated comments seem like a non-starter

Agreed. We (in a closed source setting) commonly mark things Deprecated to encourage switching to a completely new implementation with a different API and make no attempt to have the old code call the new code.

@adonovan
Copy link
Member

heuristics based on // Deprecated comments seem like a non-starter: I expect that not all deprecated functions will be suitable for inlining

Oh, for sure, but I was thinking of some syntax in addition to Deprecated like // Deprecated: call function [New] instead. [inline].

@znkr
Copy link
Contributor

znkr commented Dec 14, 2023

I was thinking of some syntax in addition to Deprecated like // Deprecated: call function [New] instead. [inline].

What I like about this idea, is that it uses the same mechanism we use for deprecation and that it's visible in godoc: It means we only need to look at one place to understand the deprecation story. Deprecation is also a good backstop to prevent accidental use. Furthermore, we always have a deprecation message that we can use for autogenerated commit messages.

@rsc
Copy link
Contributor

rsc commented Dec 20, 2023

Thanks everyone. The direction seems clear enough that we can put this on hold for an implementation.

@rsc rsc moved this from Active to Hold in Proposals Dec 21, 2023
@rsc
Copy link
Contributor

rsc commented Dec 21, 2023

Placed on hold.
— rsc for the proposal review group

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/634919 mentions this issue: internal/refactor/inline/analyzer: use "//go:fix inline" annotation

gopherbot pushed a commit to golang/tools that referenced this issue Dec 11, 2024
This CL changes the proof-of-concept "inliner" analyzer to
use "//go:fix inline" as the marker of a function that should
be inlined. This seems to be the syntax emerging from the
proposal golang/go#32816.

Also, skip inlinings that cause literalization of the callee.
Users of batch tools only want to see reductions.

Updates golang/go#32816
Updates golang/go#70717

Change-Id: I6d70118f69b7c35f51e1d51863a0312b5dcc3b63
Reviewed-on: https://go-review.googlesource.com/c/tools/+/634919
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/635680 mentions this issue: slices,maps: delegate go go1.21 packages of the same name

@adonovan
Copy link
Member

adonovan commented Dec 14, 2024

Can we narrow this proposal to the interface between source code and automated migration tools (for example, the precise syntax of the annotation comment, the rules governing where it may be used, and the expectations of tooling), and treat the embodiment of such tools (whether in cmd/fix, cmd/vet, gopls, or elsewhere) as out of scope?

We are ready to start deploying an analyzer for automated inlining of annotated functions within gopls, for interactive use, and we are blocked mainly by the question of which syntax to use.

@adonovan adonovan moved this from Hold to Active in Proposals Dec 14, 2024
gopherbot pushed a commit to golang/exp that referenced this issue Dec 15, 2024
This change replaces the body of almost every function in the package
with a call to its corresponding std function of the same name
and--assumedly--semantics.

If proposal golang/go#32816 is accepted, we will annotate each function
with a "//go:fix inline" comment so that automated tooling
such as golang.org/x/tools/internal/refactor/inline/analyzer
can automatically inline the wrappers.

(This CL is deemed to fix golang/go#70717 because it reduces maps.Clear
to the same status as all the other maps functions.)

Updates golang/go#32816
Updates golang/go#70815
Fixes   golang/go#70717

Change-Id: I85246df07f903af97673b80024acdcae057b9f63
Reviewed-on: https://go-review.googlesource.com/c/exp/+/635680
Auto-Submit: Alan Donovan <adonovan@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Proposal Proposal-Hold
Projects
Status: Active
Development

No branches or pull requests

11 participants