noir-lang / noir

Noir is a domain specific language for zero knowledge proofs
https://noir-lang.org
Apache License 2.0
854 stars 185 forks source link

Lexer does not handle U+00a0 and other unicode space characters #5163

Open pventuzelo opened 3 months ago

pventuzelo commented 3 months ago

Aim

We (https://github.com/FuzzingLabs) found that the compiler causes an error when you place in the same line the } of the end of an if and a call to else {.

The compiler doesn’t handle the line } else { properly. It seems to treat it as a return line because the error message indicates that the return type of the function is (). Placing the else statement on a new line avoids the issue.

The issue occurs with the latest compiler version on the master branch. The error message is as follows:

error: expected type u8, found type ()

error: Expected a binary operator but found
            } else { 

Expected Behavior

No error should be returned.

Bug

/* This function causes a compilation error because the compiler 
   does not handle the "} else {" line correctly */
fn func1() -> u8 {
  if true {
  } else { // Placing "} else {" on the same line causes the issue
  }
  12
}

/* The compilation error occurs even if there are instructions 
   between the lines */
fn func2() -> u8 {
  let mut var = 1;
  if var == 1 {
    var += 1;
  } else {
    var += 2;
  }
  var += 3;
  12
}

/* The compilation error only occurs if "}" and "else" are on 
   the same line. For example, this function does not cause a 
   compilation error */
fn func3() -> u8 {
  if true {
  }
  else { // Placing "else" on a new line avoids the issue
  }
  12
}

fn main() {
}

To Reproduce

1. 2. 3. 4.

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Installation Method

None

Nargo Version

No response

NoirJS Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

jfecher commented 3 months ago

I don't get any errors when running the code snippet on c93c7380c705fcec5c77bfc436c2f5ea085edd77

AFredefon commented 3 months ago

I don't get any errors when running the code snippet on c93c738

The issue seems to be that I was using the character U+00a0 in the space between "}" and "else" (I don't know why I was using this character). It appears that the character was replaced during the pasting of the code in the report.