Skip to content

Commit d0cf1f1

Browse files
committed
Simplify setConstantInternal()
* The return value is anyway ignored for #autoload. * Move checks in callers for redefined constants
1 parent 8473fcd commit d0cf1f1

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

src/main/java/org/truffleruby/core/module/ModuleFields.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,9 @@ private RubyConstant setConstantInternal(RubyContext context, Node currentNode,
340340
final SourceSection sourceSection = currentNode != null ? currentNode.getSourceSection() : null;
341341
final RubyConstant newValue = new RubyConstant(rubyModuleObject, value, isPrivate, autoload, isDeprecated, sourceSection);
342342

343-
if ((previous == null) ? (constants.putIfAbsent(name, newValue) == null) : constants.replace(name, previous, newValue)) {
343+
if (previous == null ? constants.putIfAbsent(name, newValue) == null : constants.replace(name, previous, newValue)) {
344344
newConstantsVersion();
345-
if (!autoload && previous != null && !previous.isAutoload()) {
346-
return previous;
347-
} else {
348-
return null;
349-
}
345+
return previous;
350346
}
351347
}
352348
}

src/main/java/org/truffleruby/core/module/ModuleNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ public Object setConstant(DynamicObject module, String name, Object value) {
997997
@TruffleBoundary
998998
public Object setConstantNoCheckName(DynamicObject module, String name, Object value) {
999999
final RubyConstant previous = Layouts.MODULE.getFields(module).setConstant(getContext(), this, name, value);
1000-
if (previous != null) {
1000+
if (previous != null && previous.hasValue()) {
10011001
warnAlreadyInitializedConstant(module, name, previous.getSourceSection());
10021002
}
10031003
return value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.oracle.truffle.api.source.SourceSection;
1717
import org.truffleruby.Layouts;
1818
import org.truffleruby.core.constant.WarnAlreadyInitializedNode;
19+
import org.truffleruby.core.module.ModuleOperations;
1920
import org.truffleruby.language.RubyConstant;
2021
import org.truffleruby.language.RubyGuards;
2122
import org.truffleruby.language.RubyNode;
@@ -47,7 +48,7 @@ public Object execute(VirtualFrame frame) {
4748
}
4849

4950
final RubyConstant previous = Layouts.MODULE.getFields((DynamicObject) moduleObject).setConstant(getContext(), this, name, value);
50-
if (previous != null) {
51+
if (previous != null && previous.hasValue()) {
5152
warnAlreadyInitializedConstant((DynamicObject) moduleObject, name, previous.getSourceSection());
5253
}
5354
return value;

0 commit comments

Comments
 (0)