Skip to content

Pattern matching guard causes a runtime error "tmp is not defined"  #6542

@markknol

Description

@markknol

When I use a guard with underscore and capture variable in my case at forehand, I get a runtime error 'Uncaught ReferenceError: tmp is not defined' when targeting JavaScript.

It does work when I replace if (_ > 0): with if (value > 0).

function isEven(value:Float) return value % 2 == 0;

for (value in -10 ... 10) {
  switch value {
    case value = isEven(_) => true if (_ > 0):  // runtime error
    // case value = isEven(_) => true if (value > 0): // works
      trace(value);
    case _:
  }
}

The output looks like

// Generated by Haxe 3.4.2
(function () { "use strict";
var Test = function() { };
Test.main = function() {
	var isEven = function(value) {
		return value % 2 == 0;
	};
	var _g = -10;
	while(_g < 10) {
		var value1 = _g++;
		if(isEven(value1) == true) {
			if(tmp > 0) {
				console.log(value1);
			}
		}
	}
};
Test.main();
})();

Demo: https://try.haxe.org/#dcAe7
Found on Haxe 3.4.2, JavaScript target


On the unofficial try haxe http://try-haxe.mrcdk.com/#85794 I get

Unbound variable, please report this
{
	v_id = 507;
	v_name = tmp;
	v_type = Int;
	v_capture = false;
	v_extra = None;
	v_meta = [];
}
While analyzing Test.main
dot graph written to dump/js/Test.main
File "src/optimization/analyzerTypes.ml", line 221, characters 3-9: Assertion failed

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions