module T::Props::Private::SetterFactory

def self.build_setter_proc(klass, prop, rules)

def self.build_setter_proc(klass, prop, rules)
  # Our nil check works differently than a simple T.nilable for various
  # reasons (including the `raise_on_nil_write` setting, the existence
  # of defaults & factories, and the fact that we allow `T.nilable(Foo)`
  # where Foo < T::Props::CustomType as a prop type even though calling
  # `valid?` on it won't work as expected), so unwrap any T.nilable and
  # do a check manually. (Note this hack does not fix custom types as
  # collection elements.)
  non_nil_type = T::Utils::Nilable.get_underlying_type_object(rules.fetch(:type_object))
  if non_nil_type.is_a?(T::Types::Simple) && non_nil_type.raw_type.singleton_class < T::Props::CustomType
    non_nil_type = non_nil_type.raw_type
  end
  accessor_key = rules.fetch(:accessor_key)
  validate = rules[:setter_validate]
  # It seems like a bug that this affects the behavior of setters, but
  # some existing code relies on this behavior
  has_explicit_nil_default = rules.key?(:default) && rules.fetch(:default).nil?
  # Use separate methods in order to ensure that we only close over necessary
  # variables
  if !T::Props::Utils.need_nil_write_check?(rules) || has_explicit_nil_default
    nilable_proc(prop, accessor_key, non_nil_type, klass, validate)
  else
    non_nil_proc(prop, accessor_key, non_nil_type, klass, validate)
  end
end

def self.nilable_proc(prop, accessor_key, non_nil_type, klass, validate)

def self.nilable_proc(prop, accessor_key, non_nil_type, klass, validate)
le_set(accessor_key, nil)
e.valid?(val)
(prop, val)
le_set(accessor_key, val)
te::SetterFactory.raise_pretty_error(

def self.non_nil_proc(prop, accessor_key, non_nil_type, klass, validate)

def self.non_nil_proc(prop, accessor_key, non_nil_type, klass, validate)
alid?(val)
(prop, val)
le_set(accessor_key, val)
te::SetterFactory.raise_pretty_error(

def self.raise_pretty_error(klass, prop, type, val)

def self.raise_pretty_error(klass, prop, type, val)
  base_message = "Can't set #{klass.name}.#{prop} to #{val.inspect} (instance of #{val.class}) - need a #{type}"
  pretty_message = "Parameter '#{prop}': #{base_message}\n"
  caller_loc = caller_locations&.find {|l| !l.to_s.include?('sorbet-runtime/lib/types/props')}
  if caller_loc
    pretty_message += "Caller: #{caller_loc.path}:#{caller_loc.lineno}\n"
  end
  T::Configuration.call_validation_error_handler(
    nil,
    message: base_message,
    pretty_message: pretty_message,
    kind: 'Parameter',
    name: prop,
    type: type,
    value: val,
    location: caller_loc,
  )
end