Skip to content

Pattern-matching error with KeywordArgs in 0.17.1 #307

@caseyohara

Description

@caseyohara

I recently updated to 0.17.1 and ran into an issue with pattern-matching. Contracts seems to think two methods have the same signature even when they are different.

I downgraded to 0.17.0 to confirm the problem is indeed in 0.17.1.

Versions

  • Contracts 0.17.1
  • Tested with Ruby 3.2.1, 3.2.4, 3.3.5

How to reproduce

require "contracts"

puts "Ruby #{RUBY_VERSION}"
puts "Contracts #{Contracts::VERSION}"

class Animal
  include Contracts::Core
  include Contracts::Builtin

  Contract KeywordArgs[age: Int, size: Symbol] => String
  def speak(age:, size:)
    "age: #{age} size: #{size}"
  end

  Contract KeywordArgs[sound: String] => String
  def speak(sound:)
    "sound: #{sound}"
  end
end

animal = Animal.new
puts animal.speak(age: 5, size: :large)
puts animal.speak(sound: "woof")

Expected output

Ruby 3.2.1
Contracts 0.17.1
age: 5 size: large
sound: woof

Actual output

Ruby 3.2.1
Contracts 0.17.1

/gems/contracts-0.17.1/lib/contracts/method_handler.rb:183:in `validate_pattern_matching!':  (ContractError)
It looks like you are trying to use pattern-matching, but
multiple definitions for function 'speak' have the same
contract for input parameters:

 => String
 => String

Each definition needs to have a different contract for the parameters.

	from /gems/contracts-0.17.1/lib/contracts/method_handler.rb:34:in `handle'
	from /gems/contracts-0.17.1/lib/contracts/decorators.rb:15:in `method_added'
	from contracts-test.rb:16:in `<class:Animal>'
	from contracts-test.rb:6:in `<main>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions