Skip to content

Commit

Permalink
Fix hashes
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Dec 20, 2024
1 parent a0f01f4 commit f6672e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
7 changes: 4 additions & 3 deletions rbs/TypeTranslator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,15 @@ sorbet::ast::ExpressionPtr recordType(core::MutableContext ctx,
rbs_ast_symbol_t *keyNode = (rbs_ast_symbol_t *)hash_node->key;
rbs_constant_t *keyString = rbs_constant_pool_id_to_constant(fake_constant_pool, keyNode->constant_id);
keyStr = std::string(keyString->start);
auto keyName = ctx.state.enterNameUTF8(keyStr);
keysStore.emplace_back(ast::MK::Symbol(loc, keyName));
break;
}
case RBS_AST_STRING: {
rbs_ast_string_t *keyNode = (rbs_ast_string_t *)hash_node->key;
keyStr = std::string(keyNode->string.start);
auto keyName = ctx.state.enterNameUTF8(keyStr);
keysStore.emplace_back(ast::MK::String(loc, keyName));
break;
}
default: {
Expand All @@ -301,9 +305,6 @@ sorbet::ast::ExpressionPtr recordType(core::MutableContext ctx,
}
}

auto keyName = ctx.state.enterNameUTF8(keyStr);
keysStore.emplace_back(ast::MK::Symbol(loc, keyName));

if (hash_node->value->type != RBS_TYPES_RECORD_FIELDTYPE) {
if (auto e = ctx.beginError(loc, core::errors::Rewriter::RBSError)) {
e.setHeader("Unexpected node type `{}` in record value type, expected `{}`",
Expand Down
26 changes: 22 additions & 4 deletions test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
# typed: strict
# frozen_string_literal: true

class Final
# @final
#: ?{ (?) -> untyped } -> void
def foo(&block); end
class Foo
extend T::Sig

sig { returns({"name" => String, "amount" => Integer}) }
def test
{ "name" => "Seth", "amount" => 100 }
end

#: -> {"a" => String, "b" => Integer}
def foo
{ "a" => "Foo", "b" => 42 }
end

#: -> {a: String, b: Integer}
def bar
{ a: "Foo", b: 42 }
end

#: -> {"a" => String, :b => Integer}
def baz
{ "a" => "Foo", b: 42 }
end
end
13 changes: 12 additions & 1 deletion test/testdata/rewriter/rbs_signatures_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,18 @@ def tuple_type2; T.unsafe(nil); end

#: -> { id: String, name: String }
def shape_type1; T.unsafe(nil); end
T.reveal_type(shape_type1) # error: Revealed type: `{id: String, name: String} (shape of T::Hash[T.untyped, T.untyped])`

#: -> {"a" => String, "b" => Integer}
def shape_type1; T.unsafe(nil); end
T.reveal_type(shape_type1) # error: Revealed type: `{String("a") => String, String("b") => Integer} (shape of T::Hash[T.untyped, T.untyped])`

#: -> {a: String, b: Integer}
def shape_type2; T.unsafe(nil); end
T.reveal_type(shape_type2) # error: Revealed type: `{a: String, b: Integer} (shape of T::Hash[T.untyped, T.untyped])`

#: -> {"a" => String, :b => Integer}
def shape_type3; T.unsafe(nil); end
T.reveal_type(shape_type3) # error: Revealed type: `{String("a") => String, b: Integer} (shape of T::Hash[T.untyped, T.untyped])`

# Proc types

Expand Down

0 comments on commit f6672e7

Please sign in to comment.