From 2173371b4485db90a750d4a47752b2dab9e067a1 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Wed, 8 Nov 2023 06:43:54 +0800 Subject: [PATCH] Deprecate the splat operators in macro expressions (#13939) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johannes Müller --- src/compiler/crystal/interpreter/compiler.cr | 6 +----- src/compiler/crystal/macros/interpreter.cr | 2 ++ src/macros.cr | 20 ++++++++++---------- src/object.cr | 6 +++--- src/regex.cr | 2 +- src/slice.cr | 2 +- src/static_array.cr | 2 +- src/syscall/aarch64-linux.cr | 2 +- src/syscall/arm-linux.cr | 2 +- src/syscall/i386-linux.cr | 2 +- src/syscall/x86_64-linux.cr | 2 +- src/xml/builder.cr | 2 +- src/yaml/builder.cr | 2 +- 13 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/compiler/crystal/interpreter/compiler.cr b/src/compiler/crystal/interpreter/compiler.cr index eddc87d65132..1faf5db32bb6 100644 --- a/src/compiler/crystal/interpreter/compiler.cr +++ b/src/compiler/crystal/interpreter/compiler.cr @@ -3145,11 +3145,7 @@ class Crystal::Repl::Compiler < Crystal::Visitor {% operands = instruction[:operands] || [] of Nil %} def {{name.id}}( - {% if operands.empty? %} - *, node : ASTNode? - {% else %} - {{*operands}}, *, node : ASTNode? - {% end %} + {{operands.splat(", ")}}*, node : ASTNode? ) : Nil node = @node_override || node @instructions.nodes[instructions_index] = node if node diff --git a/src/compiler/crystal/macros/interpreter.cr b/src/compiler/crystal/macros/interpreter.cr index ac07bc17bb9e..d9f5473ec9f2 100644 --- a/src/compiler/crystal/macros/interpreter.cr +++ b/src/compiler/crystal/macros/interpreter.cr @@ -511,12 +511,14 @@ module Crystal end def visit(node : Splat) + warnings.add_warning(node, "Deprecated use of splat operator. Use `#splat` instead") node.exp.accept self @last = @last.interpret("splat", [] of ASTNode, nil, nil, self, node.location) false end def visit(node : DoubleSplat) + warnings.add_warning(node, "Deprecated use of double splat operator. Use `#double_splat` instead") node.exp.accept self @last = @last.interpret("double_splat", [] of ASTNode, nil, nil, self, node.location) false diff --git a/src/macros.cr b/src/macros.cr index e8163cbec046..c01a5d35e391 100644 --- a/src/macros.cr +++ b/src/macros.cr @@ -82,16 +82,16 @@ macro record(__name name, *properties, **kwargs) {% end %} def initialize({{ - *properties.map do |field| + properties.map do |field| "@#{field.id}".id - end + end.splat }}) end {{yield}} def copy_with({{ - *properties.map do |property| + properties.map do |property| if property.is_a?(Assign) "#{property.target.id} _#{property.target.id} = @#{property.target.id}".id elsif property.is_a?(TypeDeclaration) @@ -99,10 +99,10 @@ macro record(__name name, *properties, **kwargs) else "#{property.id} _#{property.id} = @#{property.id}".id end - end + end.splat }}) self.class.new({{ - *properties.map do |property| + properties.map do |property| if property.is_a?(Assign) "_#{property.target.id}".id elsif property.is_a?(TypeDeclaration) @@ -110,13 +110,13 @@ macro record(__name name, *properties, **kwargs) else "_#{property.id}".id end - end + end.splat }}) end def clone self.class.new({{ - *properties.map do |property| + properties.map do |property| if property.is_a?(Assign) "@#{property.target.id}.clone".id elsif property.is_a?(TypeDeclaration) @@ -124,7 +124,7 @@ macro record(__name name, *properties, **kwargs) else "@#{property.id}.clone".id end - end + end.splat }}) end end @@ -150,7 +150,7 @@ macro pp!(*exps) ::print %prefix ::pp({{exp}}) {% else %} - %names = { {{*exps.map(&.stringify)}} } + %names = { {{exps.map(&.stringify).splat}} } %max_size = %names.max_of &.size { {% for exp, i in exps %} @@ -184,7 +184,7 @@ macro p!(*exps) ::print %prefix ::p({{exp}}) {% else %} - %names = { {{*exps.map(&.stringify)}} } + %names = { {{exps.map(&.stringify).splat}} } %max_size = %names.max_of &.size { {% for exp, i in exps %} diff --git a/src/object.cr b/src/object.cr index b3b06872a396..6687e19f424a 100644 --- a/src/object.cr +++ b/src/object.cr @@ -1086,7 +1086,7 @@ class Object # end # ``` macro {{macro_prefix}}property!(*names) - {{macro_prefix}}getter! \{{*names}} + {{macro_prefix}}getter! \{{names.splat}} \{% for name in names %} \{% if name.is_a?(TypeDeclaration) %} @@ -1377,8 +1377,8 @@ class Object # end # ``` macro def_equals_and_hash(*fields) - def_equals {{*fields}} - def_hash {{*fields}} + def_equals {{fields.splat}} + def_hash {{fields.splat}} end # Forwards missing methods to *delegate*. diff --git a/src/regex.cr b/src/regex.cr index 68d25f5d6be8..b1532decee64 100644 --- a/src/regex.cr +++ b/src/regex.cr @@ -403,7 +403,7 @@ class Regex str.each_byte do |byte| {% begin %} case byte.unsafe_chr - when {{*SPECIAL_CHARACTERS}} + when {{SPECIAL_CHARACTERS.splat}} result << '\\' result.write_byte byte else diff --git a/src/slice.cr b/src/slice.cr index ac83eacae99a..27584c5d1a68 100644 --- a/src/slice.cr +++ b/src/slice.cr @@ -37,7 +37,7 @@ struct Slice(T) {% if @type.name != "Slice(T)" && T < Number %} {{T}}.slice({{args.splat(", ")}}read_only: {{read_only}}) {% else %} - %ptr = Pointer(typeof({{*args}})).malloc({{args.size}}) + %ptr = Pointer(typeof({{args.splat}})).malloc({{args.size}}) {% for arg, i in args %} %ptr[{{i}}] = {{arg}} {% end %} diff --git a/src/static_array.cr b/src/static_array.cr index 0bfaa006e8b8..4cb2b186f200 100644 --- a/src/static_array.cr +++ b/src/static_array.cr @@ -50,7 +50,7 @@ struct StaticArray(T, N) # * `Number.static_array` is a convenient alternative for designating a # specific numerical item type. macro [](*args) - %array = uninitialized StaticArray(typeof({{*args}}), {{args.size}}) + %array = uninitialized StaticArray(typeof({{args.splat}}), {{args.size}}) {% for arg, i in args %} %array.to_unsafe[{{i}}] = {{arg}} {% end %} diff --git a/src/syscall/aarch64-linux.cr b/src/syscall/aarch64-linux.cr index aa0fc5dc36ef..5a61e8e7eed8 100644 --- a/src/syscall/aarch64-linux.cr +++ b/src/syscall/aarch64-linux.cr @@ -335,7 +335,7 @@ module Syscall macro def_syscall(name, return_type, *args) @[AlwaysInline] - def self.{{name.id}}({{*args}}) : {{return_type}} + def self.{{name.id}}({{args.splat}}) : {{return_type}} ret = uninitialized {{return_type}} {% if args.size == 0 %} diff --git a/src/syscall/arm-linux.cr b/src/syscall/arm-linux.cr index 128417ca4508..97119fc4b3f3 100644 --- a/src/syscall/arm-linux.cr +++ b/src/syscall/arm-linux.cr @@ -410,7 +410,7 @@ module Syscall macro def_syscall(name, return_type, *args) @[AlwaysInline] - def self.{{name.id}}({{*args}}) : {{return_type}} + def self.{{name.id}}({{args.splat}}) : {{return_type}} ret = uninitialized {{return_type}} {% if args.size == 0 %} diff --git a/src/syscall/i386-linux.cr b/src/syscall/i386-linux.cr index 3788005768bd..843b2d1fd856 100644 --- a/src/syscall/i386-linux.cr +++ b/src/syscall/i386-linux.cr @@ -446,7 +446,7 @@ module Syscall macro def_syscall(name, return_type, *args) @[AlwaysInline] - def self.{{name.id}}({{*args}}) : {{return_type}} + def self.{{name.id}}({{args.splat}}) : {{return_type}} ret = uninitialized {{return_type}} {% if args.size == 0 %} diff --git a/src/syscall/x86_64-linux.cr b/src/syscall/x86_64-linux.cr index 4dfac42decec..1f01c9226658 100644 --- a/src/syscall/x86_64-linux.cr +++ b/src/syscall/x86_64-linux.cr @@ -369,7 +369,7 @@ module Syscall macro def_syscall(name, return_type, *args) @[AlwaysInline] - def self.{{name.id}}({{*args}}) : {{return_type}} + def self.{{name.id}}({{args.splat}}) : {{return_type}} ret = uninitialized {{return_type}} {% if args.size == 0 %} diff --git a/src/xml/builder.cr b/src/xml/builder.cr index 1ebe23efcd47..54e5f45adddc 100644 --- a/src/xml/builder.cr +++ b/src/xml/builder.cr @@ -283,7 +283,7 @@ class XML::Builder end private macro call(name, *args) - ret = LibXML.xmlTextWriter{{name}}(@writer, {{*args}}) + ret = LibXML.xmlTextWriter{{name}}(@writer, {{args.splat}}) check ret, {{@def.name.stringify}} end diff --git a/src/yaml/builder.cr b/src/yaml/builder.cr index 2764580dde13..79bbd967490d 100644 --- a/src/yaml/builder.cr +++ b/src/yaml/builder.cr @@ -199,7 +199,7 @@ class YAML::Builder end private macro emit(event_name, *args) - LibYAML.yaml_{{event_name}}_event_initialize(pointerof(@event), {{*args}}) + LibYAML.yaml_{{event_name}}_event_initialize(pointerof(@event), {{args.splat}}) yaml_emit({{event_name.stringify}}) end