Skip to content

Commit b084e47

Browse files
authored
Fix Loc-related crash in definition_validator (#7454)
* Add failing test ❯ sorbet test/testdata/definition_validator/superclass_class_loc__1.rb test/testdata/definition_validator/superclass_class_loc__2.rbi msg="Bad offset2Pos off" path="test/testdata/definition_validator/superclass_class_loc__1.rb" off="778"" source="# typed: true\n\nmodule Parent\nend\n\nclass Child < Parent\nend\n" Exception::raise(): core/Loc.cc:43 enforced condition false has failed: (no message provided) Backtrace: #3 0xbb3f32 #4 0x1a5b870 sorbet::core::Loc::offset2Pos() #5 0x1a5bbda sorbet::core::Loc::position() #6 0x1a5da17 sorbet::core::Loc::filePosToString() #7 0x19df6b8 sorbet::core::Error::toString() #8 0x19f292e sorbet::core::ErrorQueue::pushError() #9 0x1a3e882 sorbet::core::GlobalState::_error() #10 0x19e11c0 sorbet::core::ErrorBuilder::~ErrorBuilder() #11 0x1288d5a sorbet::definition_validator::(anonymous namespace)::validateSuperClass() #12 0x12861b6 sorbet::definition_validator::(anonymous namespace)::ValidateWalk::preTransformClassDef() #13 0x1285f6a sorbet::ast::MapFunctions<>::CALL_MEMBER_impl_preTransformClassDef<>::call<>() #14 0x1284c0f sorbet::ast::TreeMapper<>::mapClassDef() #15 0x1283d55 sorbet::ast::TreeMapper<>::mapIt() #16 0x1285a58 sorbet::ast::TreeMapper<>::mapInsSeq() #17 0x12846a6 sorbet::ast::TreeMapper<>::mapIt() #18 0x12839fa sorbet::ast::TreeWalk::apply<>() #19 0x12838db sorbet::definition_validator::runOne() #20 0xf5e8b0 sorbet::realmain::pipeline::(anonymous namespace)::typecheckOne() #21 0xf5e50d sorbet::realmain::pipeline::typecheck()::$_5::operator()() #22 0xf5e19d std::__1::__invoke<>() #23 0xf5e14d std::__1::__invoke_void_return_wrapper<>::__call<>() #24 0xf5e11d std::__1::__function::__alloc_func<>::operator()() #25 0xf5d2e9 std::__1::__function::__func<>::operator()() #26 0x1753d52 std::__1::__function::__value_func<>::operator()() #27 0x174a5f5 std::__1::function<>::operator()() #28 0x1bf5d7a sorbet::WorkerPoolImpl::multiplexJob()::$_2::operator()() #29 0x1bf5d1d std::__1::__invoke<>() #30 0x1bf5ccd std::__1::__invoke_void_return_wrapper<>::__call<>() #31 0x1bf5c9d std::__1::__function::__alloc_func<>::operator()() #32 0x1bf4ea9 std::__1::__function::__func<>::operator()() #33 0x1bfa5b2 std::__1::__function::__value_func<>::operator()() #34 0x1bf91d5 std::__1::function<>::operator()() #35 0x1bf313d sorbet::WorkerPoolImpl::WorkerPoolImpl()::$_0::operator()() #36 0x1bf307d std::__1::__invoke<>() #37 0x1bf302d std::__1::__invoke_void_return_wrapper<>::__call<>() #38 0x1bf2ffd std::__1::__function::__alloc_func<>::operator()() #39 0x1bf2219 std::__1::__function::__func<>::operator()() #40 0x1753d52 std::__1::__function::__value_func<>::operator()() #41 0x174a5f5 std::__1::function<>::operator()() #42 0x1bfdd53 Joinable::trampoline() #43 0x7f5e1d694b43 #44 0x7f5e1d726a00 * Fix failing test
1 parent 024c489 commit b084e47

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

definition_validator/validator.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,7 @@ void validateSuperClass(core::Context ctx, const core::ClassOrModuleRef sym, con
740740
return;
741741
}
742742

743-
if (auto e = ctx.state.beginError(core::Loc(sym.data(ctx)->loc().file(), classDef.declLoc),
744-
core::errors::Resolver::NonClassSuperclass)) {
743+
if (auto e = ctx.state.beginError(ctx.locAt(classDef.declLoc), core::errors::Resolver::NonClassSuperclass)) {
745744
auto superClassFqn = superClass.show(ctx);
746745
e.setHeader("The super class `{}` of `{}` does not derive from `{}`", superClassFqn, sym.show(ctx),
747746
core::Symbols::Class().show(ctx));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# typed: true
2+
3+
module Parent
4+
end
5+
6+
class Child < Parent # error: does not derive from `Class`
7+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# typed: true
2+
3+
# This is a rather large comment at the start of the file which exposes a bug
4+
# where we were accidentally turning a LocOffset from this file into a Loc for
5+
# the other file. With a sufficiently long comment, that causes an array access
6+
# out of bounds.
7+
# This is a rather large comment at the start of the file which exposes a bug
8+
# where we were accidentally turning a LocOffset from this file into a Loc for
9+
# the other file. With a sufficiently long comment, that causes an array access
10+
# out of bounds.
11+
# This is a rather large comment at the start of the file which exposes a bug
12+
# where we were accidentally turning a LocOffset from this file into a Loc for
13+
# the other file. With a sufficiently long comment, that causes an array access
14+
# out of bounds.
15+
16+
class Child < Parent # error: does not derive from `Class`
17+
def example; end
18+
end

0 commit comments

Comments
 (0)