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

Incompatibility of constant name resolution in ruby and rbs #729

Closed
ksss opened this issue Aug 4, 2021 · 2 comments
Closed

Incompatibility of constant name resolution in ruby and rbs #729

ksss opened this issue Aug 4, 2021 · 2 comments

Comments

@ksss
Copy link
Collaborator

ksss commented Aug 4, 2021

$ cat sample.rb
class A
  module B
  end
end

class C < A
  include B # A::B
end

$ ruby sample.rb

$ rbs prototype rb sample.rb > sample.rbs

$ cat sample.rbs
class A
  module B
  end
end

class C < A
  include B
end

$ rbs -I . validate
...(snip)...
Validating class/module definition: `::A`...
Validating class/module definition: `::A::B`...
Validating class/module definition: `::C`...
/Users/ksss/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/rbs-1.3.3/lib/rbs/errors.rb:178:in `check!': t.rbs:7:2...7:11: Could not find mixin: B (RBS::NoMixinFoundError)

Is this intentional?

@pocke
Copy link
Member

pocke commented Aug 5, 2021

Yes, the constant name resolution is intentional. see #307

Type name resolution became more compatible with Ruby's constant resolution. It resolves given relative type names based on class/module nesting structure. Note that the type name resolution does not refer inheritance and includes.

And rbs prototype rb can't generate the expected RBS, such as include A::B, due to the design. It just parses Ruby code but doesn't evaluate, so rbs prototype rb doesn't know the module name's ancestors.

By the way, we can improve two points about the issue.

First, we can write a documentation about module resolution. Probably the docs don't mention this incompatibility. A doc should mention it.

Second, rbs prototype runtime has the same problem but it could be fixed unlike prototype rb. prototype runtime evaluates Ruby code, so it can get the module full name.

$ rbs prototype runtime -R sample.rb A 'A::*' 'C' 'C::*'
class A
  module B
  end
end

class C < A
  include B  # ← It should be `include A::B`, but isn't.
end

@ksss
Copy link
Collaborator Author

ksss commented Aug 5, 2021

Thank you for your kind answer.
I understood the incompatibility of name resolution in ruby and rbs.

@ksss ksss closed this as completed Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants