Closed
Description
Some new gophers may make mistakes by copying bytes.Buffer
and strings.Builder
values.
Embed a noCopy will let vet find such mistakes.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
dsnet commentedon Jun 15, 2018
Unfortunately, that will give the
bytes.Buffer
andstrings.Builder
types aLock
method. I think we need a cleaner way to tell the vet something is not copy-able.ianlancetaylor commentedon Jun 15, 2018
Note that
strings.Builder
already has a dynamic no-copy check.dotaheor commentedon Jun 16, 2018
@dsnet sorry, my mistake, the
noCopy
should be non-anonymous so that its methods will not be exported. Just like whatsync.WaitGroup
does.@ianlancetaylor I didn't notice that
strings.Builder
has a dynamic no-copy check. But it looks the dynamic check doesn't work for some situations, and it increases the run-time burden.dsnet commentedon Jun 17, 2018
In what cases does it not work? Even if we add
noCopy
tostrings.Builder
, we would still want the dynamic check since vet is not run as part of the normal build process. Otherwisestrings.Builder
would provide too easy a way to accidentally (or even deliberately) step outside Go type safety.dotaheor commentedon Jun 18, 2018
If a Builder is copied before it writes anything, then the dynamic check doesn't work.
On the other hand, I don't understand what safety the dynamic check guarantees. I couldn't imagine any type unsafety caused by copying a Builder. It looks there are only some unexpected results from the String method calls.
ianlancetaylor commentedon Jun 18, 2018
It's OK to copy a
strings.Builder
before writing anything, in the sense that both copies will work correctly.The dynamic check is there to ensure that the
String
method returns a value that can not be modified by calls to theWrite
method in a copy of theBuilder
. In general lots of code assumes that Go strings can not be modified.ianlancetaylor commentedon Jun 18, 2018
Actually adding a
Lock
method does not sound like a good idea, as @dsnet mentioned above.As described in cmd/vet/README, vet checks should be for problems that are correctness errors, frequent, and where the vet check can be precise. I guess that a small
bytes.Buffer
could have a correctness issue, in that there could be unanticipated aliasing of the small buffer. And a proper check could be precise. But I'm not sure this is a frequent problem.dotaheor commentedon Jun 19, 2018
OK, get it.
How about change the type of the
Builder.buf
field to*[]byte
? So that we the dynamic check is not needed any more.ianlancetaylor commentedon Jun 19, 2018
Using a pointer instead of a slice would normally require an additional allocation, which would be unfortunate.
dotaheor commentedon Jun 20, 2018
Yes, changing the field to a pointer also hurts the performance.
Thanks for the discussion.
Looks this issue is not very valuable, so I will close it.