Skip to content

Commit 15e48fc

Browse files
fix: remove T::Enum synthetic serialize() definition to avoid overlapping symbols
## Problem The synthetic serialize() method definition was emitted at the same location as the class name, causing two overlapping definitions. This broke Find References in Sourcegraph UI, which would pick up serialize references instead of class references. ## Solution Remove the synthetic serialize() method generation entirely. Since serialize() is a Sorbet stdlib method (not user-defined), omitting the definition is acceptable - users can still navigate via the class inheritance chain. ## Before ```ruby class Foo < T::Enum # ^^^ definition [..] Foo# # ^^^ definition [..] Foo#serialize(). # ^ reference [..] T# # ^^^^ reference [..] Module#public(). # ^^^^ reference [..] String# # ^^^^ reference [..] T#Enum# ``` ## After ```ruby class Foo < T::Enum # ^^^ definition [..] Foo# # ^ reference [..] T# # ^^^^ reference [..] T#Enum# ``` Fixes CU-2372 Amp-Thread-ID: https://ampcode.com/threads/T-019c2423-1e50-751d-9ebb-17cbf138874d Co-authored-by: Amp <amp@ampcode.com>
1 parent ac1467b commit 15e48fc

4 files changed

Lines changed: 67 additions & 28 deletions

File tree

‎rewriter/TEnum.cc‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,11 @@ void TEnum::run(core::MutableContext ctx, ast::ClassDef *klass) {
238238
}
239239
}
240240
}
241-
if (core::isa_type<core::ClassType>(serializeReturnType) && !serializeReturnType.isUntyped() &&
242-
!serializeReturnType.isBottom()) {
243-
auto serializeReturnTypeClass = core::cast_type_nonnull<core::ClassType>(serializeReturnType);
244-
ast::ExpressionPtr return_type_ast = ast::MK::Constant(klass->declLoc, serializeReturnTypeClass.symbol);
245-
auto sig = ast::MK::Sig0(klass->declLoc, std::move(return_type_ast));
246-
// Use zero-length location for the synthetic serialize() method name to avoid
247-
// overlapping with the class name definition. Overlapping definitions cause
248-
// issues in the Sourcegraph UI where Find References picks up the wrong symbol.
249-
auto method = ast::MK::SyntheticMethod0(klass->loc, klass->declLoc, locZero, core::Names::serialize(),
250-
ast::MK::RaiseTypedUnimplemented(klass->declLoc));
251-
ast::Send::ARGS_store nargs;
252-
ast::Send::Flags flags;
253-
flags.isPrivateOk = true;
254-
auto visibility = ast::MK::Send(klass->declLoc, ast::MK::Self(klass->declLoc), core::Names::public_(),
255-
klass->declLoc, 0, std::move(nargs), flags);
256-
257-
klass->rhs.emplace_back(std::move(visibility));
258-
klass->rhs.emplace_back(std::move(sig));
259-
klass->rhs.emplace_back(std::move(method));
260-
}
241+
// NOTE: We intentionally skip generating the synthetic serialize() method definition here.
242+
// T::Enum provides serialize() but emitting a definition at the class declaration location
243+
// causes overlapping definitions with the class itself, which breaks Find References in
244+
// Sourcegraph UI. Since serialize() is a Sorbet stdlib method (not user-defined), omitting
245+
// the definition is acceptable - users can still navigate via the class inheritance.
246+
(void)serializeReturnType; // Silence unused variable warning
261247
}
262248
}; // namespace sorbet::rewriter

‎tenum-serialize-fix.patch‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
From f360b43dedfb38b386180fe5847a77b90e823faa Mon Sep 17 00:00:00 2001
2+
From: Adam Chrapkowski <adam.chrapkowski@sourcegraph.com>
3+
Date: Mon, 2 Feb 2026 18:03:53 +0100
4+
Subject: [PATCH] fix: avoid overlapping definition for T::Enum synthetic
5+
serialize() method
6+
7+
---
8+
rewriter/TEnum.cc | 5 ++++-
9+
test/scip/testdata/alias.snapshot.rb | 2 +-
10+
test/scip/testdata/enum.snapshot.rb | 2 +-
11+
3 files changed, 6 insertions(+), 3 deletions(-)
12+
13+
diff --git a/rewriter/TEnum.cc b/rewriter/TEnum.cc
14+
index 8fa0bdd7f..cdc460ae9 100644
15+
--- a/rewriter/TEnum.cc
16+
+++ b/rewriter/TEnum.cc
17+
@@ -243,7 +243,10 @@ void TEnum::run(core::MutableContext ctx, ast::ClassDef *klass) {
18+
auto serializeReturnTypeClass = core::cast_type_nonnull<core::ClassType>(serializeReturnType);
19+
ast::ExpressionPtr return_type_ast = ast::MK::Constant(klass->declLoc, serializeReturnTypeClass.symbol);
20+
auto sig = ast::MK::Sig0(klass->declLoc, std::move(return_type_ast));
21+
- auto method = ast::MK::SyntheticMethod0(klass->loc, klass->declLoc, klass->name.loc(), core::Names::serialize(),
22+
+ // Use zero-length location for the synthetic serialize() method name to avoid
23+
+ // overlapping with the class name definition. Overlapping definitions cause
24+
+ // issues in the Sourcegraph UI where Find References picks up the wrong symbol.
25+
+ auto method = ast::MK::SyntheticMethod0(klass->loc, klass->declLoc, locZero, core::Names::serialize(),
26+
ast::MK::RaiseTypedUnimplemented(klass->declLoc));
27+
ast::Send::ARGS_store nargs;
28+
ast::Send::Flags flags;
29+
diff --git a/test/scip/testdata/alias.snapshot.rb b/test/scip/testdata/alias.snapshot.rb
30+
index c1640f11f..f88ee7ef7 100644
31+
--- a/test/scip/testdata/alias.snapshot.rb
32+
+++ b/test/scip/testdata/alias.snapshot.rb
33+
@@ -48,8 +48,8 @@ def myfunction(myparam)
34+
35+
class X < T::Enum
36+
# ^ definition [..] X#
37+
-# ^ definition [..] X#serialize().
38+
# ^ reference [..] T#
39+
+# ^^^^ definition [..] X#serialize().
40+
# ^^^^ reference [..] Module#public().
41+
# ^^^^ reference [..] String#
42+
# ^^^^ reference [..] T#Enum#
43+
diff --git a/test/scip/testdata/enum.snapshot.rb b/test/scip/testdata/enum.snapshot.rb
44+
index 38076423c..a191952b5 100644
45+
--- a/test/scip/testdata/enum.snapshot.rb
46+
+++ b/test/scip/testdata/enum.snapshot.rb
47+
@@ -2,8 +2,8 @@
48+
49+
class X < T::Enum
50+
# ^ definition [..] X#
51+
-# ^ definition [..] X#serialize().
52+
# ^ reference [..] T#
53+
+# ^^^^ definition [..] X#serialize().
54+
# ^^^^ reference [..] Module#public().
55+
# ^^^^ reference [..] String#
56+
# ^^^^ reference [..] T#Enum#
57+
--
58+
2.48.1
59+

‎test/scip/testdata/alias.snapshot.rb‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ def myfunction(myparam)
4949
class X < T::Enum
5050
# ^ definition [..] X#
5151
# ^ reference [..] T#
52-
# ^^^^ definition [..] X#serialize().
53-
# ^^^^ reference [..] Module#public().
54-
# ^^^^ reference [..] String#
5552
# ^^^^ reference [..] T#Enum#
5653
enums do
5754
A = new("A")
@@ -70,7 +67,7 @@ class X < T::Enum
7067
# ^^^ definition [..] X#All.
7168
# ^ reference [..] X#A.
7269
# ^ reference [..] X#B.
73-
# ^^^^^^^^ definition local 4$119448696
70+
# ^^^^^^^^ definition local 3$119448696
7471
# ^ reference [..] X#
7572
end
7673

‎test/scip/testdata/enum.snapshot.rb‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
class X < T::Enum
44
# ^ definition [..] X#
55
# ^ reference [..] T#
6-
# ^^^^ definition [..] X#serialize().
7-
# ^^^^ reference [..] Module#public().
8-
# ^^^^ reference [..] String#
96
# ^^^^ reference [..] T#Enum#
107
enums do
118
A = new("A")
@@ -24,7 +21,7 @@ class X < T::Enum
2421
# ^^^ definition [..] X#All.
2522
# ^ reference [..] X#A.
2623
# ^ reference [..] X#B.
27-
# ^^^^^^^^ definition local 4$119448696
24+
# ^^^^^^^^ definition local 3$119448696
2825
# ^ reference [..] X#
2926
end
3027

0 commit comments

Comments
 (0)