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

fix: Fix 3 parser test cases in parsing #2284

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl CustomDiagnostic {
matches!(self.kind, DiagnosticKind::Error)
}

pub fn is_warrning(&self) -> bool {
pub fn is_warning(&self) -> bool {
matches!(self.kind, DiagnosticKind::Warning)
}
}
Expand Down
87 changes: 66 additions & 21 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,10 @@ fn where_clause() -> impl NoirParser<Vec<TraitConstraint>> {
.then_ignore(just(Token::Colon))
.then(ident())
.then(generic_type_args(parse_type()))
.validate(|((typ, trait_name), trait_generics), span, emit| {
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
yordanmadzhunkov marked this conversation as resolved.
Show resolved Hide resolved
TraitConstraint { typ, trait_name, trait_generics }
.validate(|((typ, trait_name), trait_generics), _span, _emit| TraitConstraint {
typ,
trait_name,
trait_generics,
});

keyword(Keyword::Where)
Expand Down Expand Up @@ -1470,27 +1471,74 @@ mod test {
let message = format!("Failed to parse:\n{}", program);
let (op_t, errors) = parse_recover(&parser, program);
for e in errors {
if !e.is_warrning() {
panic!("{}", &message);
if !e.is_warning() {
panic!("{} with error {}", &message, e);
}
}
op_t.expect(&message)
})
}

fn parse_all_warnings<P, T>(parser: P, programs: Vec<&str>) -> Vec<CustomDiagnostic>
where
P: NoirParser<T>,
T: std::fmt::Display,
{
programs
.into_iter()
.flat_map(|program| match parse_recover(&parser, program) {
(None, errors) => errors,
(Some(expr), errors) => {
let mut has_errors = false;
for e in &errors {
if e.is_error() {
has_errors = true;
}
}
match has_errors {
true => {
unreachable!(
"Expected this input to pass with warning:\n{}\nYet it successfully failed with error:\n{}",
program, expr
)
}
false => {}
}
errors

yordanmadzhunkov marked this conversation as resolved.
Show resolved Hide resolved
}
})
.collect()
}

fn parse_all_failing<P, T>(parser: P, programs: Vec<&str>) -> Vec<CustomDiagnostic>
where
P: NoirParser<T>,
T: std::fmt::Display,
{
programs
.into_iter()
.flat_map(|program| match parse_with(&parser, program) {
Ok(expr) => unreachable!(
"Expected this input to fail:\n{}\nYet it successfully parsed as:\n{}",
program, expr
),
Err(error) => error,
.flat_map(|program| match parse_recover(&parser, program) {
yordanmadzhunkov marked this conversation as resolved.
Show resolved Hide resolved
yordanmadzhunkov marked this conversation as resolved.
Show resolved Hide resolved
(None, errors) => errors,
(Some(expr), errors) => {
let mut has_errors = false;
for e in &errors {
if e.is_error() {
has_errors = true;
}
}
match has_errors {
false => {
unreachable!(
"Expected this input to fail:\n{}\nYet it successfully parsed as:\n{}",
program, expr
)
}
true => {}
}
errors

}
})
.collect()
}
Expand Down Expand Up @@ -1679,7 +1727,7 @@ mod test {
// The first (inner) `==` is a predicate which returns 0/1
// The outer layer is an infix `==` which is
// associated with the Constrain statement
let errors = parse_all_failing(
let errors = parse_all_warnings(
jfecher marked this conversation as resolved.
Show resolved Hide resolved
constrain(expression()),
vec![
"constrain ((x + y) == k) + z == y",
Expand All @@ -1690,7 +1738,7 @@ mod test {
],
);
assert_eq!(errors.len(), 5);
assert!(errors.iter().all(|err| { format!("{}", err).contains("deprecated") }));
assert!(errors.iter().all(|err| { err.is_warning() && format!("{}", err).contains("deprecated") }))
}

/// This is the standard way to declare an assert statement
Expand Down Expand Up @@ -1784,6 +1832,8 @@ mod test {
"fn f(f: pub Field, y : Field, z : comptime Field) -> u8 { x + a }",
"fn f<T>(f: pub Field, y : T, z : comptime Field) -> u8 { x + a }",
"fn func_name<T>(f: Field, y : T) where T: SomeTrait {}",
// The following should produce compile error on later stage. From the parser prespective it's fine
yordanmadzhunkov marked this conversation as resolved.
Show resolved Hide resolved
"fn func_name<A>(f: Field, y : pub Field, z : pub [u8;5]) where T: SomeTrait {}",
yordanmadzhunkov marked this conversation as resolved.
Show resolved Hide resolved
],
);

Expand All @@ -1798,9 +1848,6 @@ mod test {
"fn func_name<T>(f: Field, y : pub Field, z : pub [u8;5],) where SomeTrait {}",
"fn func_name<T>(f: Field, y : pub Field, z : pub [u8;5],) SomeTrait {}",
"fn func_name(f: Field, y : pub Field, z : pub [u8;5],) where T: SomeTrait {}",
// TODO(GenericParameterNotFoundInFunction)
// Consider making this a compilation error:
// "fn func_name<A>(f: Field, y : pub Field, z : pub [u8;5],) where T: SomeTrait {}",
],
);
}
Expand All @@ -1820,6 +1867,9 @@ mod test {
"trait TraitWithAssociatedType { type Element; fn item(self, index: Field) -> Self::Element; }",
"trait TraitWithAssociatedConstant { let Size: Field; }",
"trait TraitWithAssociatedConstantWithDefaultValue { let Size: Field = 10; }",
"trait GenericTrait<T> { fn elem(&mut self, index: Field) -> T; }",
"trait GenericTraitWithConstraints<T> where T: SomeTrait { fn elem(self, index: Field) -> T; }",
"trait TraitWithMultipleGenericParams<A, B, C> where A: SomeTrait, B: AnotherTrait<C> { let Size: Field; fn zero() -> Self; }",
],
);

Expand All @@ -1829,11 +1879,6 @@ mod test {
"trait MissingBody",
"trait WrongDelimiter { fn foo() -> u8, fn bar() -> u8 }",
"trait WhereClauseWithoutGenerics where A: SomeTrait { }",
// TODO: when implemnt generics in traits the following 3 should pass
"trait GenericTrait<T> { fn elem(&mut self, index: Field) -> T; }",
"trait GenericTraitWithConstraints<T> where T: SomeTrait { fn elem(self, index: Field) -> T; }",
"trait TraitWithMultipleGenericParams<A, B, C> where A: SomeTrait, B: AnotherTrait<C> { comptime Size: Field; fn zero() -> Self; }",

],
);
}
Expand Down