Skip to content

switch codegen bug #245

@chrsm

Description

@chrsm

With this code:

_check_cond_expr = (iftyp, n, args) ->
  sw = { ct: iftyp, op: op }
  why = switch sw
    -- woo
    when ct: 'if', op: '=='
      'never'
    when ct: 'if', op: '!='
      'always'
    when ct: 'unless', op: '=='
      'always'
    when ct: 'unless', op: '!='
      'never'

Generated (relevant portions):
With YueScript 0.33.0:

local _check_cond_expr
_check_cond_expr = function(iftyp, n, args)
	local sw = {
		ct = iftyp,
		op = op
	}
	local why
	local _type_0 = type(sw)
	local _tab_0 = "table" == _type_0 or "userdata" == _type_0
...

With YueScript 0.33.2, which results in issue treating value like func: [string "./yuecheck/rules/helpers.yue"]:72: attempt to call a table value (local 'sw')

local _check_cond_expr
_check_cond_expr = function(iftyp, n, args)
	local sw = {
		ct = iftyp,
		op = op
	}
	local why
	local _exp_0 = sw({
	})
	local _type_0 = type(_exp_0)
	local _tab_0 = "table" == _type_0 or "userdata" == _type_0

Moving -- woo comment fixes in YueScript 0.33.2:

_check_cond_expr = (iftyp, n, args) ->
  sw = { ct: iftyp, op: op }
  why = switch sw
    -- woo
    when ct: 'if', op: '=='
      'never'
    when ct: 'if', op: '!='
      'always'
    when ct: 'unless', op: '=='
      'always'
    when ct: 'unless', op: '!='
      'never'

Generated:

local _check_cond_expr
_check_cond_expr = function(iftyp, n, args)
	local sw = {
		ct = iftyp,
		op = op
	}
	local why
	local _type_0 = type(sw)
	local _tab_0 = "table" == _type_0 or "userdata" == _type_0

I tried to fix, works for this case but broke statements like this from ambiguous.yue:

do
  x = switch f
    :v
    when f
      :v

patch https://github.com/IppClub/YueScript/blame/main/src/yuescript/yue_parser.cpp#L507:

diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp
index c5c3ef7..4779f77 100644
--- a/src/yuescript/yue_parser.cpp
+++ b/src/yuescript/yue_parser.cpp
@@ -504,7 +504,7 @@ YueParser::YueParser() {
 		expected_expression_error
 	);
 	Switch = key("switch") >> space >>
-		must_exp >> -(space >> Assignment) >>
+		disable_arg_table_block_rule(must_exp >> -(space >> Assignment)) >>
 		space >> Seperator >> (
 			SwitchCase >> space >> (
 				switch_block |

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions