From d7d11090991d217b1eeb36d5dce5102bdba8b277 Mon Sep 17 00:00:00 2001 From: Victor Shepelev Date: Tue, 23 Dec 2025 19:09:41 +0200 Subject: [PATCH 1/2] Describe base code layout rules (#15696) * Describe base code layout rules * Enhance optional keyword explanation * Change the logical operators description --- doc/syntax.rdoc | 3 ++ doc/syntax/layout.rdoc | 118 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 doc/syntax/layout.rdoc diff --git a/doc/syntax.rdoc b/doc/syntax.rdoc index cb427b6f0f03a2..a48c83ff152dc2 100644 --- a/doc/syntax.rdoc +++ b/doc/syntax.rdoc @@ -2,6 +2,9 @@ The Ruby syntax is large and is split up into the following sections: +{Code Layout}[rdoc-ref:syntax/layout.rdoc] :: + Breaking code in lines + Literals[rdoc-ref:syntax/literals.rdoc] :: Numbers, Strings, Arrays, Hashes, etc. diff --git a/doc/syntax/layout.rdoc b/doc/syntax/layout.rdoc new file mode 100644 index 00000000000000..f07447587ba333 --- /dev/null +++ b/doc/syntax/layout.rdoc @@ -0,0 +1,118 @@ += Code Layout + +Expressions in Ruby are separated by line breaks: + + x = 1 + y = 2 + z = x + y + +Line breaks also used as logical separators of the headers of some of control structures from their bodies: + + if z > 3 # line break ends the condition and starts the body + puts "more" + end + + while x < 3 # line break ends the condition and starts the body + x += 1 + end + +; can be used as an expressions separator instead of a line break: + + x = 1; y = 2; z = x + y + if z > 3; puts "more"; end + +Traditionally, expressions separated by ; is used only in short scripts and experiments. + +In some control structures, there is an optional keyword that can be used instead of a line break to separate their elements: + + # if, elsif, until and case ... when: 'then' is an optional separator: + + if z > 3 then puts "more" end + + case x + when Numeric then "number" + when String then "string" + else "object" + end + + # while and until: 'do' is an optional separator + while x < 3 do x +=1 end + +Also, line breaks can be skipped in some places where it doesn't create any ambiguity. Note in the example above: no line break needed before +end+, just as no line break needed after +else+. + +== Breaking expressions in lines + +One expression might be split into several lines when each line can be unambiguously identified as "incomplete" without the next one. + +These works: + + x = # incomplete without something after = + 1 + # incomplete without something after + + 2 + + File.read "test.txt", # incomplete without something after , + enconding: "utf-8" + +These would not: + + # unintended interpretation: + x = 1 # already complete expression + + 2 # interpreted as a separate +2 + + # syntax error: + File.read "test.txt" # already complete expression + , encoding: "utf-8" # attempt to parse as a new expression, SyntaxError + +The exceptions to the rule are lines starting with . ("leading dot" style of method calls) or logical operators &&/|| and and/or: + + # OK, interpreted as a chain of calls + File.read('test.txt') + .strip("\n") + .split("\t") + .sort + + # OK, interpreted as a chain of logical operators: + File.empty?('test.txt') + || File.size('test.txt') < 10 + || File.read('test.txt').strip.empty? + +If the expressions is broken into multiple lines in any of the ways described above, comments between separate lines are allowed: + + sum = base_salary + + # see "yearly bonuses section" + yearly_bonus(year) + + # per-employee coefficient is described + # in another module + personal_coeff(employee) + + # We want to short-circuit on empty files + File.empty?('test.txt') + # Or almost empty ones + || File.size('test.txt') < 10 + # Otherwise we check if it is full of spaces + || File.read('test.txt').strip.empty? + +Finally, the code can explicitly tell Ruby that the expression is continued on the next line with \\: + + # Unusual, but works + File.read "test.txt" \ + , encoding: "utf-8" + + # More regular usage (joins the strings on parsing instead + # of concatenating them in runtime, as + would do): + TEXT = "One pretty long line" \ + "one more long line" \ + "one other line of the text" + +The \\ works as a parse time line break escape, so with it, comments can not be inserted between the lines: + + TEXT = "line 1" \ + # here would be line 2: + "line 2" + + # This is interpreted as if there was no line break where \ is, + # i.e. the same as + TEXT = "line 1" # here would be line 2: + "line 2" + + puts TEXT #=> "line 1" From e2cf92eddc5403316b0d449b02ba403a27610d7b Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 23 Dec 2025 12:59:41 -0500 Subject: [PATCH 2/2] Move special const check to gc.c for rb_gc_impl_object_moved_p --- gc.c | 20 +++++++++++++++----- gc/default/default.c | 14 +++++--------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/gc.c b/gc.c index 104b027cca788f..8e239011e6f295 100644 --- a/gc.c +++ b/gc.c @@ -402,7 +402,7 @@ void rb_vm_update_references(void *ptr); #define RMOVED(obj) ((struct RMoved *)(obj)) #define TYPED_UPDATE_IF_MOVED(_objspace, _type, _thing) do { \ - if (rb_gc_impl_object_moved_p((_objspace), (VALUE)(_thing))) { \ + if (gc_object_moved_p_internal((_objspace), (VALUE)(_thing))) { \ *(_type *)&(_thing) = (_type)gc_location_internal(_objspace, (VALUE)_thing); \ } \ } while (0) @@ -2868,6 +2868,16 @@ gc_mark_machine_stack_location_maybe(VALUE obj, void *data) #endif } +static bool +gc_object_moved_p_internal(void *objspace, VALUE obj) +{ + if (SPECIAL_CONST_P(obj)) { + return false; + } + + return rb_gc_impl_object_moved_p(objspace, obj); +} + static VALUE gc_location_internal(void *objspace, VALUE value) { @@ -3646,7 +3656,7 @@ check_id_table_move(VALUE value, void *data) { void *objspace = (void *)data; - if (rb_gc_impl_object_moved_p(objspace, (VALUE)value)) { + if (gc_object_moved_p_internal(objspace, (VALUE)value)) { return ID_TABLE_REPLACE; } @@ -3690,7 +3700,7 @@ update_id_table(VALUE *value, void *data, int existing) { void *objspace = (void *)data; - if (rb_gc_impl_object_moved_p(objspace, (VALUE)*value)) { + if (gc_object_moved_p_internal(objspace, (VALUE)*value)) { *value = gc_location_internal(objspace, (VALUE)*value); } @@ -3733,11 +3743,11 @@ update_const_tbl_i(VALUE value, void *objspace) { rb_const_entry_t *ce = (rb_const_entry_t *)value; - if (rb_gc_impl_object_moved_p(objspace, ce->value)) { + if (gc_object_moved_p_internal(objspace, ce->value)) { ce->value = gc_location_internal(objspace, ce->value); } - if (rb_gc_impl_object_moved_p(objspace, ce->file)) { + if (gc_object_moved_p_internal(objspace, ce->file)) { ce->file = gc_location_internal(objspace, ce->file); } diff --git a/gc/default/default.c b/gc/default/default.c index be0be4a37335e4..ef100d7dea27dc 100644 --- a/gc/default/default.c +++ b/gc/default/default.c @@ -1375,16 +1375,12 @@ check_rvalue_consistency(rb_objspace_t *objspace, const VALUE obj) static inline bool gc_object_moved_p(rb_objspace_t *objspace, VALUE obj) { - if (RB_SPECIAL_CONST_P(obj)) { - return FALSE; - } - else { - int ret; - asan_unpoisoning_object(obj) { - ret = BUILTIN_TYPE(obj) == T_MOVED; - } - return ret; + + bool ret; + asan_unpoisoning_object(obj) { + ret = BUILTIN_TYPE(obj) == T_MOVED; } + return ret; } static inline int