diff --git a/base/src/ast.rs b/base/src/ast.rs index 5e43798be1..a56189b250 100644 --- a/base/src/ast.rs +++ b/base/src/ast.rs @@ -120,6 +120,7 @@ impl<'ast, Id> TypePtr for AstType<'ast, Id> { type Types = &'ast mut [AstType<'ast, Id>]; type Generics = &'ast mut [Generic]; type Fields = &'ast mut [Field]; + type TypeFields = &'ast mut [Field>]; } pub trait HasMetadata { @@ -906,7 +907,7 @@ pub fn walk_ast_type<'a, 'ast, V: ?Sized + $trait_name<'a, 'ast>>( ref $($mut)* types, ref $($mut)* rest, } => { - for field in types { + for field in & $($mut)* **types { if let Some(alias) = field.typ.$try_get_alias() { v.visit_ast_type(&$($mut)* alias.$unresolved_type()._typ.typ); } @@ -1210,6 +1211,7 @@ impl_ast_arena! { AstType<'ast, Id> => type_ptrs, Generic => generics, Field> => type_fields, + Field>> => type_type_fields, } pub struct RootSpannedExpr { diff --git a/base/src/types/mod.rs b/base/src/types/mod.rs index d19480fb4f..e138738823 100644 --- a/base/src/types/mod.rs +++ b/base/src/types/mod.rs @@ -119,7 +119,7 @@ impl<'a, T: ?Sized + PrimitiveEnv> PrimitiveEnv for &'a T { type_cache! { TypeCache(Id, T) - where [T: TypePtr>, Fields = Vec>>, T::Types: Default + Extend,] + where [T: TypeExt, T::Types: Default + Extend,] (kind_cache: crate::kind::KindCache) { T, Type } hole opaque error int byte float string char @@ -128,9 +128,7 @@ type_cache! { impl TypeCache where - T: TypePtr>, Fields = Vec>> - + From> - + Clone, + T: TypeExt + From> + Clone, T::Types: Default + Extend, { pub fn function(&self, args: I, ret: T) -> T @@ -200,7 +198,7 @@ where impl TypeCache where - T: TypePtr>, Fields = Vec>> + Clone, + T: TypeExt + Clone, T::Types: Default + Extend + FromIterator, { pub fn builtin_type(&self, typ: BuiltinType) -> T { @@ -390,8 +388,7 @@ where impl From> for Alias where - T: TypePtr>, Fields = Vec>> - + From>, + T: TypeExt + From>, T::Types: Clone + Default + Extend, { fn from(data: AliasData) -> Alias { @@ -439,8 +436,7 @@ where impl Alias where - T: TypePtr>, Fields = Vec>> - + From>, + T: TypeExt + From>, T::Types: Clone + Default + Extend, { pub fn new(name: Id, args: Vec>, typ: T) -> Alias { @@ -811,8 +807,7 @@ impl Field { where J: IntoIterator, J::IntoIter: DoubleEndedIterator, - T: TypePtr>, Fields = Vec>> - + From>, + T: TypeExt + From>, T::Types: Default + Extend, { let typ = Type::function_type(ArgType::Constructor, elems, Type::opaque()); @@ -858,13 +853,17 @@ impl Default for ArgType { T::Types: Default + Extend, T::Generics: Default + Extend>, T::Fields: DeserializeState<'de, Seed>, + T::TypeFields: DeserializeState<'de, Seed>, Id: DeserializeState<'de, Seed> + Clone - + std::any::Any", + + std::any::Any, + ", serialize = " T: TypePtr + SerializeState, Id: SerializeState, - T::Fields: SerializeState" + T::Fields: SerializeState, + T::TypeFields: SerializeState, + " )) )] #[cfg_attr(feature = "serde_derive", serde(serialize_state = "SeSeed"))] @@ -923,7 +922,7 @@ pub enum Type = ArcType> { ExtendTypeRow { /// The associated types of this record type #[cfg_attr(feature = "serde_derive", serde(state))] - types: Vec>>, + types: T::TypeFields, /// The rest of the row #[cfg_attr(feature = "serde_derive", serde(state))] rest: T, @@ -979,8 +978,7 @@ where impl Type where - T: From> - + TypePtr>, Fields = Vec>>, + T: From> + TypeExt, T::Types: Default + Extend, { pub fn hole() -> T { @@ -1632,11 +1630,17 @@ type_alloc_impl! { T ['ast, T: TypePtr] &'ast mut [T], T => intern type_alloc_impl! { T [T: TypePtr] Vec::Id, T>>, Field<::Id, T> => intern_fields } type_alloc_impl! { T ['ast, T: TypePtr] &'ast mut [Field<::Id, T>], Field<::Id, T> => intern_fields } +type_alloc_impl! { T [T: TypePtr] Vec::Id, Alias<::Id, T>>>, Field<::Id, Alias<::Id, T>> => intern_type_fields } +type_alloc_impl! { T ['ast, T: TypePtr] &'ast mut [Field<::Id, Alias<::Id, T>>], Field<::Id, Alias<::Id, T>> => intern_type_fields } + pub trait TypePtr: Deref::Id, Self>> + Sized { type Id; type Types: TypeAlloc + Deref + Default; type Generics: TypeAlloc + Deref]> + Default; type Fields: TypeAlloc + Deref]> + Default; + type TypeFields: TypeAlloc + + Deref>]> + + Default; fn flags(&self) -> Flags { Flags::all() @@ -1655,6 +1659,7 @@ pub trait TypeExt: Types = AppVec, Generics = Vec::Id>>, Fields = Vec::Id, Self>>, + TypeFields = Vec::Id, Alias<::Id, Self>>>, > + Clone + Sized { @@ -1993,16 +1998,14 @@ impl TypePtr for ArcType { type Types = AppVec; type Generics = Vec>; type Fields = Vec>; + type TypeFields = Vec>>; fn flags(&self) -> Flags { self.typ.flags } } -impl TypeExt for ArcType -where - Id: PartialEq, -{ +impl TypeExt for ArcType { fn strong_count(typ: &ArcType) -> usize { Arc::strong_count(&typ.typ) } @@ -2955,7 +2958,7 @@ where ref types, ref rest, } => { - for field in types { + for field in &**types { f.walk(&field.typ.typ); } f.walk(rest); @@ -2974,12 +2977,13 @@ where } } -pub fn walk_type_mut(typ: &mut T, f: &mut F) +pub fn walk_type_mut(typ: &mut T, f: &mut F) where F: WalkerMut, - T: TypePtr + DerefMut>, + T: TypePtr + DerefMut>, T::Types: DerefMut, - T::Fields: DerefMut]>, + T::Fields: DerefMut]>, + T::TypeFields: DerefMut>]>, { match **typ { Type::Forall(_, ref mut typ) => f.walk_mut(typ), @@ -3009,7 +3013,7 @@ where ref mut types, ref mut rest, } => { - for field in types { + for field in &mut **types { if let Some(alias) = field.typ.try_get_alias_mut() { let field_type = alias.unresolved_type_mut(); f.walk_mut(field_type); @@ -3049,6 +3053,7 @@ where T::Types: Clone, T::Generics: Clone, T::Fields: Clone, + T::TypeFields: Clone, { type Context: TypeContext; @@ -3094,7 +3099,8 @@ pub trait Walker<'a, T> { impl TypeVisitor for F where F: ?Sized + FnMut(&T) -> Option, - T: TypePtr + From<(Type, Flags)> + From>, + Id: Clone, + T: TypeExt + From<(Type, Flags)> + From>, T::Types: FromIterator + Clone, T::Generics: FromIterator> + Clone, T::Fields: FromIterator> + Clone, @@ -3156,6 +3162,10 @@ macro_rules! forward_type_interner_methods { $crate::expr!(self, $($tokens)+).intern_fields(types) } + fn intern_type_fields(&mut self, types: impl IntoIterator>>) -> T::TypeFields { + $crate::expr!(self, $($tokens)+).intern_type_fields(types) + } + fn intern_flags(&mut self, typ: $crate::types::Type<$id, $typ>, flags: $crate::types::Flags) -> $typ { $crate::expr!(self, $($tokens)+).intern_flags(typ, flags) } @@ -3203,7 +3213,7 @@ macro_rules! forward_type_interner_methods { fn record( &mut self, - types: Vec<$crate::types::Field<$id, $crate::types::Alias<$id, $typ>>>, + types: <$typ as $crate::types::TypePtr>::TypeFields, fields: <$typ as $crate::types::TypePtr>::Fields, ) -> $typ { $crate::expr!(self, $($tokens)+).record(types, fields) @@ -3211,7 +3221,7 @@ macro_rules! forward_type_interner_methods { fn poly_record( &mut self, - types: Vec<$crate::types::Field<$id, $crate::types::Alias<$id, $typ>>>, + types: <$typ as $crate::types::TypePtr>::TypeFields, fields: <$typ as $crate::types::TypePtr>::Fields, rest: $typ, ) -> $typ { @@ -3220,7 +3230,7 @@ macro_rules! forward_type_interner_methods { fn extend_full_row( &mut self, - types: Vec<$crate::types::Field<$id, $crate::types::Alias<$id, $typ>>>, + types: <$typ as $crate::types::TypePtr>::TypeFields, fields: <$typ as $crate::types::TypePtr>::Fields, rest: $typ, ) -> $typ { @@ -3237,7 +3247,7 @@ macro_rules! forward_type_interner_methods { fn extend_type_row( &mut self, - types: Vec<$crate::types::Field<$id, $crate::types::Alias<$id, $typ>>>, + types: <$typ as $crate::types::TypePtr>::TypeFields, rest: $typ, ) -> $typ { $crate::expr!(self, $($tokens)+).extend_type_row(types, rest) @@ -3362,6 +3372,11 @@ where fn intern_fields(&mut self, types: impl IntoIterator>) -> T::Fields; + fn intern_type_fields( + &mut self, + types: impl IntoIterator>>, + ) -> T::TypeFields; + fn hole(&mut self) -> T { self.intern(Type::Hole) } @@ -3456,27 +3471,17 @@ where Type::Record(self.extend_row(elems, empty_row)) } - fn record(&mut self, types: Vec>>, fields: T::Fields) -> T { + fn record(&mut self, types: T::TypeFields, fields: T::Fields) -> T { let empty_row = self.empty_row(); self.poly_record(types, fields, empty_row) } - fn poly_record( - &mut self, - types: Vec>>, - fields: T::Fields, - rest: T, - ) -> T { + fn poly_record(&mut self, types: T::TypeFields, fields: T::Fields, rest: T) -> T { let row = self.extend_full_row(types, fields, rest); self.intern(Type::Record(row)) } - fn extend_full_row( - &mut self, - types: Vec>>, - fields: T::Fields, - rest: T, - ) -> T { + fn extend_full_row(&mut self, types: T::TypeFields, fields: T::Fields, rest: T) -> T { let rest = self.extend_row(fields, rest); self.extend_type_row(types, rest) } @@ -3489,7 +3494,7 @@ where } } - fn extend_type_row(&mut self, types: Vec>>, rest: T) -> T { + fn extend_type_row(&mut self, types: T::TypeFields, rest: T) -> T { if types.is_empty() { rest } else { @@ -3692,6 +3697,7 @@ where T::Types: FromIterator, T::Generics: FromIterator>, T::Fields: FromIterator>, + T::TypeFields: FromIterator>>, { fn intern(&mut self, typ: Type) -> T { T::from(typ) @@ -3709,6 +3715,13 @@ where types.into_iter().collect() } + fn intern_type_fields( + &mut self, + types: impl IntoIterator>>, + ) -> T::TypeFields { + types.into_iter().collect() + } + fn intern_flags(&mut self, typ: Type, flags: Flags) -> T { T::from((typ, flags)) } @@ -3726,13 +3739,11 @@ macro_rules! forward_to_cache { impl TypeContext for TypeCache where - T: TypePtr>, Fields = Vec>> - + From<(Type, Flags)> - + From> - + Clone, + T: TypeExt + From<(Type, Flags)> + From> + Clone, T::Types: Default + Extend + FromIterator, T::Generics: FromIterator>, T::Fields: FromIterator>, + T::TypeFields: FromIterator>>, { fn intern(&mut self, typ: Type) -> T { T::from(typ) @@ -3750,6 +3761,13 @@ where types.into_iter().collect() } + fn intern_type_fields( + &mut self, + types: impl IntoIterator>>, + ) -> T::TypeFields { + types.into_iter().collect() + } + fn intern_flags(&mut self, typ: Type, flags: Flags) -> T { T::from((typ, flags)) } @@ -3762,13 +3780,11 @@ where impl<'a, Id, T> TypeContext for &'a TypeCache where - T: TypePtr>, Fields = Vec>> - + From<(Type, Flags)> - + From> - + Clone, + T: TypeExt + From<(Type, Flags)> + From> + Clone, T::Types: Default + Extend + FromIterator, T::Generics: FromIterator>, T::Fields: FromIterator>, + T::TypeFields: FromIterator>>, { fn intern(&mut self, typ: Type) -> T { T::from(typ) @@ -3786,6 +3802,13 @@ where types.into_iter().collect() } + fn intern_type_fields( + &mut self, + types: impl IntoIterator>>, + ) -> T::TypeFields { + types.into_iter().collect() + } + fn intern_flags(&mut self, typ: Type, flags: Flags) -> T { T::from((typ, flags)) } @@ -3823,6 +3846,13 @@ impl<'ast, Id> TypeContext> for ArenaRef<'_, 'ast, Id> { self.alloc_extend(types) } + fn intern_type_fields( + &mut self, + types: impl IntoIterator>>>, + ) -> &'ast mut [Field>>] { + self.alloc_extend(types) + } + fn intern_flags( &mut self, typ: Type>, @@ -3896,7 +3926,7 @@ impl TypeContextAlloc for ArcType { pub struct Interner where - T: TypePtr>, Fields = Vec>>, + T: TypeExt, T::Types: Default + Extend + FromIterator, { set: FnvMap, ()>, @@ -3906,10 +3936,7 @@ where impl Default for Interner where - T: Default - + TypePtr>, Fields = Vec>> - + From> - + Clone, + T: Default + TypeExt + From> + Clone, T::Target: Eq + Hash, T::Types: Default + Extend + FromIterator, { @@ -3953,6 +3980,7 @@ where T: TypeContextAlloc + TypeExt + Eq + Hash + Clone, T::Types: FromIterator, T::Generics: FromIterator>, + T::TypeFields: FromIterator>>, Id: Eq + Hash, { fn intern(&mut self, typ: Type) -> T { @@ -3972,6 +4000,13 @@ where types.into_iter().collect() } + fn intern_type_fields( + &mut self, + types: impl IntoIterator>>, + ) -> T::TypeFields { + types.into_iter().collect() + } + fn intern_flags(&mut self, typ: Type, flags: Flags) -> T { use std::collections::hash_map::Entry; @@ -3992,23 +4027,23 @@ where } impl<'i, F, V> InternerVisitor<'i, F, V> { - pub fn new(interner: &'i mut V, visitor: F) -> Self + pub fn new(interner: &'i mut V, visitor: F) -> Self where F: FnMut(&mut V, &T) -> Option, - T: TypeExt, - V: TypeContext, + T: TypeExt, + V: TypeContext, { InternerVisitor { interner, visitor } } - pub fn control( + pub fn control( interner: &'i mut V, visitor: F, ) -> InternerVisitor<'i, ControlVisitation, V> where F: FnMut(&mut V, &T) -> Option, - T: TypeExt, - V: TypeContext, + T: TypeExt, + V: TypeContext, { InternerVisitor { interner, @@ -4020,6 +4055,7 @@ impl<'i, F, V> InternerVisitor<'i, F, V> { impl<'i, F, V, Id, T> TypeVisitor for InternerVisitor<'i, F, V> where F: FnMut(&mut V, &T) -> Option, + Id: Clone, T: TypeExt>, V: TypeContext, T::Generics: FromIterator> + Clone, @@ -4042,11 +4078,12 @@ where } } -impl<'i, F, V, I, T> TypeVisitor for InternerVisitor<'i, ControlVisitation, V> +impl<'i, F, V, Id, T> TypeVisitor for InternerVisitor<'i, ControlVisitation, V> where F: FnMut(&mut V, &T) -> Option, - T: TypeExt, - V: TypeContext, + Id: Clone, + T: TypeExt, + V: TypeContext, T::Types: Clone, T::Generics: Clone, T::Fields: Clone, @@ -4059,8 +4096,8 @@ where fn visit(&mut self, typ: &T) -> Option where - T: TypePtr + Clone, - I: Clone, + T: TypePtr + Clone, + Id: Clone, { (self.visitor.0)(self.interner, typ) } @@ -4072,7 +4109,8 @@ pub struct ControlVisitation(pub F); impl TypeVisitor for ControlVisitation where F: FnMut(&T) -> Option, - T: TypePtr + From<(Type, Flags)> + From>, + Id: Clone, + T: TypeExt + From<(Type, Flags)> + From>, T::Types: FromIterator + Clone, T::Generics: FromIterator> + Clone, T::Fields: FromIterator> + Clone, @@ -4107,7 +4145,8 @@ pub struct FlagsVisitor(pub Flags, pub F); impl TypeVisitor for FlagsVisitor where F: FnMut(&T) -> Option, - T: TypePtr + From<(Type, Flags)> + From>, + Id: Clone, + T: TypeExt + From<(Type, Flags)> + From>, T::Types: FromIterator + Clone, T::Generics: FromIterator> + Clone, T::Fields: FromIterator> + Clone, @@ -4168,6 +4207,7 @@ where T: TypePtr + DerefMut>, T::Types: DerefMut, T::Fields: DerefMut]>, + T::TypeFields: DerefMut>]>, { fn walk_mut(&mut self, typ: &mut T) { self(typ); @@ -4183,6 +4223,7 @@ where T::Types: Clone, T::Generics: Clone, T::Fields: Clone, + T::TypeFields: Clone, I: Clone, { f.visit(&typ).unwrap_or(typ) @@ -4195,6 +4236,7 @@ where T::Types: Clone, T::Generics: Clone, T::Fields: Clone, + T::TypeFields: Clone, I: Clone, { f.visit(typ) @@ -4207,6 +4249,7 @@ where T::Types: Clone, T::Generics: Clone, T::Fields: Clone, + T::TypeFields: Clone, I: Clone, { match *typ { @@ -4382,7 +4425,8 @@ where _marker: PhantomData, }, }) - .collect(); + .collect::>(); + let types = interner.intern_type_fields(types); let rest = translate(interner, rest); interner.extend_type_row(types, rest) diff --git a/parser/src/grammar.lalrpop b/parser/src/grammar.lalrpop index 7bc453a166..9417e2c778 100644 --- a/parser/src/grammar.lalrpop +++ b/parser/src/grammar.lalrpop @@ -1,3 +1,5 @@ +use std::{str::FromStr, mem}; + use crate::itertools::{Either, Itertools}; use crate::base::{ @@ -9,8 +11,6 @@ use crate::base::{ metadata::{Attribute, Metadata, Comment}, }; -use std::{str::FromStr, mem}; - use crate::{ReplLine, Variant, new_ident}; use crate::token::{Token, StringLiteral}; use crate::ordered_float::NotNan; @@ -494,6 +494,7 @@ AtomicType_: Type> = { } Either::Right(r) => Some(r), })); + let types = arena.alloc_extend(types); Type::Record(arena.clone().extend_full_row( types, fields, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index f0b9187bb4..8293fce184 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -330,6 +330,7 @@ impl_temp_vec! { ast::InnerAstType<'ast, Id> => types, AstType<'ast, Id> => type_ptrs, Field> => type_fields, + Field>> => type_type_fields, Either>>, Field>> => either_type_fields, }