From 034b05c53c43bd52129f706e7c3e5f120208ca3f Mon Sep 17 00:00:00 2001 From: xgopilot Date: Fri, 14 Nov 2025 14:49:30 +0000 Subject: [PATCH] cl: remove Underlying() call to reject string type aliases in rewrites Type aliases like `type T string` are no longer supported for -ldflags -X rewrites. Only direct *string types are now allowed. - Removed Underlying() call from isStringPtrType - Added TestRewriteIgnoresStringAlias test case Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: cpunion <8459+cpunion@users.noreply.github.com> --- cl/compile.go | 2 +- cl/rewrite_internal_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/cl/compile.go b/cl/compile.go index 2f770667..d9381137 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -151,7 +151,7 @@ func (p *context) isStringPtrType(typ types.Type) bool { if !ok { return false } - basic, ok := ptr.Elem().Underlying().(*types.Basic) + basic, ok := ptr.Elem().(*types.Basic) return ok && basic.Kind() == types.String } diff --git a/cl/rewrite_internal_test.go b/cl/rewrite_internal_test.go index 041c6058..8fbf89a3 100644 --- a/cl/rewrite_internal_test.go +++ b/cl/rewrite_internal_test.go @@ -153,3 +153,17 @@ var VarStruct = wrapper{v: 1} t.Fatalf("non-string variables must not be rewritten:\n%s", ir) } } + +func TestRewriteIgnoresStringAlias(t *testing.T) { + const src = `package rewritepkg +type T string +var VarAlias T = "original_value" +` + ir := compileWithRewrites(t, src, map[string]string{"VarAlias": "rewrite_alias"}) + if strings.Contains(ir, `c"rewrite_alias"`) { + t.Fatalf("string alias types must not be rewritten:\n%s", ir) + } + if !strings.Contains(ir, `c"original_value"`) { + t.Fatalf("original value should remain for alias type:\n%s", ir) + } +}