Skip to content

Commit 8f46982

Browse files
committed
Remove visibility check when looking up constant with lexical scope
1 parent d6566f7 commit 8f46982

File tree

4 files changed

+35
-36
lines changed

4 files changed

+35
-36
lines changed

spec/ruby/language/constants_spec.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,24 @@ class PrivateClass
551551
end
552552

553553
it "can be accessed from classes that include the module" do
554-
ConstantVisibility::PrivConstModuleChild.new.private_constant_from_include.should be_true
554+
ConstantVisibility::ClassIncludingPrivConstModule.new.private_constant_from_include.should be_true
555+
end
556+
557+
it "can be accessed from modules that include the module" do
558+
ConstantVisibility::ModuleIncludingPrivConstModule.private_constant_from_include.should be_true
559+
end
560+
561+
it "raises a NameError when accessed directly from modules that include the module" do
562+
-> do
563+
ConstantVisibility::ModuleIncludingPrivConstModule.private_constant_self_from_include
564+
end.should raise_error(NameError)
565+
-> do
566+
ConstantVisibility::ModuleIncludingPrivConstModule.private_constant_named_from_include
567+
end.should raise_error(NameError)
555568
end
556569

557570
it "is defined? from classes that include the module" do
558-
ConstantVisibility::PrivConstModuleChild.new.defined_from_include.should == "constant"
571+
ConstantVisibility::ClassIncludingPrivConstModule.new.defined_from_include.should == "constant"
559572
end
560573
end
561574

@@ -673,7 +686,7 @@ class PrivateClass
673686
}
674687

675688
-> do
676-
ConstantVisibility::PrivConstModuleChild::PRIVATE_CONSTANT_MODULE
689+
ConstantVisibility::ClassIncludingPrivConstModule::PRIVATE_CONSTANT_MODULE
677690
end.should raise_error(NameError) {|e|
678691
e.receiver.should == ConstantVisibility::PrivConstModule
679692
e.name.should == :PRIVATE_CONSTANT_MODULE

spec/ruby/language/fixtures/constant_visibility.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def self.defined_from_scope
6565
end
6666
end
6767

68-
class PrivConstModuleChild
68+
class ClassIncludingPrivConstModule
6969
include PrivConstModule
7070

7171
def private_constant_from_include
@@ -77,6 +77,22 @@ def defined_from_include
7777
end
7878
end
7979

80+
module ModuleIncludingPrivConstModule
81+
include PrivConstModule
82+
83+
def self.private_constant_from_include
84+
PRIVATE_CONSTANT_MODULE
85+
end
86+
87+
def self.private_constant_self_from_include
88+
self::PRIVATE_CONSTANT_MODULE
89+
end
90+
91+
def self.private_constant_named_from_include
92+
PrivConstModule::PRIVATE_CONSTANT_MODULE
93+
end
94+
end
95+
8096
class PrivConstClassChild < PrivConstClass
8197
def private_constant_from_subclass
8298
PRIVATE_CONSTANT_CLASS

src/main/java/org/truffleruby/language/constants/LookupConstantWithDynamicScopeNode.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.truffleruby.core.module.RubyModule;
1616
import org.truffleruby.language.LexicalScope;
1717
import org.truffleruby.language.RubyConstant;
18-
import org.truffleruby.language.control.RaiseException;
1918

2019
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2120
import com.oracle.truffle.api.dsl.Cached;
@@ -45,13 +44,7 @@ public RubyConstant lookupConstant(LexicalScope lexicalScope, RubyModule module,
4544
limit = "getCacheLimit()")
4645
protected RubyConstant lookupConstant(LexicalScope lexicalScope,
4746
@Cached("lexicalScope") LexicalScope cachedLexicalScope,
48-
@Cached("doLookup(cachedLexicalScope)") ConstantLookupResult constant,
49-
@Cached("isVisible(cachedLexicalScope, constant)") boolean isVisible) {
50-
if (!isVisible) {
51-
throw new RaiseException(
52-
getContext(),
53-
coreExceptions().nameErrorPrivateConstant(constant.getConstant().getDeclaringModule(), name, this));
54-
}
47+
@Cached("doLookup(cachedLexicalScope)") ConstantLookupResult constant) {
5548
if (constant.isDeprecated()) {
5649
warnDeprecatedConstant(constant.getConstant().getDeclaringModule(), constant.getConstant(), name);
5750
}
@@ -60,14 +53,8 @@ protected RubyConstant lookupConstant(LexicalScope lexicalScope,
6053

6154
@Specialization
6255
protected RubyConstant lookupConstantUncached(LexicalScope lexicalScope,
63-
@Cached ConditionProfile isVisibleProfile,
6456
@Cached ConditionProfile isDeprecatedProfile) {
6557
final ConstantLookupResult constant = doLookup(lexicalScope);
66-
if (isVisibleProfile.profile(!isVisible(lexicalScope, constant))) {
67-
throw new RaiseException(
68-
getContext(),
69-
coreExceptions().nameErrorPrivateConstant(constant.getConstant().getDeclaringModule(), name, this));
70-
}
7158
if (isDeprecatedProfile.profile(constant.isDeprecated())) {
7259
warnDeprecatedConstant(constant.getConstant().getDeclaringModule(), constant.getConstant(), name);
7360
}
@@ -79,8 +66,4 @@ protected ConstantLookupResult doLookup(LexicalScope lexicalScope) {
7966
return ModuleOperations.lookupConstantWithLexicalScope(getContext(), lexicalScope, name);
8067
}
8168

82-
protected boolean isVisible(LexicalScope lexicalScope, ConstantLookupResult constant) {
83-
return constant.isVisibleTo(getContext(), lexicalScope, lexicalScope.getLiveModule());
84-
}
85-
8669
}

src/main/java/org/truffleruby/language/constants/LookupConstantWithLexicalScopeNode.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.truffleruby.core.module.RubyModule;
1616
import org.truffleruby.language.LexicalScope;
1717
import org.truffleruby.language.RubyConstant;
18-
import org.truffleruby.language.control.RaiseException;
1918

2019
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
2120
import com.oracle.truffle.api.dsl.Cached;
@@ -48,11 +47,7 @@ public RubyConstant lookupConstant(LexicalScope lexicalScope, RubyModule module,
4847

4948
@Specialization(assumptions = "constant.getAssumptions()")
5049
protected RubyConstant lookupConstant(
51-
@Cached("doLookup()") ConstantLookupResult constant,
52-
@Cached("isVisible(constant)") boolean isVisible) {
53-
if (!isVisible) {
54-
throw new RaiseException(getContext(), coreExceptions().nameErrorPrivateConstant(getModule(), name, this));
55-
}
50+
@Cached("doLookup()") ConstantLookupResult constant) {
5651
if (constant.isDeprecated()) {
5752
warnDeprecatedConstant(getModule(), constant.getConstant(), name);
5853
}
@@ -61,12 +56,8 @@ protected RubyConstant lookupConstant(
6156

6257
@Specialization
6358
protected RubyConstant lookupConstantUncached(
64-
@Cached ConditionProfile isVisibleProfile,
6559
@Cached ConditionProfile isDeprecatedProfile) {
6660
final ConstantLookupResult constant = doLookup();
67-
if (isVisibleProfile.profile(!isVisible(constant))) {
68-
throw new RaiseException(getContext(), coreExceptions().nameErrorPrivateConstant(getModule(), name, this));
69-
}
7061
if (isDeprecatedProfile.profile(constant.isDeprecated())) {
7162
warnDeprecatedConstant(getModule(), constant.getConstant(), name);
7263
}
@@ -78,8 +69,4 @@ protected ConstantLookupResult doLookup() {
7869
return ModuleOperations.lookupConstantWithLexicalScope(getContext(), lexicalScope, name);
7970
}
8071

81-
protected boolean isVisible(ConstantLookupResult constant) {
82-
return constant.isVisibleTo(getContext(), lexicalScope, getModule());
83-
}
84-
8572
}

0 commit comments

Comments
 (0)