Skip to content

Commit

Permalink
Warn about Struct layout redefinition only
Browse files Browse the repository at this point in the history
Since raising an exception is an API change but it doesn't justify a major version bump, just warn in that case.
  • Loading branch information
larskanis committed Jan 9, 2020
1 parent bd45ff5 commit 89f4258
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/ffi/struct.rb
Expand Up @@ -204,7 +204,7 @@ class << self
# end
# @note Creating a layout from a hash +spec+ is supported only for Ruby 1.9.
def layout(*spec)
raise RuntimeError, "struct layout already defined for #{self.inspect}" if defined?(@layout)
warn "[DEPRECATION] Struct layout is already defined for class #{self.inspect}. Redefinition as in #{caller[0]} will be disallowed in ffi-2.0." if defined?(@layout)
return @layout if spec.size == 0

builder = StructLayoutBuilder.new
Expand Down
25 changes: 18 additions & 7 deletions spec/ffi/struct_spec.rb
Expand Up @@ -463,13 +463,24 @@ class TestStruct < FFI::Struct
expect(s.offset_of(:c)).to eq(8)
end

it "denies redefinition of struct layouts" do
expect do
Class.new(FFI::Struct) do
layout :a, :int
layout :a, :int
end
end.to raise_error(/struct layout already defined/)
if FFI::VERSION < "2"
it "warns about redefinition of struct layouts" do
expect do
Class.new(FFI::Struct) do
layout :a, :int
layout :a, :int
end
end.to output(/Redefinition .* will be disallowed in ffi-2.0/).to_stderr
end
else
it "denies redefinition of struct layouts" do
expect do
Class.new(FFI::Struct) do
layout :a, :int
layout :a, :int
end
end.to raise_error(/struct layout already defined/)
end
end

it "allows redefinition of struct layouts in derived classes" do
Expand Down

0 comments on commit 89f4258

Please sign in to comment.