Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort if subtyping fails to (possible) infinite loop #1055

Merged
merged 1 commit into from
Feb 22, 2024
Merged

Conversation

soutaro
Copy link
Owner

@soutaro soutaro commented Feb 21, 2024

Type checking "" =~ "" doesn't stop by infinitely recursive subtyping check.

# Simplified RBS excerpt

interface _MatchAgainst[T]
  def =~: (String) -> T
end

class String
  def =~: (Regexp) -> Integer?
        | [T] (_MatchAgainst[T]) -> T
end

# Ruby code

"" =~ ""

# Subtyping trace

String <: _MatchAgainst[T_1]                           # T_1 is a fresh type variable
  =~
    [T] (_MatchAgainst[T]) -> T <: (String) -> T_1     # Expand =~ method types
      (_MatchAgainst[T_2]) -> T_2 <: (String) -> T_1   # Expand generics with fresh type variable T_2
        String <: _MatchAgainst[T_2]                   # The first subtyping relationship but with T_2, and continues
        T_2 <: T_1

Instead of causing SystemStackError, let subtyping check fail with new LoopAbort error.

@@ -1,6 +1,8 @@
module Steep
module Subtyping
class Check
ABORT_LIMIT = ENV.fetch("STEEP_SUBTYPING_ABORT_LIMIT", 100).to_i
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default limit is 100, but you can override with $STEEP_SUBTYPING_ABORT_LIMIT environment variable.
Note that this is only for troubleshooting and not expecting to be configured by the user for daily uses.

@soutaro
Copy link
Owner Author

soutaro commented Feb 22, 2024

Changed the default limit to 50 because 100 was too big for Windows.

@soutaro soutaro added the area/core Issues related to the type checker core label Feb 22, 2024
@soutaro soutaro added this to the Steep 1.7 milestone Feb 22, 2024
@soutaro soutaro merged commit f7f90e5 into master Feb 22, 2024
22 checks passed
@soutaro soutaro deleted the limit-subtyping branch February 22, 2024 01:35
@soutaro soutaro added the Released The PR is already included in a published release label Jun 7, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core Issues related to the type checker core Released The PR is already included in a published release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant