Skip to content

Commit

Permalink
Fix ReadInstanceVar on typedefs (#14044)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Dec 5, 2023
1 parent 25d3303 commit 70095f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
34 changes: 30 additions & 4 deletions spec/compiler/semantic/c_type_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@ require "../../spec_helper"

describe "Semantic: type" do
it "can call methods of original type" do
assert_type("
assert_type(<<-CRYSTAL, inject_primitives: true) { uint64 }
lib Lib
type X = Void*
fun foo : X
end
Lib.foo.address
", inject_primitives: true) { uint64 }
CRYSTAL
end

it "can call methods of parent type" do
assert_error("
assert_error(<<-CRYSTAL, "undefined method 'baz'")
lib Lib
type X = Void*
fun foo : X
end
Lib.foo.baz
", "undefined method 'baz'")
CRYSTAL
end

it "can access instance variables of original type" do
assert_type(<<-CRYSTAL) { int32 }
lib Lib
struct X
x : Int32
end
type Y = X
fun foo : Y
end
Lib.foo.@x
CRYSTAL
end

it "errors if original type doesn't support instance variables" do
assert_error(<<-CRYSTAL, "can't use instance variables inside primitive types (at Int32)")
lib Lib
type X = Int32
fun foo : X
end
Lib.foo.@x
CRYSTAL
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2700,7 +2700,7 @@ module Crystal
end

delegate remove_typedef, pointer?, defs,
macros, reference_like?, parents, to: typedef
macros, reference_like?, parents, lookup_instance_var, to: typedef

def remove_indirection
self
Expand Down

0 comments on commit 70095f4

Please sign in to comment.