Project

General

Profile

Actions

Bug #20468

closed

Segfault on safe navigation in for target

Added by kddnewton (Kevin Newton) 26 days ago. Updated 14 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:117763]

Description

for foo&.bar in []; end

Updated by nobu (Nobuyoshi Nakada) 26 days ago

I can't imagine what it should do when foo == nil.
Should be a syntax error probably?

Updated by nobu (Nobuyoshi Nakada) 26 days ago

Hmmm, may be possible to interpret that code like as foo&.then {|recv| for recv.bar in []; end}.
Just an idea.

Updated by nobu (Nobuyoshi Nakada) 26 days ago

  • Assignee set to matz (Yukihiro Matsumoto)

Updated by nobu (Nobuyoshi Nakada) 25 days ago

  • Assignee deleted (matz (Yukihiro Matsumoto))

I found that a for loop variable is looser than I expected.
Not only a safe navigation call, even a constant can be used.
I don't think a constant is useful as a loop variable that is expected to be re-assigned.

https://github.com/ruby/ruby/pull/10723

Updated by Eregon (Benoit Daloze) 25 days ago

Given for foo.bar in []; end is valid and does for.bar = element,
I think for foo&.bar in []; end could simply do for&.bar = element which is the same as for.bar = element unless foo.nil? (NIL_P(foo) to be precise).

Changing the syntax for for variables sounds more risky and likely to unexpectedly disallow some new lhs forms when the lhs rule is updated, and the for_var rule is forgotten to be updated too.

Updated by Eregon (Benoit Daloze) 25 days ago

IOW I think the segfault could be fixed here without changing what is valid syntax.
And given this segfault probably affects older versions too there it seems better to fix the segfault than to change syntax.

Updated by zverok (Victor Shepelev) 25 days ago

Agree with @Eregon (Benoit Daloze). The code should be more or less equivalent to:

for temp in [1, 2, 3]
  foo&.bar = temp
end

...which is totally valid and foo&.bar = is a no-op (semantically).

Updated by kddnewton (Kevin Newton) 22 days ago

@nobu (Nobuyoshi Nakada) it's the same possibilities in for, rescue, and multi-write. As in:

for (foo, @foo, @@foo, $foo, Foo, Foo::Foo, foo.foo, foo[foo]) in bar; end

begin; rescue => foo; end
begin; rescue => @foo; end
begin; rescue => @@foo; end
begin; rescue => $foo; end
begin; rescue => Foo; end
begin; rescue => Foo::Foo; end
begin; rescue => foo.foo; end
begin; rescue => foo[foo]; end

(foo, @foo, @@foo, $foo, Foo, Foo::Foo, foo.foo, foo[foo]) = bar
Actions #9

Updated by nobu (Nobuyoshi Nakada) 14 days ago

  • Status changed from Open to Closed

Applied in changeset git|2dd46bb82ffc4dff01d7ea70922f0e407acafb4e.


[Bug #20468] Fix safe navigation in for variable

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0