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: ban self-referential structs #4883

Merged
merged 20 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
136be06
wip adding test for self-referential structs, test case failing with ICE
michaeljklein Apr 18, 2024
b85f581
add error for self-referential struct, add btree set of currently-res…
michaeljklein Apr 22, 2024
9a8ac12
cargo fmt/clippt
michaeljklein Apr 22, 2024
380fb0a
remove duplicated test, revert debugging changes to node interner, re…
michaeljklein Apr 22, 2024
f8d2b61
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 22, 2024
6f6697d
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 23, 2024
f21962d
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 23, 2024
5fde15e
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 23, 2024
2a82fa3
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 24, 2024
e5e5f3a
use StructId instead of Ident to identify structs
michaeljklein Apr 24, 2024
513eaec
Update compiler/noirc_frontend/src/hir/resolution/resolver.rs
jfecher Apr 25, 2024
e90ab08
Update compiler/noirc_frontend/src/hir/resolution/resolver.rs
jfecher Apr 25, 2024
2225c66
Update compiler/noirc_frontend/src/hir/resolution/resolver.rs
jfecher Apr 25, 2024
b452a99
Update compiler/noirc_frontend/src/hir/resolution/resolver.rs
jfecher Apr 25, 2024
3589377
Update compiler/noirc_frontend/src/hir/resolution/resolver.rs
jfecher Apr 25, 2024
fd8a926
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 25, 2024
f75becc
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 26, 2024
9fb4d91
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 26, 2024
6a2d10c
add explanation of BtreeSet in defintion of resolving_ids
michaeljklein Apr 26, 2024
cb00fef
Merge branch 'master' into michaeljklein/ban-rec-struct
michaeljklein Apr 26, 2024
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
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub enum ResolverError {
JumpInConstrainedFn { is_break: bool, span: Span },
#[error("break/continue are only allowed within loops")]
JumpOutsideLoop { is_break: bool, span: Span },
#[error("Self-referential structs are not supported")]
SelfReferentialStruct { span: Span },
}

impl ResolverError {
Expand Down Expand Up @@ -340,6 +342,13 @@ impl From<ResolverError> for Diagnostic {
span,
)
},
ResolverError::SelfReferentialStruct { span } => {
Diagnostic::simple_error(
"Self-referential structs are not supported".into(),
"".into(),
span,
)
},
}
}
}
17 changes: 16 additions & 1 deletion compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::hir_def::traits::{Trait, TraitConstraint};
use crate::macros_api::SecondaryAttribute;
use crate::token::{Attributes, FunctionAttribute};
use regex::Regex;
use std::collections::{BTreeMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::rc::Rc;

use crate::ast::{
Expand Down Expand Up @@ -96,6 +96,9 @@ pub struct Resolver<'a> {
/// Used to link items to their dependencies in the dependency graph
current_item: Option<DependencyId>,

/// In-resolution names
resolving_names: BTreeSet<Ident>,
michaeljklein marked this conversation as resolved.
Show resolved Hide resolved

/// True if the current module is a contract.
/// This is usually determined by self.path_resolver.module_id(), but it can
/// be overridden for impls. Impls are an odd case since the methods within resolve
Expand Down Expand Up @@ -159,6 +162,7 @@ impl<'a> Resolver<'a> {
lambda_stack: Vec::new(),
current_trait_impl: None,
current_item: None,
resolving_names: BTreeSet::new(),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
file,
in_contract,
in_unconstrained_fn: false,
Expand Down Expand Up @@ -616,6 +620,14 @@ impl<'a> Resolver<'a> {

match self.lookup_struct_or_error(path) {
Some(struct_type) => {
if self.resolving_names.contains(&struct_type.borrow().name) {
self.push_err(ResolverError::SelfReferentialStruct {
span: struct_type.borrow().name.span(),
});

return Type::Error;
}

let expected_generic_count = struct_type.borrow().generics.len();
if !self.in_contract
&& self
Expand Down Expand Up @@ -886,7 +898,10 @@ impl<'a> Resolver<'a> {
self.resolve_local_globals();

self.current_item = Some(DependencyId::Struct(struct_id));

self.resolving_names.insert(unresolved.name.clone());
let fields = vecmap(unresolved.fields, |(ident, typ)| (ident, self.resolve_type(typ)));
self.resolving_names.remove(&unresolved.name);

(generics, fields, self.errors)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ fn lambda$f1(mut env$l1: (Field)) -> Field {
}

#[test]
fn deny_cyclic_structs() {
fn deny_mutually_recursive_structs() {
let src = r#"
struct Foo { bar: Bar }
struct Bar { foo: Foo }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "self_referential_struct"
type = "bin"
authors = [""]
compiler_version = ">=0.27.0"

[dependencies]
11 changes: 11 additions & 0 deletions test_programs/compile_failure/self_referential_struct/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct Option2<T> {
_is_some: bool,
_value: T,
}

struct SelfReferential
{
prop : Option2<SelfReferential>
}

fn main(x: SelfReferential) { assert(x._is_some); }
Loading