Skip to content

mrb_str_aset_m() should return replace instead of str#6303

Merged
matz merged 1 commit into
mruby:masterfrom
hasumikin:fix/return-value-of-String_aset
Jul 17, 2024
Merged

mrb_str_aset_m() should return replace instead of str#6303
matz merged 1 commit into
mruby:masterfrom
hasumikin:fix/return-value-of-String_aset

Conversation

@hasumikin

Copy link
Copy Markdown
Contributor

string[]=(idx, replace) should return replace.

Actual (wrong)

string.[]=(idx, replace) → string
string.[]=(idx, len, replace) → string

Expected

string.[]=(idx, replace) → replace
string.[]=(idx, len, replace) → replace

Sidenote

As of the current mruby-compiler, (string[idx] = 'X') creates not only "CALL_NODE" but also "ASGN_NODE" and "OP_MOVE", overriding the wrong return value. On the other hand, string.[]=(idx, 'X') creates only "CALL_NODE", exposing the wrong return value.

If my new mruby-compiler2, leveraging Prism, took the place of official compiler, (string[idx] = 'X') and string.[]=(idx, 'X') would be going to generate the same VM code without "OP_MOVE". So I paranoidly added tests.

FYI: You can find how the new mruby-compiler2's AST and VM code look like in mruby/c's issue (mruby/c had the same bug): mrubyc/mrubyc#210

`string[]=(idx, replace)` should return `replace`.

## Actual (wrong)

```
string.[]=(idx, replace) → string
string.[]=(idx, len, replace) → string
```

## Expected

```
string.[]=(idx, replace) → replace
string.[]=(idx, len, replace) → replace
```

## Sidenote

As of the current mruby-compiler, `(string[idx] = 'X')` creates not only "CALL_NODE" but also "ASGN_NODE" and "OP_MOVE", overriding the wrong return value.
On the other hand, `string.[]=(idx, 'X')` creates only "CALL_NODE", exposing the wrong return value.

If my new mruby-compiler2, leveraging Prism, took the place of official compiler, `(string[idx] = 'X')` and `string.[]=(idx, 'X')` would be going to generate the same VM code without "OP_MOVE".
So I paranoidly added tests.

FYI: You can find how the new mruby-compiler2's AST and VM code look like in mruby/c's issue (mruby/c had the same bug): mrubyc/mrubyc#210
@hasumikin hasumikin requested a review from matz as a code owner July 16, 2024 09:59
@github-actions github-actions Bot added the core label Jul 16, 2024
@matz matz merged commit 1fc8c20 into mruby:master Jul 17, 2024
@matz

matz commented Jul 17, 2024

Copy link
Copy Markdown
Member

Merged. But considering the behavior of the current CRuby parser, I assume the Prism generation is a bug.

@hasumikin hasumikin deleted the fix/return-value-of-String_aset branch July 17, 2024 01:08
@hasumikin

Copy link
Copy Markdown
Contributor Author

@matz Thank you.
I will talk to Kevin regarding the lack of NODE_ASGN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants