From 62638f4bb652bb2506af508dcd24da48e724d5b0 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Sun, 1 Dec 2024 09:29:17 -0600 Subject: [PATCH] Use safe var name in getter/setter/property macro (#15239) Previously, using a local variable called `value` or calling a method with that name without an explicit `self` receiver would collide with the one defined in the `getter`, `setter`, and `property` blocks. This commit uses a safe variable name to avoid that collision. --- src/object.cr | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/object.cr b/src/object.cr index 800736687788..4443eaec3916 100644 --- a/src/object.cr +++ b/src/object.cr @@ -457,18 +457,18 @@ class Object {{var_prefix}}\{{name.var.id}} : \{{name.type}}? def {{method_prefix}}\{{name.var.id}} : \{{name.type}} - if (value = {{var_prefix}}\{{name.var.id}}).nil? + if (%value = {{var_prefix}}\{{name.var.id}}).nil? {{var_prefix}}\{{name.var.id}} = \{{yield}} else - value + %value end end \{% else %} def {{method_prefix}}\{{name.id}} - if (value = {{var_prefix}}\{{name.id}}).nil? + if (%value = {{var_prefix}}\{{name.id}}).nil? {{var_prefix}}\{{name.id}} = \{{yield}} else - value + %value end end \{% end %} @@ -561,10 +561,10 @@ class Object end def {{method_prefix}}\{{name.var.id}} : \{{name.type}} - if (value = {{var_prefix}}\{{name.var.id}}).nil? + if (%value = {{var_prefix}}\{{name.var.id}}).nil? ::raise ::NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.var.id}} cannot be nil") else - value + %value end end \{% else %} @@ -573,10 +573,10 @@ class Object end def {{method_prefix}}\{{name.id}} - if (value = {{var_prefix}}\{{name.id}}).nil? + if (%value = {{var_prefix}}\{{name.id}}).nil? ::raise ::NilAssertionError.new("\{{@type}}\{{"{{doc_prefix}}".id}}\{{name.id}} cannot be nil") else - value + %value end end \{% end %} @@ -688,18 +688,18 @@ class Object {{var_prefix}}\{{name.var.id}} : \{{name.type}}? def {{method_prefix}}\{{name.var.id}}? : \{{name.type}} - if (value = {{var_prefix}}\{{name.var.id}}).nil? + if (%value = {{var_prefix}}\{{name.var.id}}).nil? {{var_prefix}}\{{name.var.id}} = \{{yield}} else - value + %value end end \{% else %} def {{method_prefix}}\{{name.id}}? - if (value = {{var_prefix}}\{{name.id}}).nil? + if (%value = {{var_prefix}}\{{name.id}}).nil? {{var_prefix}}\{{name.id}} = \{{yield}} else - value + %value end end \{% end %} @@ -970,10 +970,10 @@ class Object {{var_prefix}}\{{name.var.id}} : \{{name.type}}? def {{method_prefix}}\{{name.var.id}} : \{{name.type}} - if (value = {{var_prefix}}\{{name.var.id}}).nil? + if (%value = {{var_prefix}}\{{name.var.id}}).nil? {{var_prefix}}\{{name.var.id}} = \{{yield}} else - value + %value end end @@ -981,10 +981,10 @@ class Object end \{% else %} def {{method_prefix}}\{{name.id}} - if (value = {{var_prefix}}\{{name.id}}).nil? + if (%value = {{var_prefix}}\{{name.id}}).nil? {{var_prefix}}\{{name.id}} = \{{yield}} else - value + %value end end @@ -1216,10 +1216,10 @@ class Object {{var_prefix}}\{{name.var.id}} : \{{name.type}}? def {{method_prefix}}\{{name.var.id}}? : \{{name.type}} - if (value = {{var_prefix}}\{{name.var.id}}).nil? + if (%value = {{var_prefix}}\{{name.var.id}}).nil? {{var_prefix}}\{{name.var.id}} = \{{yield}} else - value + %value end end @@ -1227,10 +1227,10 @@ class Object end \{% else %} def {{method_prefix}}\{{name.id}}? - if (value = {{var_prefix}}\{{name.id}}).nil? + if (%value = {{var_prefix}}\{{name.id}}).nil? {{var_prefix}}\{{name.id}} = \{{yield}} else - value + %value end end