Skip to content

Commit

Permalink
Merge pull request #507 from soutaro/validate-type-children
Browse files Browse the repository at this point in the history
Validate type decendants
  • Loading branch information
soutaro authored Mar 11, 2022
2 parents 40b53ba + 39e10aa commit 7b1efcd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/steep/signature/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,7 @@ def validate_type_application_constraints(type_name, type_params, type_args, loc
end
end

def validate_type(type)
Steep.logger.debug "#{Location.to_string type.location}: Validating #{type}..."

validator.validate_type type, context: [RBS::Namespace.root]

def validate_type_application(type)
name, type_params, type_args =
case type
when RBS::Types::ClassInstance
Expand Down Expand Up @@ -123,6 +119,17 @@ def validate_type(type)
validate_type_application_constraints(type.name, type_params, type_args, location: type.location)
end
end

type.each_type do |child|
validate_type_application(child)
end
end

def validate_type(type)
Steep.logger.debug "#{Location.to_string type.location}: Validating #{type}..."

validator.validate_type type, context: [RBS::Namespace.root]
validate_type_application(type)
end

def ancestor_to_type(ancestor)
Expand Down
59 changes: 58 additions & 1 deletion test/validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def f: () -> x[X]
end
class Bar[X0 < String]
def f: () -> x[X0]
def f: () -> Array[x[X0]]
end
EOF

Expand Down Expand Up @@ -739,6 +739,63 @@ class D1[X < Object] < Base[X]
end
end

def test_validate_type_application_module_self
with_checker <<RBS do |checker|
class Base[X < Numeric]
end
module M1 : Base[Integer]
end
module M2 : Base[String]
end
module M3[X < Integer] : Base[X]
end
module M4[X < String] : Base[X]
end
RBS

Validator.new(checker: checker).tap do |validator|
validator.validate_one_class(TypeName("::Base"))
assert_predicate validator, :no_error?
end

Validator.new(checker: checker).tap do |validator|
validator.validate_one_class(TypeName("::M1"))
assert_predicate validator, :no_error?
end

Validator.new(checker: checker).tap do |validator|
validator.validate_one_class(TypeName("::M2"))
refute_predicate validator, :no_error?

assert_any!(validator.each_error, size: 1) do |error|
assert_instance_of Diagnostic::Signature::UnsatisfiableTypeApplication, error
assert_equal "Type application of `::Base` doesn't satisfy the constraints: ::String <: ::Numeric", error.header_line
assert_equal "Base[String]", error.location.source
end
end

Validator.new(checker: checker).tap do |validator|
validator.validate_one_class(TypeName("::M3"))
assert_predicate validator, :no_error?
end

Validator.new(checker: checker).tap do |validator|
validator.validate_one_class(TypeName("::M4"))
refute_predicate validator, :no_error?

assert_any!(validator.each_error, size: 1) do |error|
assert_instance_of Diagnostic::Signature::UnsatisfiableTypeApplication, error
assert_equal "Type application of `::Base` doesn't satisfy the constraints: X <: ::Numeric", error.header_line
assert_equal "Base[X]", error.location.source
end
end
end
end

def test_validate_type_application_mixin
with_checker <<RBS do |checker|
module M[X < Numeric]
Expand Down

0 comments on commit 7b1efcd

Please sign in to comment.