diff --git a/lib/ffi/struct.rb b/lib/ffi/struct.rb index bfb607142..0d59eea6d 100644 --- a/lib/ffi/struct.rb +++ b/lib/ffi/struct.rb @@ -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 diff --git a/spec/ffi/struct_spec.rb b/spec/ffi/struct_spec.rb index b86b0d979..45e10ec88 100644 --- a/spec/ffi/struct_spec.rb +++ b/spec/ffi/struct_spec.rb @@ -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