From bde1da584d6de79a7ed3df0cbdb64ed7c12a2681 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 5 Jul 2024 09:38:12 -0300 Subject: [PATCH] fix: incorrect parsing of `dep::{foo, bar}` --- compiler/noirc_frontend/src/parser/parser.rs | 12 ++++++++++++ compiler/noirc_frontend/src/parser/parser/path.rs | 2 +- tooling/nargo_fmt/tests/expected/use_dep_tree.nr | 1 + tooling/nargo_fmt/tests/input/use_dep_tree.nr | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tooling/nargo_fmt/tests/expected/use_dep_tree.nr create mode 100644 tooling/nargo_fmt/tests/input/use_dep_tree.nr diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index afeee889ede..02a8191b2d0 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -1187,6 +1187,7 @@ mod test { use super::test_helpers::*; use super::*; use crate::ast::ArrayLiteral; + use crate::macros_api::PathKind; #[test] fn parse_infix() { @@ -1498,6 +1499,17 @@ mod test { } } + #[test] + fn parse_use_dep() { + let str = "use dep::{foo, bar}"; + let result = parse_with(use_statement(), str); + if let Ok(TopLevelStatement::Import(UseTree { prefix, .. })) = result { + assert_eq!(prefix.kind, PathKind::Dep); + } else { + panic!("Expected a use statement, got {:?}", result); + } + } + #[test] fn parse_type_aliases() { let cases = vec!["type foo = u8", "type bar = String", "type baz = Vec"]; diff --git a/compiler/noirc_frontend/src/parser/parser/path.rs b/compiler/noirc_frontend/src/parser/parser/path.rs index e40268af410..ab7c569ffb2 100644 --- a/compiler/noirc_frontend/src/parser/parser/path.rs +++ b/compiler/noirc_frontend/src/parser/parser/path.rs @@ -25,7 +25,7 @@ fn empty_path() -> impl NoirParser { let make_path = |kind| move |_, span| Path { segments: Vec::new(), kind, span }; let path_kind = |key, kind| keyword(key).map_with_span(make_path(kind)); - choice((path_kind(Keyword::Crate, PathKind::Crate), path_kind(Keyword::Dep, PathKind::Plain))) + choice((path_kind(Keyword::Crate, PathKind::Crate), path_kind(Keyword::Dep, PathKind::Dep))) } pub(super) fn maybe_empty_path() -> impl NoirParser { diff --git a/tooling/nargo_fmt/tests/expected/use_dep_tree.nr b/tooling/nargo_fmt/tests/expected/use_dep_tree.nr new file mode 100644 index 00000000000..863bb4f34e4 --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/use_dep_tree.nr @@ -0,0 +1 @@ +use dep::{foo, bar}; diff --git a/tooling/nargo_fmt/tests/input/use_dep_tree.nr b/tooling/nargo_fmt/tests/input/use_dep_tree.nr new file mode 100644 index 00000000000..679ca36aa0d --- /dev/null +++ b/tooling/nargo_fmt/tests/input/use_dep_tree.nr @@ -0,0 +1 @@ +use dep::{ foo, bar };