[ruby-core:117714] [Ruby master Bug#20457] Final `return` is eliminated from the AST

Issue #20457 has been reported by tenderlovemaking (Aaron Patterson). ---------------------------------------- Bug #20457: Final `return` is eliminated from the AST https://bugs.ruby-lang.org/issues/20457 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Given the following code: ```ruby def foo a = 1 return a end ``` If you parse this with RubyVM::AbstractSyntaxTree, the AST will be missing the `return` node. Of course the `return` node isn't necessary for compilation, but would be required for building an LSP for example. Here's a full program to demonstrate: ```ruby ast = RubyVM::AbstractSyntaxTree.parse DATA.read pp ast # Output is like this: # # (SCOPE@1:0-4:3 # tbl: [] # args: nil # body: # (DEFN@1:0-4:3 # mid: :foo # body: # (SCOPE@1:0-4:3 # tbl: [:a] # args: (ARGS@1:7-1:7 pre_num: 0 pre_init: nil opt: nil first_post: nil post_num: 0 post_init: nil rest: nil kw: nil kwrest: nil block: nil) # body: (BLOCK@2:2-3:10 (LASGN@2:2-2:7 :a (INTEGER@2:6-2:7 1)) (LVAR@3:9-3:10 :a))))) __END__ def foo a = 1 return a end ``` Btw, I'm happy to write failing tests for this type of stuff I'm just not sure where to put it! :) -- https://bugs.ruby-lang.org/

Issue #20457 has been updated by nobu (Nobuyoshi Nakada). Optimizations in the parser such as `reduce_nodes` that are not intended for the VM will no longer be necessary. ---------------------------------------- Bug #20457: Final `return` is eliminated from the AST https://bugs.ruby-lang.org/issues/20457#change-108126 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Given the following code: ```ruby def foo a = 1 return a end ``` If you parse this with RubyVM::AbstractSyntaxTree, the AST will be missing the `return` node. Of course the `return` node isn't necessary for compilation, but would be required for building an LSP for example. Here's a full program to demonstrate: ```ruby ast = RubyVM::AbstractSyntaxTree.parse DATA.read pp ast # Output is like this: # # (SCOPE@1:0-4:3 # tbl: [] # args: nil # body: # (DEFN@1:0-4:3 # mid: :foo # body: # (SCOPE@1:0-4:3 # tbl: [:a] # args: (ARGS@1:7-1:7 pre_num: 0 pre_init: nil opt: nil first_post: nil post_num: 0 post_init: nil rest: nil kw: nil kwrest: nil block: nil) # body: (BLOCK@2:2-3:10 (LASGN@2:2-2:7 :a (INTEGER@2:6-2:7 1)) (LVAR@3:9-3:10 :a))))) __END__ def foo a = 1 return a end ``` Btw, I'm happy to write failing tests for this type of stuff I'm just not sure where to put it! :) -- https://bugs.ruby-lang.org/

Issue #20457 has been updated by Eregon (Benoit Daloze). I want to add for context that naturally Prism already provides this information (and much more) and has a much better API. Nobody should use `RubyVM::AbstractSyntaxTree`, as its documentation pretty much already says. ---------------------------------------- Bug #20457: Final `return` is eliminated from the AST https://bugs.ruby-lang.org/issues/20457#change-108136 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Given the following code: ```ruby def foo a = 1 return a end ``` If you parse this with RubyVM::AbstractSyntaxTree, the AST will be missing the `return` node. Of course the `return` node isn't necessary for compilation, but would be required for building an LSP for example. Here's a full program to demonstrate: ```ruby ast = RubyVM::AbstractSyntaxTree.parse DATA.read pp ast # Output is like this: # # (SCOPE@1:0-4:3 # tbl: [] # args: nil # body: # (DEFN@1:0-4:3 # mid: :foo # body: # (SCOPE@1:0-4:3 # tbl: [:a] # args: (ARGS@1:7-1:7 pre_num: 0 pre_init: nil opt: nil first_post: nil post_num: 0 post_init: nil rest: nil kw: nil kwrest: nil block: nil) # body: (BLOCK@2:2-3:10 (LASGN@2:2-2:7 :a (INTEGER@2:6-2:7 1)) (LVAR@3:9-3:10 :a))))) __END__ def foo a = 1 return a end ``` Btw, I'm happy to write failing tests for this type of stuff I'm just not sure where to put it! :) -- https://bugs.ruby-lang.org/

Issue #20457 has been updated by tenderlovemaking (Aaron Patterson). Eregon (Benoit Daloze) wrote in #note-2:
I want to add for context that naturally Prism already provides this information (and much more) and has a much better API. Nobody should use `RubyVM::AbstractSyntaxTree`, as its documentation pretty much already says.
From what I understand, lrama intends to eventually provide the same AST that Prism does (or a superset). As you say, people shouldn't use `RubyVM::AbstractSyntaxTree`, but if lrama (basically parse.y) intends on providing the same AST, then I think cases where Prism provides information that lrama doesn't should be considered a bug (and we should track it). ---------------------------------------- Bug #20457: Final `return` is eliminated from the AST https://bugs.ruby-lang.org/issues/20457#change-108152 * Author: tenderlovemaking (Aaron Patterson) * Status: Open * Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN ---------------------------------------- Given the following code: ```ruby def foo a = 1 return a end ``` If you parse this with RubyVM::AbstractSyntaxTree, the AST will be missing the `return` node. Of course the `return` node isn't necessary for compilation, but would be required for building an LSP for example. Here's a full program to demonstrate: ```ruby ast = RubyVM::AbstractSyntaxTree.parse DATA.read pp ast # Output is like this: # # (SCOPE@1:0-4:3 # tbl: [] # args: nil # body: # (DEFN@1:0-4:3 # mid: :foo # body: # (SCOPE@1:0-4:3 # tbl: [:a] # args: (ARGS@1:7-1:7 pre_num: 0 pre_init: nil opt: nil first_post: nil post_num: 0 post_init: nil rest: nil kw: nil kwrest: nil block: nil) # body: (BLOCK@2:2-3:10 (LASGN@2:2-2:7 :a (INTEGER@2:6-2:7 1)) (LVAR@3:9-3:10 :a))))) __END__ def foo a = 1 return a end ``` Btw, I'm happy to write failing tests for this type of stuff I'm just not sure where to put it! :) -- https://bugs.ruby-lang.org/
participants (3)
-
Eregon (Benoit Daloze)
-
nobu (Nobuyoshi Nakada)
-
tenderlovemaking (Aaron Patterson)