BZ4: tests conflict

It seems two tests are conflict in BZ4, the inner local scope > closure which captured when declare the function > outer scope.

var x = 1;
var y = 2;

fun printBoth() {
  if (x < y) {
    print "x is less than y:";
    print x;
    print y;
  } else {
    print "x is not less than y:";
    print x;
    print y;
  }
}

{
  var x = 10;
  {
    var y = 20;

    var i = 0;
    while (i < 3) {
      x = x + 1;
      y = y - 1;
      print "Local x: ";
      print x;
      print "Local y: ";
      print y;
      i = i + 1;
    }

    if (x > y) {
      print "Local x > y";
    }

    printBoth();
  }
}

if (x == 1 and y == 2) {
  print "Globals unchanged:";
  printBoth();
}

print --- output ---;
// Logs from your program will appear here!
// Local x: 
// 11
// Local y: 
// 19
// Local x: 
// 12
// Local y: 
// 18
// Local x: 
// 13
// Local y: 
// 17
// x is less than y:
// 1   // -> captured as closure
// 2
// Globals unchanged:
// x is less than y:
// 1
// 2
// This program demonstrates global and local variable shadowing in Lox.
var a = 63;

fun printAndModify() {
  print a;
  var a = 58;
  print a;
}

print a;
var a = 97;
printAndModify();

// remote: [tester::#BZ4] [test-1] $ ./your_program.sh run test.lox
// remote: [your_program] Logs from your program will appear here!
// remote: [your_program] 63
// remote: [your_program] 63 // ?? -> 63 is caputured as closure when function declared I believe, same as example above
// remote: [your_program] 58
// remote: [tester::#BZ4] [test-1] ✓ 63
// remote: [tester::#BZ4] [test-1] 𐄂 63
// remote: [tester::#BZ4] [test-1] Expected line #2 on stdout to be "97", got "63"
// remote: [tester::#BZ4] [test-1] Test failed (try setting 'debug: true' in your codecrafters.yml to see more details)

Hey @unkcpz, thanks for highlighting this!

I’ll check in with the team and get back to you as soon as we have a fix.

Thanks!
I actually make tests passed. I think there is a bit ambiguity about how to deal with the var a=97; before the function call.

If I understand correctly, this is a case in the challenge (where re-declaration should be an error) of the book. To make it more similar to the book (such as craftinginterpreters/test/closure/assign_to_closure.lox at 4a840f70f69c6ddd17cfef4f6964f8e1bcd8c3d4 · munificent/craftinginterpreters · GitHub), I think it should be:

var a = 63;

fun printAndModify() {
  print a;
  var a = 58;
  print a;
}

print a;
a = 97;
printAndModify();
1 Like

Quick update: we’ve made a minor QoL fix in this PR.

1 Like

Thanks for quick fix.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.