Skip to content

Commit 436518f

Browse files
bjfisheregon
authored andcommitted
Implement -W warning category options
(cherry picked from commit 1042db1)
1 parent 75a4bba commit 436518f

File tree

12 files changed

+106
-17
lines changed

12 files changed

+106
-17
lines changed

spec/tags/command_line/dash_upper_w_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ slow:The -W command line option with 1 sets $VERBOSE to false
33
slow:The -W command line option with 2 sets $VERBOSE to true
44
slow:The -W command line option with :no-deprecated suppresses deprecation warnings
55
slow:The -W command line option with :no-experimental suppresses experimental warnings
6-
fails:The -W command line option with :no-deprecated suppresses deprecation warnings
76
fails:The -W command line option with :no-experimental suppresses experimental warnings
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
slow:Warning.[]= emits and suppresses warnings for :deprecated
22
slow:Warning.[]= emits and suppresses warnings for :experimental
3-
fails:Warning.[]= emits and suppresses warnings for :deprecated
43
fails:Warning.[]= emits and suppresses warnings for :experimental
54
slow:Warning.[]= :experimental emits and suppresses warnings for :experimental
65
fails:Warning.[]= :experimental emits and suppresses warnings for :experimental

spec/tags/language/predefined_tags.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ slow:The predefined global constant ARGV contains Strings encoded in locale Enco
66
slow:Global variable $0 is the path given as the main script and the same as __FILE__
77
slow:Global variable $? is thread-local
88
slow:Global variable $0 actually sets the program name
9-
fails:Predefined global $, warns if assigned non-nil
10-
fails:Predefined global $; warns if assigned non-nil
119
fails:$LOAD_PATH.resolve_feature_path returns what will be loaded without actual loading, .rb file
1210
fails:$LOAD_PATH.resolve_feature_path returns what will be loaded without actual loading, .so file
1311
fails:$LOAD_PATH.resolve_feature_path raises LoadError if feature cannot be found

src/launcher/java/org/truffleruby/launcher/CommandLineParser.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,26 +340,46 @@ private void processArgument() throws CommandLineException {
340340
break;
341341
case 'w':
342342
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.TRUE);
343+
setAllWarningCategories(true);
343344
break;
344345
case 'W': {
345346
String temp = grabOptionalValue();
346347
if (temp == null) {
347-
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.TRUE);
348+
temp = "2";
349+
}
350+
if (temp.startsWith(":")) {
351+
switch (temp) {
352+
case ":deprecated":
353+
config.setOption(OptionsCatalog.WARN_DEPRECATED, true);
354+
break;
355+
case ":no-deprecated":
356+
config.setOption(OptionsCatalog.WARN_DEPRECATED, false);
357+
break;
358+
case ":experimental":
359+
config.setOption(OptionsCatalog.WARN_EXPERIMENTAL, true);
360+
break;
361+
case ":no-experimental":
362+
config.setOption(OptionsCatalog.WARN_EXPERIMENTAL, false);
363+
break;
364+
default:
365+
LOGGER.warning("unknown warning category: `" + temp.substring(1) + "'");
366+
break;
367+
}
348368
} else {
349369
switch (temp) {
350370
case "0":
351371
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.NIL);
352-
break;
353-
case "1":
354-
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.FALSE);
372+
setAllWarningCategories(false);
355373
break;
356374
case "2":
357375
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.TRUE);
376+
setAllWarningCategories(true);
358377
break;
378+
case "1":
359379
default:
360-
throw new CommandLineException(
361-
getArgumentError(" -W must be followed by either 0, 1, 2 or nothing"),
362-
true);
380+
config.setOption(OptionsCatalog.VERBOSITY, Verbosity.FALSE);
381+
config.setOption(OptionsCatalog.WARN_DEPRECATED, false);
382+
break;
363383
}
364384
}
365385
break FOR;
@@ -466,6 +486,11 @@ private void processArgument() throws CommandLineException {
466486
}
467487
}
468488

489+
private void setAllWarningCategories(boolean value) {
490+
config.setOption(OptionsCatalog.WARN_DEPRECATED, value);
491+
config.setOption(OptionsCatalog.WARN_EXPERIMENTAL, value);
492+
}
493+
469494
private void enableDisableFeature(String name, boolean enable) {
470495
final BiConsumer<CommandLineParser, Boolean> feature = FEATURES.get(name);
471496

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
import org.truffleruby.shared.TruffleRuby;
8383
import org.truffleruby.shared.options.OptionsCatalog;
8484
import org.truffleruby.shared.options.RubyOptionTypes;
85-
import org.truffleruby.shared.options.Verbosity;
8685
import org.truffleruby.stdlib.CoverageManager;
8786
import org.truffleruby.stdlib.readline.ConsoleHolder;
8887

@@ -177,8 +176,8 @@ public RubyContext(RubyLanguage language, TruffleLanguage.Env env) {
177176

178177
options = createOptions(env, language.options);
179178

180-
warningCategoryDeprecated = new AssumedValue<>(options.VERBOSITY == Verbosity.TRUE);
181-
warningCategoryExperimental = new AssumedValue<>(options.VERBOSITY != Verbosity.NIL);
179+
warningCategoryDeprecated = new AssumedValue<>(options.WARN_DEPRECATED);
180+
warningCategoryExperimental = new AssumedValue<>(options.WARN_EXPERIMENTAL);
182181

183182
safepointManager = new SafepointManager(this, language);
184183
coreExceptions = new CoreExceptions(this, language);
@@ -294,6 +293,12 @@ protected boolean patch(Env newEnv) {
294293
return false;
295294
}
296295
this.options = newOptions;
296+
if (newOptions.WARN_DEPRECATED != oldOptions.WARN_DEPRECATED) {
297+
warningCategoryDeprecated.set(newOptions.WARN_DEPRECATED);
298+
}
299+
if (newOptions.WARN_EXPERIMENTAL != oldOptions.WARN_EXPERIMENTAL) {
300+
warningCategoryExperimental.set(newOptions.WARN_EXPERIMENTAL);
301+
}
297302
this.rubyHome = newHome;
298303
this.rubyHomeTruffleFile = newHome == null ? null : newEnv.getInternalTruffleFile(newHome);
299304

src/main/java/org/truffleruby/options/Options.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public class Options {
121121
public final boolean CEXTS_LOG_LOAD;
122122
/** --cexts-log-warnings=false */
123123
public final boolean CEXTS_LOG_WARNINGS;
124+
/** --warn-deprecated=false */
125+
public final boolean WARN_DEPRECATED;
126+
/** --warn-experimental=true */
127+
public final boolean WARN_EXPERIMENTAL;
124128
/** --argv-globals=false */
125129
public final boolean ARGV_GLOBALS;
126130
/** --chomp-loop=false */
@@ -300,6 +304,8 @@ public Options(Env env, OptionValues options, LanguageOptions languageOptions) {
300304
METRICS_PROFILE_REQUIRE = options.get(OptionsCatalog.METRICS_PROFILE_REQUIRE_KEY);
301305
CEXTS_LOG_LOAD = options.get(OptionsCatalog.CEXTS_LOG_LOAD_KEY);
302306
CEXTS_LOG_WARNINGS = options.get(OptionsCatalog.CEXTS_LOG_WARNINGS_KEY);
307+
WARN_DEPRECATED = options.get(OptionsCatalog.WARN_DEPRECATED_KEY);
308+
WARN_EXPERIMENTAL = options.get(OptionsCatalog.WARN_EXPERIMENTAL_KEY);
303309
ARGV_GLOBALS = options.get(OptionsCatalog.ARGV_GLOBALS_KEY);
304310
CHOMP_LOOP = options.get(OptionsCatalog.CHOMP_LOOP_KEY);
305311
GETS_LOOP = options.get(OptionsCatalog.GETS_LOOP_KEY);
@@ -466,6 +472,10 @@ public Object fromDescriptor(OptionDescriptor descriptor) {
466472
return CEXTS_LOG_LOAD;
467473
case "ruby.cexts-log-warnings":
468474
return CEXTS_LOG_WARNINGS;
475+
case "ruby.warn-deprecated":
476+
return WARN_DEPRECATED;
477+
case "ruby.warn-experimental":
478+
return WARN_EXPERIMENTAL;
469479
case "ruby.argv-globals":
470480
return ARGV_GLOBALS;
471481
case "ruby.chomp-loop":

src/main/ruby/truffleruby/core/truffle/kernel_operations.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,25 @@ def self.define_read_only_global(name, getter)
7878
if v && !Primitive.object_kind_of?(v, String)
7979
raise TypeError, '$, must be a String'
8080
end
81+
warn "`$,' is deprecated", uplevel: 1 if !Primitive.nil?(v) && Warning[:deprecated]
8182
Primitive.global_variable_set :$,, v
8283
})
8384

85+
define_read_only_global(:'$-W',
86+
-> {
87+
case $VERBOSE
88+
when nil
89+
0
90+
when false
91+
1
92+
when true
93+
2
94+
else
95+
nil
96+
end
97+
})
98+
99+
84100
$, = nil # It should be defined by the time boot has finished.
85101

86102
$= = false
@@ -127,6 +143,14 @@ def self.define_read_only_global(name, getter)
127143
Primitive.global_variable_set :$stderr, v
128144
})
129145

146+
define_hooked_variable(
147+
:"$;",
148+
-> { Primitive.global_variable_get :"$;" },
149+
-> v {
150+
warn "`$;' is deprecated", uplevel: 1 if !Primitive.nil?(v) && Warning[:deprecated]
151+
Primitive.global_variable_set :"$;", v
152+
})
153+
130154
def self.load_error(name)
131155
load_error = LoadError.new("cannot load such file -- #{name}")
132156
load_error.path = name

src/options.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ EXPERT:
9191
CEXTS_LOG_LOAD: [cexts-log-load, boolean, false, Log loading of cexts]
9292
CEXTS_LOG_WARNINGS: [cexts-log-warnings, boolean, false, Log cexts warnings]
9393

94+
WARN_DEPRECATED: [[warn-deprecated, -W], boolean, false, 'Sets deprecated Warning category']
95+
WARN_EXPERIMENTAL: [[warn-experimental, -W], boolean, true, 'Sets experimental Warning category']
96+
9497
INTERNAL: # Options for debugging the TruffleRuby implementation
9598
EXPERIMENTAL:
9699
# Options corresponding to MRI options, which are only meaningful when using the TruffleRuby launcher

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public class OptionsCatalog {
7373
public static final OptionKey<Profile> METRICS_PROFILE_REQUIRE_KEY = new OptionKey<>(Profile.NONE, EnumOptionType.optionTypeFor(Profile.class));
7474
public static final OptionKey<Boolean> CEXTS_LOG_LOAD_KEY = new OptionKey<>(false);
7575
public static final OptionKey<Boolean> CEXTS_LOG_WARNINGS_KEY = new OptionKey<>(false);
76+
public static final OptionKey<Boolean> WARN_DEPRECATED_KEY = new OptionKey<>(false);
77+
public static final OptionKey<Boolean> WARN_EXPERIMENTAL_KEY = new OptionKey<>(true);
7678
public static final OptionKey<Boolean> ARGV_GLOBALS_KEY = new OptionKey<>(false);
7779
public static final OptionKey<Boolean> CHOMP_LOOP_KEY = new OptionKey<>(false);
7880
public static final OptionKey<Boolean> GETS_LOOP_KEY = new OptionKey<>(false);
@@ -519,6 +521,20 @@ public class OptionsCatalog {
519521
.stability(OptionStability.EXPERIMENTAL)
520522
.build();
521523

524+
public static final OptionDescriptor WARN_DEPRECATED = OptionDescriptor
525+
.newBuilder(WARN_DEPRECATED_KEY, "ruby.warn-deprecated")
526+
.help("Sets deprecated Warning category (configured by the -W Ruby option)")
527+
.category(OptionCategory.EXPERT)
528+
.stability(OptionStability.EXPERIMENTAL)
529+
.build();
530+
531+
public static final OptionDescriptor WARN_EXPERIMENTAL = OptionDescriptor
532+
.newBuilder(WARN_EXPERIMENTAL_KEY, "ruby.warn-experimental")
533+
.help("Sets experimental Warning category (configured by the -W Ruby option)")
534+
.category(OptionCategory.EXPERT)
535+
.stability(OptionStability.EXPERIMENTAL)
536+
.build();
537+
522538
public static final OptionDescriptor ARGV_GLOBALS = OptionDescriptor
523539
.newBuilder(ARGV_GLOBALS_KEY, "ruby.argv-globals")
524540
.help("Parse options in script argv into global variables (configured by the -s Ruby option)")
@@ -1145,6 +1161,10 @@ public static OptionDescriptor fromName(String name) {
11451161
return CEXTS_LOG_LOAD;
11461162
case "ruby.cexts-log-warnings":
11471163
return CEXTS_LOG_WARNINGS;
1164+
case "ruby.warn-deprecated":
1165+
return WARN_DEPRECATED;
1166+
case "ruby.warn-experimental":
1167+
return WARN_EXPERIMENTAL;
11481168
case "ruby.argv-globals":
11491169
return ARGV_GLOBALS;
11501170
case "ruby.chomp-loop":
@@ -1353,6 +1373,8 @@ public static OptionDescriptor[] allDescriptors() {
13531373
METRICS_PROFILE_REQUIRE,
13541374
CEXTS_LOG_LOAD,
13551375
CEXTS_LOG_WARNINGS,
1376+
WARN_DEPRECATED,
1377+
WARN_EXPERIMENTAL,
13561378
ARGV_GLOBALS,
13571379
CHOMP_LOOP,
13581380
GETS_LOOP,

test/mri/excludes/TestRubyOptions.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@
3434
exclude :test_usage_long, "needs investigation"
3535
exclude :test_verbose, "needs investigation"
3636
exclude :test_version, "needs investigation"
37-
exclude :test_warning, "needs investigation"
3837
exclude :test_yydebug, "needs investigation"
3938
exclude :test_segv_loaded_features, "needs investigation"
4039
exclude :test_segv_setproctitle, "needs investigation"
4140
exclude :test_segv_test, "needs investigation"
41+
exclude :test_assignment_in_conditional, "needs investigation"
42+
exclude :test_cwd_encoding, "needs investigation"
43+
exclude :test_disable, "needs investigation"
44+
exclude :test_flag_in_shebang, "needs investigation"
45+
exclude :test_frozen_string_literal, "needs investigation"
46+
exclude :test_jit_debug, "needs investigation"

0 commit comments

Comments
 (0)