Skip to content

Commit cfcbb6d

Browse files
committed
Capturing proc warning should be verbose mode
1 parent c66b62a commit cfcbb6d

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;
@@ -2065,4 +2066,42 @@ protected Object untaint(Object object,
20652066

20662067
}
20672068

2069+
@Primitive(name = "warning_get_category")
2070+
public abstract static class WarningGetCategoryNode extends PrimitiveArrayArgumentsNode {
2071+
2072+
@Specialization(guards = "category == coreSymbols().DEPRECATED")
2073+
protected boolean getCategoryDeprecated(RubySymbol category) {
2074+
return getContext().getWarningCategoryDeprecated().get();
2075+
}
2076+
2077+
@Specialization(guards = "category == coreSymbols().EXPERIMENTAL")
2078+
protected boolean getCategoryExperimental(RubySymbol category) {
2079+
return getContext().getWarningCategoryExperimental().get();
2080+
}
2081+
2082+
}
2083+
2084+
@Primitive(name = "warning_set_category")
2085+
public abstract static class WarningSetCategoryNode extends PrimitiveArrayArgumentsNode {
2086+
2087+
@TruffleBoundary
2088+
@Specialization
2089+
protected boolean setCategory(RubySymbol category, boolean newValue) {
2090+
final AssumedValue<Boolean> existingValue;
2091+
if (category == coreSymbols().DEPRECATED) {
2092+
existingValue = getContext().getWarningCategoryDeprecated();
2093+
} else if (category == coreSymbols().EXPERIMENTAL) {
2094+
existingValue = getContext().getWarningCategoryExperimental();
2095+
} else {
2096+
throw CompilerDirectives.shouldNotReachHere("unexpected warning category");
2097+
}
2098+
2099+
if (existingValue.get() != newValue) {
2100+
existingValue.set(newValue);
2101+
}
2102+
return newValue;
2103+
}
2104+
2105+
}
2106+
20682107
}

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)