-
Notifications
You must be signed in to change notification settings - Fork 375
fix: Error on infinitely recursive types #7579
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
258df9b
Error on infinitely recursive types
jfecher 60cedac
Remove duplicate get_monomorphized test functions
jfecher 34e6507
Merge branch 'master' into jf/recursive-type-error
jfecher 30df4e0
reuse existing self referential type check
jfecher 990038e
Remove old test
jfecher ac89789
Merge branch 'master' into jf/recursive-type-error
0b02c0c
Add tests
jfecher 1603cf0
Merge branch 'jf/recursive-type-error' of https://github.com/noir-lan…
jfecher c7d6abe
Fix outdated comments
jfecher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #![cfg(test)] | ||
| use crate::tests::get_program; | ||
|
|
||
| use super::{ast::Program, errors::MonomorphizationError, monomorphize}; | ||
|
|
||
| pub fn get_monomorphized(src: &str) -> Result<Program, MonomorphizationError> { | ||
| let (_parsed_module, mut context, errors) = get_program(src); | ||
| assert!( | ||
| errors.iter().all(|err| !err.is_error()), | ||
| "Expected monomorphized program to have no errors before monomorphization, but found: {errors:?}" | ||
| ); | ||
|
|
||
| let main = context | ||
| .get_main_function(context.root_crate_id()) | ||
| .unwrap_or_else(|| panic!("get_monomorphized: test program contains no 'main' function")); | ||
|
|
||
| monomorphize(main, &mut context.def_interner, false) | ||
| } | ||
|
|
||
| fn check_rewrite(src: &str, expected: &str) { | ||
| let program = get_monomorphized(src).unwrap(); | ||
| assert!(format!("{}", program) == expected); | ||
| } | ||
|
|
||
| #[test] | ||
| fn bounded_recursive_type_errors() { | ||
| // We want to eventually allow bounded recursive types like this, but for now they are | ||
| // disallowed because they cause a panic in convert_type during monomorphization. | ||
| let src = " | ||
| fn main() { | ||
| let _tree: Tree<Tree<Tree<()>>> = Tree::Branch( | ||
| Tree::Branch(Tree::Leaf, Tree::Leaf), | ||
| Tree::Branch(Tree::Leaf, Tree::Leaf), | ||
| ); | ||
| } | ||
|
|
||
| enum Tree<T> { | ||
jfecher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Branch(T, T), | ||
| Leaf, | ||
| }"; | ||
|
|
||
| let error = get_monomorphized(src).unwrap_err(); | ||
| assert!(matches!(error, MonomorphizationError::RecursiveType { .. })); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recursive_type_with_alias_errors() { | ||
| // We want to eventually allow bounded recursive types like this, but for now they are | ||
| // disallowed because they cause a panic in convert_type during monomorphization. | ||
| // | ||
| // In the future we could lower this type to: | ||
| // struct OptOptUnit { | ||
| // is_some: Field, | ||
| // some: OptUnit, | ||
| // none: (), | ||
| // } | ||
| // | ||
| // struct OptUnit { | ||
| // is_some: Field, | ||
| // some: (), | ||
| // none: (), | ||
| // } | ||
| let src = " | ||
| fn main() { | ||
| let _tree: Opt<OptAlias<()>> = Opt::Some(OptAlias::None); | ||
| } | ||
|
|
||
| type OptAlias<T> = Opt<T>; | ||
|
|
||
| enum Opt<T> { | ||
| Some(T), | ||
| None, | ||
| }"; | ||
|
|
||
| let error = get_monomorphized(src).unwrap_err(); | ||
| assert!(matches!(error, MonomorphizationError::RecursiveType { .. })); | ||
| } | ||
|
|
||
| #[test] | ||
| fn mutually_recursive_types_error() { | ||
| let src = " | ||
| fn main() { | ||
| let _zero = Even::Zero; | ||
| } | ||
|
|
||
| enum Even { | ||
| Zero, | ||
| Succ(Odd), | ||
| } | ||
|
|
||
| enum Odd { | ||
| One, | ||
| Succ(Even), | ||
| }"; | ||
|
|
||
| let error = get_monomorphized(src).unwrap_err(); | ||
| assert!(matches!(error, MonomorphizationError::RecursiveType { .. })); | ||
| } | ||
|
|
||
| #[test] | ||
| fn simple_closure_with_no_captured_variables() { | ||
| let src = r#" | ||
| fn main() -> pub Field { | ||
| let x = 1; | ||
| let closure = || x; | ||
| closure() | ||
| } | ||
| "#; | ||
|
|
||
| let expected_rewrite = r#"fn main$f0() -> Field { | ||
| let x$0 = 1; | ||
| let closure$3 = { | ||
| let closure_variable$2 = { | ||
| let env$1 = (x$l0); | ||
| (env$l1, lambda$f1) | ||
| }; | ||
| closure_variable$l2 | ||
| }; | ||
| { | ||
| let tmp$4 = closure$l3; | ||
| tmp$l4.1(tmp$l4.0) | ||
| } | ||
| } | ||
| fn lambda$f1(mut env$l1: (Field)) -> Field { | ||
| env$l1.0 | ||
| } | ||
| "#; | ||
| check_rewrite(src, expected_rewrite); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.