Skip to content

Commit 2ee575f

Browse files
authored
Revert "fix: avoid overlapping definition for T::Enum synthetic serialize() m…" (#249)
This reverts commit 0584803.
1 parent 0584803 commit 2ee575f

3 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎rewriter/TEnum.cc‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,22 @@ void TEnum::run(core::MutableContext ctx, ast::ClassDef *klass) {
238238
}
239239
}
240240
}
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
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+
auto method = ast::MK::SyntheticMethod0(klass->loc, klass->declLoc, klass->name.loc(), core::Names::serialize(),
247+
ast::MK::RaiseTypedUnimplemented(klass->declLoc));
248+
ast::Send::ARGS_store nargs;
249+
ast::Send::Flags flags;
250+
flags.isPrivateOk = true;
251+
auto visibility = ast::MK::Send(klass->declLoc, ast::MK::Self(klass->declLoc), core::Names::public_(),
252+
klass->declLoc, 0, std::move(nargs), flags);
253+
254+
klass->rhs.emplace_back(std::move(visibility));
255+
klass->rhs.emplace_back(std::move(sig));
256+
klass->rhs.emplace_back(std::move(method));
257+
}
247258
}
248259
}; // namespace sorbet::rewriter

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def myfunction(myparam)
4848

4949
class X < T::Enum
5050
# ^ definition [..] X#
51+
# ^ definition [..] X#serialize().
5152
# ^ reference [..] T#
53+
# ^^^^ reference [..] Module#public().
54+
# ^^^^ reference [..] String#
5255
# ^^^^ reference [..] T#Enum#
5356
enums do
5457
A = new("A")
@@ -67,7 +70,7 @@ class X < T::Enum
6770
# ^^^ definition [..] X#All.
6871
# ^ reference [..] X#A.
6972
# ^ reference [..] X#B.
70-
# ^^^^^^^^ definition local 3$119448696
73+
# ^^^^^^^^ definition local 4$119448696
7174
# ^ reference [..] X#
7275
end
7376

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
class X < T::Enum
44
# ^ definition [..] X#
5+
# ^ definition [..] X#serialize().
56
# ^ reference [..] T#
7+
# ^^^^ reference [..] Module#public().
8+
# ^^^^ reference [..] String#
69
# ^^^^ reference [..] T#Enum#
710
enums do
811
A = new("A")
@@ -21,7 +24,7 @@ class X < T::Enum
2124
# ^^^ definition [..] X#All.
2225
# ^ reference [..] X#A.
2326
# ^ reference [..] X#B.
24-
# ^^^^^^^^ definition local 3$119448696
27+
# ^^^^^^^^ definition local 4$119448696
2528
# ^ reference [..] X#
2629
end
2730

0 commit comments

Comments
 (0)