Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement block scopes #103

Open
apetkov-so opened this issue Feb 19, 2025 · 2 comments · May be fixed by #109
Open

Implement block scopes #103

apetkov-so opened this issue Feb 19, 2025 · 2 comments · May be fixed by #109
Assignees
Labels
policy-compiler Policy language compiler

Comments

@apetkov-so
Copy link
Contributor

apetkov-so commented Feb 19, 2025

Limit variable scope to its block.
c.f. Policy Lang v2 spec

function f() int {
  let f = 1
  if f == 1 {
    let a = f + 1 // vars from outer scopes are visible in inner scopes
    check a == 2
  }
  // `a` should not be available here
  return 0
}
@apetkov-so apetkov-so added this to the Policy Lang v2 Features milestone Feb 19, 2025
@apetkov-so apetkov-so self-assigned this Feb 19, 2025
@apetkov-so apetkov-so added the policy-compiler Policy language compiler label Feb 19, 2025
@apetkov-so
Copy link
Contributor Author

@chip-so This seems to be done? I couldn't find an explicit test for it, but the following passes:

 let text = r#"
    function foo() int {
        let x = {
            let y = 4
            : y
        }
        // This is an error - y does not exist in the outer scope
        return y
}"#;
let policy = parse_policy_str(text, Version::V1).expect("should parse");
let r = Compiler::new(&policy).compile().unwrap_err().err_type;
assert_eq!(r, CompileErrorType::NotDefined("Unknown identifier `y`".to_string()));

@chip-so
Copy link
Contributor

chip-so commented Feb 21, 2025

This seems to be done?

That works because you're using the block expression, which was implemented previously. Now it needs to work in all other contexts that have a block. This ticket is for adding it to if and match statements. We already implemented it for map, and if/match expressions are in different tickets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
policy-compiler Policy language compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants