Skip to content

lang: no need for pointer.Of in Go 1.26#27875

Open
pkazmierczak wants to merge 3 commits intomainfrom
f-pointer-of-new
Open

lang: no need for pointer.Of in Go 1.26#27875
pkazmierczak wants to merge 3 commits intomainfrom
f-pointer-of-new

Conversation

@pkazmierczak
Copy link
Copy Markdown
Contributor

No description provided.

@pkazmierczak pkazmierczak changed the title lang: no need for pointer.Of in Go 1.26 lang: no need for pointer.Of in Go 1.26 Apr 23, 2026
@pkazmierczak pkazmierczak marked this pull request as ready for review April 23, 2026 16:09
@pkazmierczak pkazmierczak requested review from a team as code owners April 23, 2026 16:09
@pkazmierczak pkazmierczak requested a review from gulducat April 23, 2026 16:10
@gulducat gulducat mentioned this pull request May 5, 2026
7 tasks
@gulducat
Copy link
Copy Markdown
Member

gulducat commented May 5, 2026

LGTM! I'll happily approve after mergeflicts are resolved.

Because the changeset is so big, I did not look at every line with my eyeballs. Here is how I checked that the contents X of pointer.Of(X) was always retained within a respective new(X), basically verifying what presumably was a sed search/replace by iterating over git diff --word-diff.

I got a bit carried away with Python

but for posterity, here's what I ran:

#!/usr/bin/env python3

import re
import subprocess

class change:
    def __init__(self, file, new, ptr, before, after):
        self.file = file  # filename 
        self.new = new    # new()
        self.ptr = ptr    # pointer.Of()
        # lines before and after for context
        self.before = before
        self.after = after

    def is_copacetic(self):
        # simple cases where type is inferred
        # pointer.Of(val) => new(val)
        if self.new == self.ptr.replace("-pointer.Of", "+new"):
            return True 

        # within the pointer package
        # Of(val) => new(val)
        if self.file.startswith("helper/pointer") \
            and self.new == self.ptr.replace("-Of(", "+new("):
            return True

        # getting a bit silly now, detect when the type is specified
        # pointer.Of[type](val) => new(type(val))
        groups = re.match(r"-pointer.Of\[(?P<type>[\d\w\.]+)\]\((?P<val>.*)\)", self.ptr)
        if groups:
            type = groups.group("type")
            val = groups.group("val")
            # strip occasional type redundancy
            # pointer.Of[type](type(val)) => pointer.Of[type](val)
            val = val.lstrip(f"{type}(").rstrip(")")
            expect = f"+new({type}({val})"
            # little variations on the suffix
            if self.new == expect \
                or self.new == expect + ")" \
                or self.new == expect + "),":
                return True
        return False

    def __repr__(self):
        return f'{self.file=} {self.new=} {self.ptr=}'

    def __str__(self):
        return f"""\
>>> {self.file}
{self.before}
>>> {self.ptr}
>>> {self.new}
{self.after}"""


if __name__ == '__main__':
    lines = subprocess.check_output(
        # at the time of this script, HEAD~3 is just before these changes.
        ["git", "diff", "HEAD~3", "HEAD", "--word-diff=porcelain"]
    ).decode("utf-8").splitlines()
    
    # clean up "~"-only lines
    lines = [l for l in lines if l != '~']

    # collect changes
    changes = []
    file = None
    for i, line in enumerate(lines):
        if line.startswith('---'):
            file = line.split('--- a/')[1]
        if line.startswith("+new("):
            # each +new should have a -pointer.Of right before it,
            # so we have everything we need from here.
            changes.append(change(
                file=file,
                new=line,
                ptr=lines[i-1],
                # lines before and after for context
                before="\n".join(lines[i-3:i-1]),
                after="\n".join(lines[i+1:i+4]),
            ))
    
    for ch in changes:
        if not ch.is_copacetic():
            print(ch)

    print(f"\nHOW MANY: {len(changes)}")

Result:

$ ./pointer.of.py
>>> command/agent/agent_endpoint_test.go

        trueP, falseP :=
>>> -pointer.Of(true), pointer.Of(false)
>>> +new(true), new(false)
        cases := []struct {
                acl   *bool
                debug *bool

HOW MANY: 1897

found lots of changes, and the one outlier looks copacetic to me.

Do you intend to backport this, dear friend?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants