diff --git a/spec/compiler/codegen/regex_literal_spec.cr b/spec/compiler/codegen/regex_literal_spec.cr new file mode 100644 index 000000000000..39506aa06b1f --- /dev/null +++ b/spec/compiler/codegen/regex_literal_spec.cr @@ -0,0 +1,17 @@ +require "../../spec_helper" + +describe "Code gen: regex literal spec" do + it "works in a class variable (#10951)" do + run(%( + require "prelude" + class Foo + @@regex = /whatever/ + + def self.check_regex + @@regex == /whatever/ + end + end + Foo.check_regex + )).to_b.should eq(true) + end +end diff --git a/src/compiler/crystal/semantic/literal_expander.cr b/src/compiler/crystal/semantic/literal_expander.cr index d44c8b08f8f9..a93a50a49b4f 100644 --- a/src/compiler/crystal/semantic/literal_expander.cr +++ b/src/compiler/crystal/semantic/literal_expander.cr @@ -281,9 +281,11 @@ module Crystal const = Const.new(@program, @program, const_name, const_value) @program.types[const_name] = const + else + const = @program.types[const_name].as(Const) end - Path.new(const_name) + Path.new(const_name).at(const.value) else regex_new_call(node, node_value) end