From fc29d8360440c39cd1934c1a5f3995c6c069d6a5 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Mon, 6 Apr 2026 22:35:42 +0900 Subject: [PATCH 1/3] Return early on generic args in non-raising lookup Fix #14731 --- src/compiler/crystal/semantic/type_lookup.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/crystal/semantic/type_lookup.cr b/src/compiler/crystal/semantic/type_lookup.cr index fc7d9db2e57a..09a5a4b10eca 100644 --- a/src/compiler/crystal/semantic/type_lookup.cr +++ b/src/compiler/crystal/semantic/type_lookup.cr @@ -253,7 +253,7 @@ class Crystal::Type end next when SizeOf, InstanceSizeOf, AlignOf, InstanceAlignOf, OffsetOf - next unless @raise + return unless @raise type_var.raise "can't use #{type_var} as a generic type argument" end From d172bb374567c956141d9f84ba15c6465da1b3e2 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Mon, 6 Apr 2026 22:36:26 +0900 Subject: [PATCH 2/3] Add tests --- spec/compiler/semantic/generic_class_spec.cr | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/spec/compiler/semantic/generic_class_spec.cr b/spec/compiler/semantic/generic_class_spec.cr index 5c094ad0c63f..5cb598f541e8 100644 --- a/spec/compiler/semantic/generic_class_spec.cr +++ b/spec/compiler/semantic/generic_class_spec.cr @@ -1362,4 +1362,45 @@ describe "Semantic: generic class" do Bar(Int32).new.@foo CRYSTAL end + + it "doesn't crash on sizeof in inferred ivar type (#14731)" do + assert_error <<-CRYSTAL, "can't infer the type of instance variable '@x' of Bar(T)" + class Foo(N) + end + + class Bar(T) + @x = Foo(sizeof(T)).new + end + CRYSTAL + end + + it "doesn't crash on offsetof in inferred ivar type (#14731)" do + assert_error <<-CRYSTAL, "can't infer the type of instance variable '@x' of Bar(T)" + class Foo(N) + end + + struct SomeStruct + @field : Int32 = 0 + end + + class Bar(T) + @x = Foo(offsetof(SomeStruct, @field)).new + end + CRYSTAL + end + + it "doesn't crash on sizeof in initialize ivar assignment (#14731)" do + assert_error <<-CRYSTAL, "can't infer the type of instance variable '@x' of Bar(Int32)" + class Foo(N) + end + + class Bar(T) + def initialize + @x = Foo(sizeof(T)).new + end + end + + Bar(Int32).new + CRYSTAL + end end From df634911eee2bef052282312a001aa50e2b28ccc Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 8 Apr 2026 21:03:07 +0900 Subject: [PATCH 3/3] Revert "Add tests" It has been proposed to allow integer expressions and `sizeof()` as part of type notation.(#5427) This reverts commit d172bb374567c956141d9f84ba15c6465da1b3e2. --- spec/compiler/semantic/generic_class_spec.cr | 41 -------------------- 1 file changed, 41 deletions(-) diff --git a/spec/compiler/semantic/generic_class_spec.cr b/spec/compiler/semantic/generic_class_spec.cr index 5cb598f541e8..5c094ad0c63f 100644 --- a/spec/compiler/semantic/generic_class_spec.cr +++ b/spec/compiler/semantic/generic_class_spec.cr @@ -1362,45 +1362,4 @@ describe "Semantic: generic class" do Bar(Int32).new.@foo CRYSTAL end - - it "doesn't crash on sizeof in inferred ivar type (#14731)" do - assert_error <<-CRYSTAL, "can't infer the type of instance variable '@x' of Bar(T)" - class Foo(N) - end - - class Bar(T) - @x = Foo(sizeof(T)).new - end - CRYSTAL - end - - it "doesn't crash on offsetof in inferred ivar type (#14731)" do - assert_error <<-CRYSTAL, "can't infer the type of instance variable '@x' of Bar(T)" - class Foo(N) - end - - struct SomeStruct - @field : Int32 = 0 - end - - class Bar(T) - @x = Foo(offsetof(SomeStruct, @field)).new - end - CRYSTAL - end - - it "doesn't crash on sizeof in initialize ivar assignment (#14731)" do - assert_error <<-CRYSTAL, "can't infer the type of instance variable '@x' of Bar(Int32)" - class Foo(N) - end - - class Bar(T) - def initialize - @x = Foo(sizeof(T)).new - end - end - - Bar(Int32).new - CRYSTAL - end end