From 62da85cc0667fc53b93f76e56a66e3d706c3637b Mon Sep 17 00:00:00 2001 From: Folkert Date: Sun, 4 Apr 2021 23:18:50 +0200 Subject: [PATCH] remove ATTR_ATTR usage --- compiler/mono/src/ir.rs | 17 --- compiler/mono/src/layout.rs | 55 ++-------- compiler/reporting/src/error/type.rs | 1 - compiler/solve/src/solve.rs | 154 ++++----------------------- compiler/types/src/pretty_print.rs | 68 +----------- compiler/types/src/types.rs | 6 +- compiler/unify/src/unify.rs | 1 - 7 files changed, 37 insertions(+), 265 deletions(-) diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index c9430e502c..1957cfaa2f 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -2103,17 +2103,6 @@ fn build_specialized_proc_from_var<'a>( ret_var, ) } - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, args)) - if !pattern_symbols.is_empty() => - { - build_specialized_proc_from_var( - env, - layout_cache, - proc_name, - pattern_symbols, - args[1], - ) - } Content::Alias(_, _, actual) => build_specialized_proc_from_var( env, layout_cache, @@ -7142,12 +7131,6 @@ pub fn num_argument_to_int_or_float( | Content::Alias(Symbol::NUM_AT_UNSIGNED8, _, _) => { IntOrFloat::UnsignedIntType(IntPrecision::I8) } - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, attr_args)) => { - debug_assert!(attr_args.len() == 2); - - // Recurse on the second argument - num_argument_to_int_or_float(subs, ptr_bytes, attr_args[1], false) - } Content::Alias(Symbol::NUM_FLOATINGPOINT, args, _) => { debug_assert!(args.len() == 1); diff --git a/compiler/mono/src/layout.rs b/compiler/mono/src/layout.rs index 41654db7b8..96867cbcdb 100644 --- a/compiler/mono/src/layout.rs +++ b/compiler/mono/src/layout.rs @@ -1454,9 +1454,6 @@ pub fn union_sorted_tags<'a>( fn get_recursion_var(subs: &Subs, var: Variable) -> Option { match subs.get_without_compacting(var).content { Content::Structure(FlatType::RecursiveTagUnion(rec_var, _, _)) => Some(rec_var), - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, args)) => { - get_recursion_var(subs, args[1]) - } Content::Alias(_, _, actual) => get_recursion_var(subs, actual), _ => None, } @@ -1820,18 +1817,6 @@ fn layout_from_num_content<'a>(content: Content) -> Result, LayoutPro fn unwrap_num_tag<'a>(subs: &Subs, var: Variable) -> Result, LayoutProblem> { match subs.get_without_compacting(var).content { - Content::Structure(flat_type) => match flat_type { - FlatType::Apply(Symbol::ATTR_ATTR, args) => { - debug_assert_eq!(args.len(), 2); - - let arg_var = args.get(1).unwrap(); - - unwrap_num_tag(subs, *arg_var) - } - _ => { - todo!("TODO handle Num.@Num flat_type {:?}", flat_type); - } - }, Content::Alias(Symbol::NUM_INTEGER, args, _) => { debug_assert!(args.len() == 1); @@ -1908,37 +1893,20 @@ fn dict_layout_from_key_value<'a>( value_var: Variable, ) -> Result, LayoutProblem> { match env.subs.get_without_compacting(key_var).content { - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, key_args)) => { - debug_assert_eq!(key_args.len(), 2); - - let var = *key_args.get(1).unwrap(); - - dict_layout_from_key_value(env, var, value_var) - } Content::FlexVar(_) | Content::RigidVar(_) => { // If this was still a (Dict * *) then it must have been an empty dict Ok(Layout::Builtin(Builtin::EmptyDict)) } key_content => { - match env.subs.get_without_compacting(value_var).content { - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, value_args)) => { - debug_assert_eq!(value_args.len(), 2); + let value_content = env.subs.get_without_compacting(value_var).content; + let key_layout = Layout::new_help(env, key_var, key_content)?; + let value_layout = Layout::new_help(env, value_var, value_content)?; - let var = *value_args.get(1).unwrap(); - - dict_layout_from_key_value(env, key_var, var) - } - value_content => { - let key_layout = Layout::new_help(env, key_var, key_content)?; - let value_layout = Layout::new_help(env, value_var, value_content)?; - - // This is a normal list. - Ok(Layout::Builtin(Builtin::Dict( - env.arena.alloc(key_layout), - env.arena.alloc(value_layout), - ))) - } - } + // This is a normal list. + Ok(Layout::Builtin(Builtin::Dict( + env.arena.alloc(key_layout), + env.arena.alloc(value_layout), + ))) } } } @@ -1948,13 +1916,6 @@ pub fn list_layout_from_elem<'a>( elem_var: Variable, ) -> Result, LayoutProblem> { match env.subs.get_without_compacting(elem_var).content { - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, args)) => { - debug_assert_eq!(args.len(), 2); - - let var = *args.get(1).unwrap(); - - list_layout_from_elem(env, var) - } Content::FlexVar(_) | Content::RigidVar(_) => { // If this was still a (List *) then it must have been an empty list Ok(Layout::Builtin(Builtin::EmptyList)) diff --git a/compiler/reporting/src/error/type.rs b/compiler/reporting/src/error/type.rs index af7c40c78d..e3b3babd25 100644 --- a/compiler/reporting/src/error/type.rs +++ b/compiler/reporting/src/error/type.rs @@ -823,7 +823,6 @@ fn count_arguments(tipe: &ErrorType) -> usize { match tipe { Function(args, _, _) => args.len(), - Type(Symbol::ATTR_ATTR, args) => count_arguments(&args[1]), Alias(_, _, actual) => count_arguments(actual), _ => 0, } diff --git a/compiler/solve/src/solve.rs b/compiler/solve/src/solve.rs index 4907e2aff3..33632009e6 100644 --- a/compiler/solve/src/solve.rs +++ b/compiler/solve/src/solve.rs @@ -1,7 +1,6 @@ use roc_can::constraint::Constraint::{self, *}; use roc_can::expected::{Expected, PExpected}; use roc_collections::all::{ImMap, MutMap}; -use roc_module::ident::TagName; use roc_module::symbol::Symbol; use roc_region::all::{Located, Region}; use roc_types::solved_types::Solved; @@ -872,98 +871,39 @@ fn check_for_infinite_type( ) { let var = loc_var.value; - let is_uniq_infer = matches!( - subs.get_ref(var).content, - Content::Alias(Symbol::ATTR_ATTR, _, _) - ); - - while let Some((recursive, chain)) = subs.occurs(var) { + while let Some((recursive, _chain)) = subs.occurs(var) { let description = subs.get(recursive); let content = description.content; // try to make a tag union recursive, see if that helps match content { Content::Structure(FlatType::TagUnion(tags, ext_var)) => { - if !is_uniq_infer { - let rec_var = subs.fresh_unnamed_flex_var(); - subs.set_rank(rec_var, description.rank); - subs.set_content( - rec_var, - Content::RecursionVar { - opt_name: None, - structure: recursive, - }, - ); + let rec_var = subs.fresh_unnamed_flex_var(); + subs.set_rank(rec_var, description.rank); + subs.set_content( + rec_var, + Content::RecursionVar { + opt_name: None, + structure: recursive, + }, + ); - let mut new_tags = MutMap::default(); + let mut new_tags = MutMap::default(); - for (label, args) in &tags { - let new_args: Vec<_> = args - .iter() - .map(|var| subs.explicit_substitute(recursive, rec_var, *var)) - .collect(); + for (label, args) in &tags { + let new_args: Vec<_> = args + .iter() + .map(|var| subs.explicit_substitute(recursive, rec_var, *var)) + .collect(); - new_tags.insert(label.clone(), new_args); - } - - let new_ext_var = subs.explicit_substitute(recursive, rec_var, ext_var); - - let flat_type = FlatType::RecursiveTagUnion(rec_var, new_tags, new_ext_var); - - subs.set_content(recursive, Content::Structure(flat_type)); - } else { - // Sometimes, the recursion "starts" at the tag-union, not an `Attr`. Here we - // We use the path that `occurs` took to find the recursion to go one step - // forward in the recursion and find the `Attr` there. - let index = 0; - match subs.get(chain[index]).content { - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, args)) => { - debug_assert!(args.len() == 2); - debug_assert!( - subs.get_root_key_without_compacting(recursive) - == subs.get_root_key_without_compacting(args[1]) - ); - - // NOTE this ensures we use the same uniqueness var for the whole spine - // that might add too much uniqueness restriction. - // using `subs.fresh_unnamed_flex_var()` loosens it. - let uniq_var = args[0]; - let tag_union_var = recursive; - let recursive = chain[index]; - - correct_recursive_attr( - subs, - recursive, - uniq_var, - tag_union_var, - ext_var, - description.rank, - &tags, - ); - } - _ => circular_error(subs, problems, symbol, &loc_var), - } - } - } - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, args)) => { - debug_assert!(args.len() == 2); - let uniq_var = args[0]; - let tag_union_var = args[1]; - let nested_description = subs.get(tag_union_var); - match nested_description.content { - Content::Structure(FlatType::TagUnion(tags, ext_var)) => { - correct_recursive_attr( - subs, - recursive, - uniq_var, - tag_union_var, - ext_var, - description.rank, - &tags, - ); - } - _ => circular_error(subs, problems, symbol, &loc_var), + new_tags.insert(label.clone(), new_args); } + + let new_ext_var = subs.explicit_substitute(recursive, rec_var, ext_var); + + let flat_type = FlatType::RecursiveTagUnion(rec_var, new_tags, new_ext_var); + + subs.set_content(recursive, Content::Structure(flat_type)); } _other => circular_error(subs, problems, symbol, &loc_var), @@ -971,54 +911,6 @@ fn check_for_infinite_type( } } -fn content_attr(u: Variable, a: Variable) -> Content { - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, vec![u, a])) -} - -fn correct_recursive_attr( - subs: &mut Subs, - recursive: Variable, - uniq_var: Variable, - tag_union_var: Variable, - ext_var: Variable, - recursion_var_rank: Rank, - tags: &MutMap>, -) { - let rec_var = subs.fresh_unnamed_flex_var(); - let attr_var = subs.fresh_unnamed_flex_var(); - - let content = content_attr(uniq_var, rec_var); - subs.set_content(attr_var, content); - - subs.set_rank(rec_var, recursion_var_rank); - subs.set_content( - rec_var, - Content::RecursionVar { - opt_name: None, - structure: recursive, - }, - ); - - let mut new_tags = MutMap::default(); - - let new_ext_var = subs.explicit_substitute(recursive, attr_var, ext_var); - for (label, args) in tags { - let new_args: Vec<_> = args - .iter() - .map(|var| subs.explicit_substitute(recursive, attr_var, *var)) - .collect(); - - new_tags.insert(label.clone(), new_args); - } - - let new_tag_type = FlatType::RecursiveTagUnion(rec_var, new_tags, new_ext_var); - subs.set_content(tag_union_var, Content::Structure(new_tag_type)); - - let new_recursive = content_attr(uniq_var, tag_union_var); - - subs.set_content(recursive, new_recursive); -} - fn circular_error( subs: &mut Subs, problems: &mut Vec, diff --git a/compiler/types/src/pretty_print.rs b/compiler/types/src/pretty_print.rs index 9f9bacd42e..aa1a39fa89 100644 --- a/compiler/types/src/pretty_print.rs +++ b/compiler/types/src/pretty_print.rs @@ -140,11 +140,6 @@ fn find_names_needed( // We must not accidentally generate names that collide with them! names_taken.insert(name); } - Structure(Apply(Symbol::ATTR_ATTR, args)) => { - // assign uniqueness var names based on when they occur in the base type - find_names_needed(args[1], subs, roots, root_appearances, names_taken); - find_names_needed(args[0], subs, roots, root_appearances, names_taken); - } Structure(Apply(_, args)) => { for var in args { find_names_needed(var, subs, roots, root_appearances, names_taken); @@ -195,17 +190,12 @@ fn find_names_needed( find_names_needed(ext_var, subs, roots, root_appearances, names_taken); find_names_needed(rec_var, subs, roots, root_appearances, names_taken); } - Alias(symbol, args, _actual) => { - if let Symbol::ATTR_ATTR = symbol { - find_names_needed(args[0].1, subs, roots, root_appearances, names_taken); - find_names_needed(args[1].1, subs, roots, root_appearances, names_taken); - } else { - for (_, var) in args { - find_names_needed(var, subs, roots, root_appearances, names_taken); - } - // TODO should we also look in the actual variable? - // find_names_needed(_actual, subs, roots, root_appearances, names_taken); + Alias(_symbol, args, _actual) => { + for (_, var) in args { + find_names_needed(var, subs, roots, root_appearances, names_taken); } + // TODO should we also look in the actual variable? + // find_names_needed(_actual, subs, roots, root_appearances, names_taken); } Error | Structure(Erroneous(_)) | Structure(EmptyRecord) | Structure(EmptyTagUnion) => { // Errors and empty records don't need names. @@ -325,24 +315,6 @@ fn write_content(env: &Env, content: Content, subs: &Subs, buf: &mut String, par }), }, - Structure(FlatType::Apply(Symbol::ATTR_ATTR, nested_args)) => { - let attr_content = subs.get_without_compacting(nested_args[1]).content; - match &attr_content { - Alias(nested, _, _) => match *nested { - Symbol::NUM_INTEGER => buf.push_str("I64"), - Symbol::NUM_FLOATINGPOINT => buf.push_str("F64"), - _ => write_parens!(write_parens, buf, { - buf.push_str("Num "); - write_content(env, content, subs, buf, parens); - }), - }, - _ => write_parens!(write_parens, buf, { - buf.push_str("Num "); - write_content(env, content, subs, buf, parens); - }), - } - } - _ => write_parens!(write_parens, buf, { buf.push_str("Num "); write_content(env, content, subs, buf, parens); @@ -598,11 +570,6 @@ pub fn chase_ext_tag_union( chase_ext_tag_union(subs, ext_var, fields) } - Content::Structure(Apply(Symbol::ATTR_ATTR, arguments)) => { - debug_assert_eq!(arguments.len(), 2); - - chase_ext_tag_union(subs, arguments[1], fields) - } Content::Alias(_, _, var) => chase_ext_tag_union(subs, var, fields), content => Err((var, content)), @@ -626,12 +593,6 @@ pub fn chase_ext_record( Structure(EmptyRecord) => Ok(()), - Content::Structure(Apply(Symbol::ATTR_ATTR, arguments)) => { - debug_assert_eq!(arguments.len(), 2); - - chase_ext_record(subs, arguments[1], fields) - } - Alias(_, _, var) => chase_ext_record(subs, var, fields), content => Err((var, content)), @@ -683,25 +644,6 @@ fn write_apply( Symbol::NUM_FLOATINGPOINT if nested_args.len() == 1 => { buf.push_str("F64"); } - Symbol::ATTR_ATTR => match nested_args - .get(1) - .map(|v| subs.get_without_compacting(*v).content) - { - Some(Content::Structure(FlatType::Apply( - double_nested_symbol, - double_nested_args, - ))) => match double_nested_symbol { - Symbol::NUM_INTEGER if double_nested_args.len() == 1 => { - buf.push_str("I64"); - } - Symbol::NUM_FLOATINGPOINT if double_nested_args.len() == 1 => { - buf.push_str("F64"); - } - _ => default_case(subs, arg_content), - }, - - _other => default_case(subs, arg_content), - }, _ => default_case(subs, arg_content), }, _ => default_case(subs, arg_content), diff --git a/compiler/types/src/types.rs b/compiler/types/src/types.rs index f940eeb61f..b95ea6eaaf 100644 --- a/compiler/types/src/types.rs +++ b/compiler/types/src/types.rs @@ -363,11 +363,7 @@ impl Type { } } pub fn is_recursive(&self) -> bool { - match self { - Type::RecursiveTagUnion(_, _, _) => true, - Type::Alias(Symbol::ATTR_ATTR, _, actual) => actual.is_recursive(), - _ => false, - } + matches!(self, Type::RecursiveTagUnion(_, _, _)) } pub fn variables(&self) -> ImSet { diff --git a/compiler/unify/src/unify.rs b/compiler/unify/src/unify.rs index a4cdfaad31..daed2667e1 100644 --- a/compiler/unify/src/unify.rs +++ b/compiler/unify/src/unify.rs @@ -704,7 +704,6 @@ fn unify_tag_union_not_recursive_recursive( fn is_structure(var: Variable, subs: &mut Subs) -> bool { match subs.get(var).content { Content::Alias(_, _, actual) => is_structure(actual, subs), - Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, args)) => is_structure(args[1], subs), Content::Structure(_) => true, _ => false, }