Enforce protected beyond top-level namespace#17115
Open
jgaskins wants to merge 1 commit into
Open
Conversation
Consider this code:
module A
module Validations
end
struct Query
protected def this_method_is_protected
"this_method_is_protected!!!"
end
end
end
struct B
include A::Validations
def call
puts A::Query.new.this_method_is_protected
end
end
B.new.call
`B#call` calls a protected method on `A::Query`, but it has no
relationship with `A::Query`. It does include `A::Validations`, but
that should not give B the same access to `A::Query`'s protected methods
that `A::Validations` has.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16827
Notably, there were a couple places in the spec suite that called protected methods on
Spec::ExampleGroup, which worked because the top level includesSpec::Methods. That effectively makes every protected method in every type nested underSpecpublic, which is a symptom of the problem we're fixing here.My workaround for that here was to wrap those calls in a
Spectype specifically for this spec suite (not intended to be used by regular Crystal apps/shards), which highlights that being able to callprotectedmethods from within the namespace is preserved. I'm not a huge fan of the monkeypatch, but the alternative was to makeSpec::ExampleGroup#reportpublic. Notably,Spec::RootContext#reportis already public butSpec::ExampleGroup#reportis protected, so it seems feasible to make it public at least to bring that method into parity between the twoSpec::Contextconcrete types, but my initial implementation preserves the existing visibility in the stdlib until we discuss whether it should change.