diff --git a/crates/aiken-lang/src/tipo/environment.rs b/crates/aiken-lang/src/tipo/environment.rs index 1b9bb4ba..cd358ee5 100644 --- a/crates/aiken-lang/src/tipo/environment.rs +++ b/crates/aiken-lang/src/tipo/environment.rs @@ -18,7 +18,7 @@ use crate::{ }; use super::{ - error::{Error, Warning}, + error::{Error, Snippet, Warning}, hydrator::Hydrator, AccessorsMap, PatternConstructor, RecordAccessor, Type, TypeConstructor, TypeInfo, TypeVar, ValueConstructor, ValueConstructorVariant, @@ -867,26 +867,41 @@ impl<'a> Environment<'a> { Some(e) => { let known_types_after = names.keys().copied().collect::>(); if known_types_before == known_types_after { - let cycle = remaining_definitions + let unknown_name = match e { + Error::UnknownType { ref name, .. } => name, + _ => "", + }; + let mut is_cyclic = false; + let unknown_types = remaining_definitions .into_iter() .filter_map(|def| match def { Definition::TypeAlias(TypeAlias { alias, location, .. - }) => Some((alias.to_owned(), location.to_owned())), - _ => None, + }) => { + is_cyclic = is_cyclic || alias == unknown_name; + Some(Snippet { + location: location.to_owned(), + }) + } + Definition::DataType(DataType { name, location, .. }) => { + is_cyclic = is_cyclic || name == unknown_name; + Some(Snippet { + location: location.to_owned(), + }) + } + Definition::Fn { .. } + | Definition::Use { .. } + | Definition::ModuleConstant { .. } + | Definition::Test { .. } => None, }) - .collect::>(); - match cycle.first() { - None => Err(e), - Some((alias, location)) => { - let mut types = - cycle.iter().map(|def| def.0.clone()).collect::>(); - types.push(alias.clone()); - Err(Error::CyclicTypeDefinitions { - location: *location, - types, - }) - } + .collect::>(); + + if is_cyclic { + Err(Error::CyclicTypeDefinitions { + errors: unknown_types, + }) + } else { + Err(e) } } else { self.register_types(remaining_definitions, module, hydrators, names)