Skip to content

Commit

Permalink
Fix: Display Bool's size as 1 byte in crystal tool hierarchy, not…
Browse files Browse the repository at this point in the history
… 0 (#13588)
  • Loading branch information
HertzDevil authored Jun 27, 2023
1 parent 742d920 commit 9565027
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
20 changes: 20 additions & 0 deletions spec/compiler/crystal/tools/hierarchy_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ describe Crystal::TextHierarchyPrinter do
+- class Bar (4 bytes)\n
EOS
end

it "shows correct size for Bool member" do
program = semantic(<<-CRYSTAL).program
struct Foo
@x = true
end
CRYSTAL

output = String.build { |io| Crystal.print_hierarchy(program, io, "Foo", "text") }
output.should eq(<<-EOS)
- class Object (4 bytes)
|
+- struct Value (0 bytes)
|
+- struct Struct (0 bytes)
|
+- struct Foo (1 bytes)
@x : Bool (1 bytes)\n
EOS
end
end

describe Crystal::JSONHierarchyPrinter do
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ module Crystal
# `Pointer(Void).malloc` must work like `Pointer(UInt8).malloc`,
# that is, consider Void like the size of a byte.
1
elsif type.is_a?(BoolType)
# LLVM reports 0 for bool (i1) but it must be 1 because it does occupy memory
1
else
llvm_typer.size_of(llvm_typer.llvm_type(type))
end
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/interpreter/compiler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,8 @@ class Crystal::Repl::Compiler < Crystal::Visitor
args = node.args
obj_type = obj.try(&.type) || target_def.owner

if obj_type == @context.program
# TODO: should this use `Type#passed_as_self?` instead?
if obj_type == @context.program || obj_type.is_a?(FileModule)
# Nothing
elsif obj_type.passed_by_value?
args_bytesize += sizeof(Pointer(UInt8))
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/interpreter/local_vars.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class Crystal::Repl::LocalVars

def declare(name : String, type : Type) : Int32?
is_self = name == "self"
return if is_self && type.is_a?(Program)
# TODO: should this use `Type#passed_as_self?` instead?
return if is_self && (type.is_a?(Program) || type.is_a?(FileModule))

key = Key.new(name, @block_level)

Expand Down
3 changes: 2 additions & 1 deletion src/llvm/target_data.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ struct LLVM::TargetData
end

def size_in_bytes(type)
size_in_bits(type) // 8
size_in_bits = size_in_bits(type)
size_in_bits // 8 &+ (size_in_bits & 0x7 != 0 ? 1 : 0)
end

def abi_size(type)
Expand Down

0 comments on commit 9565027

Please sign in to comment.