From 7e8fa06022a9e412e3f8e6c8b6f0ba1909f648d5 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Tue, 20 Dec 2022 12:44:11 -0800 Subject: Always issue deprecation warning when calling Regexp.new with 3rd positional argument Previously, only certain values of the 3rd argument triggered a deprecation warning. First step for fix for bug #18797. Support for the 3rd argument will be removed after the release of Ruby 3.2. Fix minor fallout discovered by the tests. Co-authored-by: Nobuyoshi Nakada --- re.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 're.c') diff --git a/re.c b/re.c index 837ca87835..7a74318558 100644 --- a/re.c +++ b/re.c @@ -3759,10 +3759,11 @@ struct reg_init_args { static VALUE reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args); static VALUE reg_init_args(VALUE self, VALUE str, rb_encoding *enc, int flags); +void rb_warn_deprecated_to_remove(const char *removal, const char *fmt, const char *suggest, ...); /* * call-seq: - * Regexp.new(string, options = 0, n_flag = nil, timeout: nil) -> regexp + * Regexp.new(string, options = 0, timeout: nil) -> regexp * Regexp.new(regexp, timeout: nil) -> regexp * * With argument +string+ given, returns a new regexp with the given string @@ -3780,24 +3781,18 @@ static VALUE reg_init_args(VALUE self, VALUE str, rb_encoding *enc, int flags); * Regexp.new('foo', 'im') # => /foo/im * * - The logical OR of one or more of the constants - * Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::MULTILINE: + * Regexp::EXTENDED, Regexp::IGNORECASE, Regexp::MULTILINE, and + * Regexp::NOENCODING: * * Regexp.new('foo', Regexp::IGNORECASE) # => /foo/i * Regexp.new('foo', Regexp::EXTENDED) # => /foo/x * Regexp.new('foo', Regexp::MULTILINE) # => /foo/m + * Regexp.new('foo', Regexp::NOENCODING) # => /foo/n * flags = Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE * Regexp.new('foo', flags) # => /foo/mix * * - +nil+ or +false+, which is ignored. * - * If optional argument +n_flag+ if it is a string starts with - * 'n' or 'N', the encoding of +string+ is - * ignored and the new regexp encoding is fixed to +ASCII-8BIT+ or - * +US-ASCII+, by its content. - * - * Regexp.new('foo', nil, 'n') # => /foo/n - * Regexp.new("\u3042", nil, 'n') # => /\xE3\x81\x82/n - * * If optional keyword argument +timeout+ is given, * its float value overrides the timeout interval for the class, * Regexp.timeout. @@ -3841,7 +3836,7 @@ reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args) VALUE str, src, opts = Qundef, n_flag = Qundef, kwargs; VALUE re = Qnil; - rb_scan_args(argc, argv, "12:", &src, &opts, &n_flag, &kwargs); + argc = rb_scan_args(argc, argv, "12:", &src, &opts, &n_flag, &kwargs); args->timeout = Qnil; if (!NIL_P(kwargs)) { @@ -3852,6 +3847,10 @@ reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args) rb_get_kwargs(kwargs, keywords, 0, 1, &args->timeout); } + if (argc == 3) { + rb_warn_deprecated_to_remove("3.3", "3rd argument to Regexp.new", "2nd argument"); + } + if (RB_TYPE_P(src, T_REGEXP)) { re = src; @@ -3876,9 +3875,6 @@ reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args) enc = rb_ascii8bit_encoding(); flags |= ARG_ENCODING_NONE; } - else { - rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "encoding option is ignored - %s", kcode); - } } str = StringValue(src); } -- cgit v1.2.1