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

chore: easier way to test monormophization errors #7679

Merged
merged 3 commits into from
Mar 12, 2025
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
35 changes: 22 additions & 13 deletions compiler/noirc_frontend/src/monomorphization/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![cfg(test)]
use crate::tests::get_program;
use crate::{
elaborator::UnstableFeature,
tests::{check_monomorphization_error_using_features, get_program},
};

use super::{ast::Program, errors::MonomorphizationError, monomorphize};

Expand Down Expand Up @@ -29,6 +32,8 @@
let src = "
fn main() {
let _tree: Tree<Tree<Tree<()>>> = Tree::Branch(
^^^^^^^^^^^^ Type `Tree<Tree<()>>` is recursive
~~~~~~~~~~~~ All types in Noir must have a known size at compile-time
Tree::Branch(Tree::Leaf, Tree::Leaf),
Tree::Branch(Tree::Leaf, Tree::Leaf),
);
Expand All @@ -37,10 +42,10 @@
enum Tree<T> {
Branch(T, T),
Leaf,
}";

let error = get_monomorphized(src).unwrap_err();
assert!(matches!(error, MonomorphizationError::RecursiveType { .. }));
}
";
let features = vec![UnstableFeature::Enums];
check_monomorphization_error_using_features(src, &features);
}

#[test]
Expand All @@ -63,17 +68,19 @@
let src = "
fn main() {
let _tree: Opt<OptAlias<()>> = Opt::Some(OptAlias::None);
^^^^^^^^^ Type `Opt<()>` is recursive
~~~~~~~~~ All types in Noir must have a known size at compile-time
}

type OptAlias<T> = Opt<T>;

enum Opt<T> {
Some(T),
None,
}";

let error = get_monomorphized(src).unwrap_err();
assert!(matches!(error, MonomorphizationError::RecursiveType { .. }));
}
";
let features = vec![UnstableFeature::Enums];
check_monomorphization_error_using_features(src, &features);
}

#[test]
Expand All @@ -85,16 +92,18 @@

enum Even {
Zero,
^^^^ Type `Odd` is recursive
~~~~ All types in Noir must have a known size at compile-time
Succ(Odd),

Check warning on line 97 in compiler/noirc_frontend/src/monomorphization/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Succ)
}

enum Odd {
One,
Succ(Even),

Check warning on line 102 in compiler/noirc_frontend/src/monomorphization/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Succ)
}";

let error = get_monomorphized(src).unwrap_err();
assert!(matches!(error, MonomorphizationError::RecursiveType { .. }));
}
";
let features = vec![UnstableFeature::Enums];
check_monomorphization_error_using_features(src, &features);
}

#[test]
Expand Down
61 changes: 54 additions & 7 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,45 @@
/// will produce errors at those locations and with/ those messages.
fn check_errors(src: &str) {
let allow_parser_errors = false;
check_errors_with_options(src, allow_parser_errors, FrontendOptions::test_default());
let monomorphize = false;
check_errors_with_options(
src,
allow_parser_errors,
monomorphize,
FrontendOptions::test_default(),
);
}

fn check_errors_using_features(src: &str, features: &[UnstableFeature]) {
let allow_parser_errors = false;
let mut options = FrontendOptions::test_default();
options.enabled_unstable_features = features;
check_errors_with_options(src, allow_parser_errors, options);
let monomorphize = false;
let options =
FrontendOptions { enabled_unstable_features: features, ..FrontendOptions::test_default() };
check_errors_with_options(src, allow_parser_errors, monomorphize, options);
}

#[allow(unused)]
pub(super) fn check_monomorphization_error(src: &str) {
check_monomorphization_error_using_features(src, &[]);
}

pub(super) fn check_monomorphization_error_using_features(src: &str, features: &[UnstableFeature]) {
let allow_parser_errors = false;
let monomorphize = true;
check_errors_with_options(
src,
allow_parser_errors,
monomorphize,
FrontendOptions { enabled_unstable_features: features, ..FrontendOptions::test_default() },
);
}

fn check_errors_with_options(src: &str, allow_parser_errors: bool, options: FrontendOptions) {
fn check_errors_with_options(
src: &str,
allow_parser_errors: bool,
monomorphize: bool,
options: FrontendOptions,
) {
let lines = src.lines().collect::<Vec<_>>();

// Here we'll hold just the lines that are code
Expand Down Expand Up @@ -213,12 +241,31 @@
secondary_spans_with_errors.into_iter().collect();

let src = code_lines.join("\n");
let (_, _, errors) = get_program_with_options(&src, allow_parser_errors, options);
let (_, mut context, errors) = get_program_with_options(&src, allow_parser_errors, options);
let mut errors = errors.iter().map(CustomDiagnostic::from).collect::<Vec<_>>();

if monomorphize {
if !errors.is_empty() {
panic!("Expected no errors before monomorphization, got: {:?}", errors);
}

let main = context.get_main_function(context.root_crate_id()).unwrap_or_else(|| {
panic!("get_monomorphized: test program contains no 'main' function")
});

let result = crate::monomorphization::monomorphize(main, &mut context.def_interner, false);
match result {
Ok(_) => panic!("Expected a monomorphization error but got none"),
Err(error) => {
errors.push(error.into());
}
}
}

if errors.is_empty() && !primary_spans_with_errors.is_empty() {
panic!("Expected some errors but got none");
}

let errors = errors.iter().map(CustomDiagnostic::from).collect::<Vec<_>>();
for error in &errors {
let secondary = error
.secondaries
Expand Down Expand Up @@ -290,7 +337,7 @@

let chars = line.chars().collect::<Vec<_>>();
let first_caret = chars.iter().position(|c| *c == char).unwrap();
let last_caret = chars.iter().rposition(|c| *c == char).unwrap();

Check warning on line 340 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (rposition)
let start = byte - last_line_length;
let span = Span::from((start + first_caret - 1) as u32..(start + last_caret) as u32);
let error = line.trim().trim_start_matches(char).trim().to_string();
Expand Down Expand Up @@ -2971,7 +3018,7 @@
// wouldn't panic due to infinite recursion, but the errors asserted here
// come from the compilation checks, which does static analysis to catch the
// problem before it even has a chance to cause a panic.
let srcs = vec![

Check warning on line 3021 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
r#"
fn main() {
^^^^ function `main` cannot return without recursing
Expand Down Expand Up @@ -3053,14 +3100,14 @@
"#,
];

for src in srcs {

Check warning on line 3103 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
check_errors(src);
}
}

#[test]
fn unconditional_recursion_pass() {
let srcs = vec![

Check warning on line 3110 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
r#"
fn main() {
if false { main(); }
Expand Down Expand Up @@ -3102,7 +3149,7 @@
"#,
];

for src in srcs {

Check warning on line 3152 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
assert_no_errors(src);
}
}
Expand Down Expand Up @@ -4088,12 +4135,12 @@
let mut array = [1, 2, 3];
borrow(&array);
^^^^^^ This requires the unstable feature 'ownership' which is not enabled
~~~~~~ Pass -Zownership to nargo to enable this feature at your own risk.

Check warning on line 4138 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Zownership)
}

fn borrow(_array: &[Field; 3]) {}
^^^^^^^^^^^ This requires the unstable feature 'ownership' which is not enabled
~~~~~~~~~~~ Pass -Zownership to nargo to enable this feature at your own risk.

Check warning on line 4143 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Zownership)
"#;
check_errors(src);
}
Expand Down
Loading