Skip to content

Commit 5fd2b6b

Browse files
committed
[GR-20446] Capturing proc warning for deprecated
PullRequest: truffleruby/2188
2 parents 87c7c43 + cfcbb6d commit 5fd2b6b

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

src/main/java/org/truffleruby/RubyContext.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.oracle.truffle.api.RootCallTarget;
2727
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
2828
import com.oracle.truffle.api.nodes.Node;
29+
import com.oracle.truffle.api.utilities.AssumedValue;
2930
import org.graalvm.collections.Pair;
3031
import org.graalvm.options.OptionDescriptor;
3132
import org.joni.Regex;
@@ -81,6 +82,7 @@
8182
import org.truffleruby.shared.TruffleRuby;
8283
import org.truffleruby.shared.options.OptionsCatalog;
8384
import org.truffleruby.shared.options.RubyOptionTypes;
85+
import org.truffleruby.shared.options.Verbosity;
8486
import org.truffleruby.stdlib.CoverageManager;
8587
import org.truffleruby.stdlib.readline.ConsoleHolder;
8688

@@ -150,6 +152,9 @@ public class RubyContext {
150152
private boolean initialized;
151153
private volatile boolean finalizing;
152154

155+
private final AssumedValue<Boolean> warningCategoryDeprecated;
156+
private final AssumedValue<Boolean> warningCategoryExperimental;
157+
153158
private static boolean preInitializeContexts = TruffleRuby.PRE_INITIALIZE_CONTEXTS;
154159

155160
private static boolean isPreInitializingContext() {
@@ -172,6 +177,9 @@ public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
172177

173178
options = createOptions(env, language.options);
174179

180+
warningCategoryDeprecated = new AssumedValue<>(options.VERBOSITY == Verbosity.TRUE);
181+
warningCategoryExperimental = new AssumedValue<>(options.VERBOSITY != Verbosity.NIL);
182+
175183
safepointManager = new SafepointManager(this, language);
176184
coreExceptions = new CoreExceptions(this, language);
177185
encodingManager = new EncodingManager(this, language);
@@ -814,6 +822,14 @@ public Object getTopScopeObject() {
814822
return coreLibrary.topScopeObject;
815823
}
816824

825+
public AssumedValue<Boolean> getWarningCategoryDeprecated() {
826+
return warningCategoryDeprecated;
827+
}
828+
829+
public AssumedValue<Boolean> getWarningCategoryExperimental() {
830+
return warningCategoryExperimental;
831+
}
832+
817833
@TruffleBoundary
818834
public static String fileLine(SourceSection section) {
819835
if (section == null) {

src/main/java/org/truffleruby/core/kernel/KernelNodes.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.TimeUnit;
2222

2323
import com.oracle.truffle.api.dsl.CachedContext;
24+
import com.oracle.truffle.api.utilities.AssumedValue;
2425
import org.jcodings.specific.UTF8Encoding;
2526
import org.truffleruby.RubyContext;
2627
import org.truffleruby.RubyLanguage;
@@ -2104,4 +2105,42 @@ protected Object untrust(Object object,
21042105

21052106
}
21062107

2108+
@Primitive(name = "warning_get_category")
2109+
public abstract static class WarningGetCategoryNode extends PrimitiveArrayArgumentsNode {
2110+
2111+
@Specialization(guards = "category == coreSymbols().DEPRECATED")
2112+
protected boolean getCategoryDeprecated(RubySymbol category) {
2113+
return getContext().getWarningCategoryDeprecated().get();
2114+
}
2115+
2116+
@Specialization(guards = "category == coreSymbols().EXPERIMENTAL")
2117+
protected boolean getCategoryExperimental(RubySymbol category) {
2118+
return getContext().getWarningCategoryExperimental().get();
2119+
}
2120+
2121+
}
2122+
2123+
@Primitive(name = "warning_set_category")
2124+
public abstract static class WarningSetCategoryNode extends PrimitiveArrayArgumentsNode {
2125+
2126+
@TruffleBoundary
2127+
@Specialization
2128+
protected boolean setCategory(RubySymbol category, boolean newValue) {
2129+
final AssumedValue<Boolean> existingValue;
2130+
if (category == coreSymbols().DEPRECATED) {
2131+
existingValue = getContext().getWarningCategoryDeprecated();
2132+
} else if (category == coreSymbols().EXPERIMENTAL) {
2133+
existingValue = getContext().getWarningCategoryExperimental();
2134+
} else {
2135+
throw CompilerDirectives.shouldNotReachHere("unexpected warning category");
2136+
}
2137+
2138+
if (existingValue.get() != newValue) {
2139+
existingValue.set(newValue);
2140+
}
2141+
return newValue;
2142+
}
2143+
2144+
}
2145+
21072146
}

src/main/java/org/truffleruby/core/proc/ProcNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected RubyProc proc(VirtualFrame frame, RubyClass procClass, Object[] args,
8282
if (parentBlock == nil) {
8383
throw new RaiseException(getContext(), coreExceptions().argumentErrorProcWithoutBlock(this));
8484
} else {
85-
if (warnNode.shouldWarn()) {
85+
if (warnNode.shouldWarnForDeprecation()) {
8686
warnNode.warningMessage(
8787
getContext().getCallStack().getTopMostUserSourceSection(),
8888
"Capturing the given block using Kernel#proc is deprecated; use `&block` instead");

src/main/java/org/truffleruby/core/symbol/CoreSymbols.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class CoreSymbols {
3636
public final RubySymbol LINE = createRubySymbol("line");
3737
public final RubySymbol NEVER = createRubySymbol("never");
3838
public final RubySymbol ON_BLOCKING = createRubySymbol("on_blocking");
39+
public final RubySymbol DEPRECATED = createRubySymbol("deprecated");
40+
public final RubySymbol EXPERIMENTAL = createRubySymbol("experimental");
3941

4042
public static final int FIRST_OP_ID = 33;
4143

src/main/java/org/truffleruby/language/WarnNode.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ public boolean shouldWarn() {
3636
return verbosity != nil;
3737
}
3838

39-
/** Must only be called if {@link #shouldWarn()} is true, in order to avoid computing a SourceSection and message if
40-
* not needed. */
39+
public boolean shouldWarnForDeprecation() {
40+
final Object verbosity = readVerboseNode.execute();
41+
return verbosity != nil && getContext().getWarningCategoryDeprecated().get();
42+
}
43+
44+
/** Must only be called if {@link #shouldWarn()} or {@link #shouldWarnForDeprecation()} is true, in order to avoid
45+
* computing a SourceSection and message if not needed. */
4146
public void warningMessage(SourceSection sourceSection, String message) {
4247
assert shouldWarn();
4348
callWarn(sourceSection, message);

src/main/ruby/truffleruby/core/warning.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ def warn(message)
2222
nil
2323
end
2424

25-
@deprecated = false
26-
@experimental = true
27-
2825
def self.[](category)
2926
case category
3027
when :deprecated
31-
@deprecated
28+
Primitive.warning_get_category(:deprecated)
3229
when :experimental
33-
@experimental
30+
Primitive.warning_get_category(:experimental)
3431
else
3532
raise ArgumentError, "unknown category: #{category}"
3633
end
@@ -39,9 +36,9 @@ def self.[](category)
3936
def self.[]=(category, value)
4037
case category
4138
when :deprecated
42-
@deprecated = !!value
39+
Primitive.warning_set_category(:deprecated, !!value)
4340
when :experimental
44-
@experimental = !!value
41+
Primitive.warning_set_category(:experimental, !!value)
4542
else
4643
raise ArgumentError, "unknown category: #{category}"
4744
end

tool/generate-core-symbols.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
public final RubySymbol LINE = createRubySymbol("line");
5454
public final RubySymbol NEVER = createRubySymbol("never");
5555
public final RubySymbol ON_BLOCKING = createRubySymbol("on_blocking");
56+
public final RubySymbol DEPRECATED = createRubySymbol("deprecated");
57+
public final RubySymbol EXPERIMENTAL = createRubySymbol("experimental");
5658
5759
JAVA
5860
lines = []

0 commit comments

Comments
 (0)