diff --git a/compiler/exhaustive/src/lib.rs b/compiler/exhaustive/src/lib.rs index 644af3add0..c94b98aaa0 100644 --- a/compiler/exhaustive/src/lib.rs +++ b/compiler/exhaustive/src/lib.rs @@ -2,7 +2,10 @@ //! http://moscova.inria.fr/~maranget/papers/warn/warn.pdf use roc_collections::all::{HumanIndex, MutMap}; -use roc_module::ident::{Lowercase, TagIdIntType, TagName}; +use roc_module::{ + ident::{Lowercase, TagIdIntType, TagName}, + symbol::Symbol, +}; use roc_region::all::Region; use roc_std::RocDec; @@ -15,9 +18,9 @@ pub struct Union { } impl Union { - pub fn newtype_wrapper(tag_name: TagName, arity: usize) -> Self { + pub fn newtype_wrapper(name: CtorName, arity: usize) -> Self { let alternatives = vec![Ctor { - name: tag_name, + name, tag_id: TagId(0), arity, }]; @@ -40,9 +43,24 @@ pub enum RenderAs { #[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)] pub struct TagId(pub TagIdIntType); +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum CtorName { + Tag(TagName), + Opaque(Symbol), +} + +impl CtorName { + pub fn is_tag(&self, tag_name: &TagName) -> bool { + match self { + Self::Tag(test) => test == tag_name, + _ => false, + } + } +} + #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Ctor { - pub name: TagName, + pub name: CtorName, pub tag_id: TagId, pub arity: usize, } diff --git a/compiler/gen_dev/src/lib.rs b/compiler/gen_dev/src/lib.rs index 1d6fe2ea2c..04e79d5831 100644 --- a/compiler/gen_dev/src/lib.rs +++ b/compiler/gen_dev/src/lib.rs @@ -913,9 +913,6 @@ trait Backend<'a> { TagName::Closure(sym) => { self.set_last_seen(*sym, stmt); } - TagName::Private(sym) => { - self.set_last_seen(*sym, stmt); - } TagName::Global(_) => {} } for sym in *arguments { diff --git a/compiler/module/src/ident.rs b/compiler/module/src/ident.rs index 2b4347d3d9..5335cad281 100644 --- a/compiler/module/src/ident.rs +++ b/compiler/module/src/ident.rs @@ -53,10 +53,6 @@ pub enum TagName { /// into integers. (Record field labels work the same way, for the same reason.) Global(Uppercase), - /// Private tags are associated with a specific module, and as such use a - /// Symbol just like all other module-specific identifiers. - Private(Symbol), - /// Used to connect the closure size to the function it corresponds to Closure(Symbol), } @@ -69,9 +65,6 @@ impl TagName { pub fn as_ident_str(&self, interns: &Interns, home: ModuleId) -> IdentStr { match self { TagName::Global(uppercase) => uppercase.as_ident_str().clone(), - TagName::Private(symbol) => { - symbol.fully_qualified(interns, home).as_ident_str().clone() - } TagName::Closure(symbol) => { symbol.fully_qualified(interns, home).as_ident_str().clone() } diff --git a/compiler/mono/src/decision_tree.rs b/compiler/mono/src/decision_tree.rs index 2d9718c417..2295d307a7 100644 --- a/compiler/mono/src/decision_tree.rs +++ b/compiler/mono/src/decision_tree.rs @@ -4,7 +4,7 @@ use crate::ir::{ use crate::layout::{Builtin, Layout, LayoutCache, TagIdIntType, UnionLayout}; use roc_builtins::bitcode::{FloatWidth, IntWidth}; use roc_collections::all::{MutMap, MutSet}; -use roc_exhaustive::{Ctor, RenderAs, TagId, Union}; +use roc_exhaustive::{Ctor, CtorName, RenderAs, TagId, Union}; use roc_module::ident::TagName; use roc_module::low_level::LowLevel; use roc_module::symbol::Symbol; @@ -82,7 +82,7 @@ enum GuardedTest<'a> { enum Test<'a> { IsCtor { tag_id: TagIdIntType, - tag_name: TagName, + ctor_name: CtorName, union: roc_exhaustive::Union, arguments: Vec<(Pattern<'a>, Layout<'a>)>, }, @@ -512,7 +512,7 @@ fn test_at_path<'a>( render_as: RenderAs::Tag, alternatives: vec![Ctor { tag_id: TagId(0), - name: TagName::Global(RECORD_TAG_NAME.into()), + name: CtorName::Tag(TagName::Global(RECORD_TAG_NAME.into())), arity: destructs.len(), }], }; @@ -532,7 +532,7 @@ fn test_at_path<'a>( IsCtor { tag_id: 0, - tag_name: TagName::Global(RECORD_TAG_NAME.into()), + ctor_name: CtorName::Tag(TagName::Global(RECORD_TAG_NAME.into())), union, arguments, } @@ -543,11 +543,12 @@ fn test_at_path<'a>( arguments, } => { let tag_id = 0; - let union = Union::newtype_wrapper(tag_name.clone(), arguments.len()); + let union = + Union::newtype_wrapper(CtorName::Tag(tag_name.clone()), arguments.len()); IsCtor { tag_id, - tag_name: tag_name.clone(), + ctor_name: CtorName::Tag(tag_name.clone()), union, arguments: arguments.to_vec(), } @@ -561,7 +562,7 @@ fn test_at_path<'a>( .. } => IsCtor { tag_id: *tag_id, - tag_name: tag_name.clone(), + ctor_name: CtorName::Tag(tag_name.clone()), union: union.clone(), arguments: arguments.to_vec(), }, @@ -571,14 +572,14 @@ fn test_at_path<'a>( render_as: RenderAs::Tag, alternatives: vec![Ctor { tag_id: TagId(0), - name: TagName::Private(*opaque), + name: CtorName::Opaque(*opaque), arity: 1, }], }; IsCtor { tag_id: 0, - tag_name: TagName::Private(*opaque), + ctor_name: CtorName::Opaque(*opaque), union, arguments: vec![(**argument).clone()], } @@ -680,11 +681,11 @@ fn to_relevant_branch_help<'a>( RecordDestructure(destructs, _) => match test { IsCtor { - tag_name: test_name, + ctor_name: test_name, tag_id, .. } => { - debug_assert!(test_name == &TagName::Global(RECORD_TAG_NAME.into())); + debug_assert!(test_name == &CtorName::Tag(TagName::Global(RECORD_TAG_NAME.into()))); let sub_positions = destructs.into_iter().enumerate().map(|(index, destruct)| { let pattern = match destruct.typ { DestructType::Guard(guard) => guard.clone(), @@ -713,11 +714,11 @@ fn to_relevant_branch_help<'a>( OpaqueUnwrap { opaque, argument } => match test { IsCtor { - tag_name: test_opaque_tag_name, + ctor_name: test_opaque_tag_name, tag_id, .. } => { - debug_assert_eq!(test_opaque_tag_name, &TagName::Private(opaque)); + debug_assert_eq!(test_opaque_tag_name, &CtorName::Opaque(opaque)); let (argument, _) = *argument; @@ -744,10 +745,10 @@ fn to_relevant_branch_help<'a>( .. } => match test { IsCtor { - tag_name: test_name, + ctor_name: test_name, tag_id: test_id, .. - } if &tag_name == test_name => { + } if test_name.is_tag(&tag_name) => { let tag_id = 0; debug_assert_eq!(tag_id, *test_id); @@ -785,10 +786,10 @@ fn to_relevant_branch_help<'a>( } => { match test { IsCtor { - tag_name: test_name, + ctor_name: test_name, tag_id: test_id, .. - } if &tag_name == test_name => { + } if test_name.is_tag(&tag_name) => { debug_assert_eq!(tag_id, *test_id); // the test matches the constructor of this pattern diff --git a/compiler/mono/src/exhaustive.rs b/compiler/mono/src/exhaustive.rs index bfcb3b3e90..514cdf3f2d 100644 --- a/compiler/mono/src/exhaustive.rs +++ b/compiler/mono/src/exhaustive.rs @@ -1,7 +1,7 @@ use crate::ir::DestructType; use roc_collections::all::HumanIndex; use roc_exhaustive::{ - is_useful, Context, Ctor, Error, Guard, Literal, Pattern, RenderAs, TagId, Union, + is_useful, Context, Ctor, CtorName, Error, Guard, Literal, Pattern, RenderAs, TagId, Union, }; use roc_module::ident::{TagIdIntType, TagName}; use roc_region::all::{Loc, Region}; @@ -45,7 +45,7 @@ fn simplify(pattern: &crate::ir::Pattern) -> Pattern { let union = Union { render_as: RenderAs::Record(field_names), alternatives: vec![Ctor { - name: TagName::Global("#Record".into()), + name: CtorName::Tag(TagName::Global("#Record".into())), tag_id, arity: destructures.len(), }], @@ -62,7 +62,7 @@ fn simplify(pattern: &crate::ir::Pattern) -> Pattern { let simplified_args: std::vec::Vec<_> = arguments.iter().map(|v| simplify(&v.0)).collect(); Ctor( - Union::newtype_wrapper(tag_name.clone(), arguments.len()), + Union::newtype_wrapper(CtorName::Tag(tag_name.clone()), arguments.len()), TagId(tag_id), simplified_args, ) @@ -87,7 +87,7 @@ fn simplify(pattern: &crate::ir::Pattern) -> Pattern { let union = Union { render_as: RenderAs::Opaque, alternatives: vec![Ctor { - name: TagName::Private(*opaque), + name: CtorName::Opaque(*opaque), tag_id, arity: 1, }], @@ -169,7 +169,7 @@ fn to_nonredundant_rows( render_as: RenderAs::Guard, alternatives: vec![Ctor { tag_id, - name: TagName::Global("#Guard".into()), + name: CtorName::Tag(TagName::Global("#Guard".into())), arity: 2, }], }; diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index 2ed74c3735..f98b26370f 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -10,7 +10,7 @@ use roc_builtins::bitcode::{FloatWidth, IntWidth}; use roc_can::abilities::AbilitiesStore; use roc_can::expr::{ClosureData, IntValue}; use roc_collections::all::{default_hasher, BumpMap, BumpMapDefault, MutMap}; -use roc_exhaustive::{Ctor, Guard, RenderAs, TagId}; +use roc_exhaustive::{Ctor, CtorName, Guard, RenderAs, TagId}; use roc_module::ident::{ForeignSymbol, Lowercase, TagName}; use roc_module::low_level::LowLevel; use roc_module::symbol::{IdentIds, ModuleId, Symbol}; @@ -1656,7 +1656,6 @@ impl<'a> Expr<'a> { } => { let doc_tag = match tag_name { TagName::Global(s) => alloc.text(s.as_str()), - TagName::Private(s) => symbol_to_doc(alloc, *s), TagName::Closure(s) => alloc .text("ClosureTag(") .append(symbol_to_doc(alloc, *s)) @@ -1678,7 +1677,6 @@ impl<'a> Expr<'a> { } => { let doc_tag = match tag_name { TagName::Global(s) => alloc.text(s.as_str()), - TagName::Private(s) => alloc.text(format!("{}", s)), TagName::Closure(s) => alloc .text("ClosureTag(") .append(symbol_to_doc(alloc, *s)) @@ -8039,7 +8037,7 @@ fn from_can_pattern_help<'a>( render_as: RenderAs::Tag, alternatives: vec![Ctor { tag_id: TagId(0), - name: tag_name.clone(), + name: CtorName::Tag(tag_name.clone()), arity: 0, }], }, @@ -8052,12 +8050,12 @@ fn from_can_pattern_help<'a>( alternatives: vec![ Ctor { tag_id: TagId(0), - name: ffalse, + name: CtorName::Tag(ffalse), arity: 0, }, Ctor { tag_id: TagId(1), - name: ttrue, + name: CtorName::Tag(ttrue), arity: 0, }, ], @@ -8073,7 +8071,7 @@ fn from_can_pattern_help<'a>( for (i, tag_name) in tag_names.into_iter().enumerate() { ctors.push(Ctor { tag_id: TagId(i as _), - name: tag_name, + name: CtorName::Tag(tag_name), arity: 0, }) } @@ -8164,7 +8162,7 @@ fn from_can_pattern_help<'a>( for (i, (tag_name, args)) in tags.iter().enumerate() { ctors.push(Ctor { tag_id: TagId(i as _), - name: tag_name.clone(), + name: CtorName::Tag(tag_name.clone()), arity: args.len(), }) } @@ -8215,7 +8213,7 @@ fn from_can_pattern_help<'a>( for (i, (tag_name, args)) in tags.iter().enumerate() { ctors.push(Ctor { tag_id: TagId(i as _), - name: tag_name.clone(), + name: CtorName::Tag(tag_name.clone()), // don't include tag discriminant in arity arity: args.len() - 1, }) @@ -8260,7 +8258,7 @@ fn from_can_pattern_help<'a>( ctors.push(Ctor { tag_id: TagId(0), - name: tag_name.clone(), + name: CtorName::Tag(tag_name.clone()), arity: fields.len(), }); @@ -8307,7 +8305,7 @@ fn from_can_pattern_help<'a>( if i == nullable_id as usize { ctors.push(Ctor { tag_id: TagId(i as _), - name: nullable_name.clone(), + name: CtorName::Tag(nullable_name.clone()), // don't include tag discriminant in arity arity: 0, }); @@ -8317,7 +8315,7 @@ fn from_can_pattern_help<'a>( ctors.push(Ctor { tag_id: TagId(i as _), - name: tag_name.clone(), + name: CtorName::Tag(tag_name.clone()), // don't include tag discriminant in arity arity: args.len() - 1, }); @@ -8328,7 +8326,7 @@ fn from_can_pattern_help<'a>( if i == nullable_id as usize { ctors.push(Ctor { tag_id: TagId(i as _), - name: nullable_name.clone(), + name: CtorName::Tag(nullable_name.clone()), // don't include tag discriminant in arity arity: 0, }); @@ -8378,13 +8376,13 @@ fn from_can_pattern_help<'a>( ctors.push(Ctor { tag_id: TagId(nullable_id as _), - name: nullable_name.clone(), + name: CtorName::Tag(nullable_name.clone()), arity: 0, }); ctors.push(Ctor { tag_id: TagId(!nullable_id as _), - name: nullable_name.clone(), + name: CtorName::Tag(nullable_name.clone()), // FIXME drop tag arity: other_fields.len() - 1, }); diff --git a/compiler/mono/src/layout_soa.rs b/compiler/mono/src/layout_soa.rs index 05b7858b20..85bd46553f 100644 --- a/compiler/mono/src/layout_soa.rs +++ b/compiler/mono/src/layout_soa.rs @@ -344,7 +344,6 @@ impl LambdaSet { layouts.symbols.push(*symbol); } TagName::Global(_) => unreachable!("lambda set tags must be closure tags"), - TagName::Private(_) => unreachable!("lambda set tags must be closure tags"), } } diff --git a/compiler/types/src/builtin_aliases.rs b/compiler/types/src/builtin_aliases.rs index 63ca184df9..b638cad6e4 100644 --- a/compiler/types/src/builtin_aliases.rs +++ b/compiler/types/src/builtin_aliases.rs @@ -1076,10 +1076,3 @@ pub fn set_type(a: SolvedType) -> SolvedType { pub fn dict_type(key: SolvedType, value: SolvedType) -> SolvedType { SolvedType::Apply(Symbol::DICT_DICT, vec![key, value]) } - -pub fn single_private_tag(symbol: Symbol, type_arguments: Vec) -> SolvedType { - SolvedType::TagUnion( - vec![(TagName::Private(symbol), type_arguments)], - Box::new(SolvedType::EmptyTagUnion), - ) -} diff --git a/compiler/types/src/subs.rs b/compiler/types/src/subs.rs index 3a9888c8c5..c5ddade386 100644 --- a/compiler/types/src/subs.rs +++ b/compiler/types/src/subs.rs @@ -114,7 +114,6 @@ fn round_to_multiple_of(value: usize, base: usize) -> usize { enum SerializedTagName { Global(SubsSlice), - Private(Symbol), Closure(Symbol), } @@ -211,7 +210,6 @@ impl Subs { ); SerializedTagName::Global(slice) } - TagName::Private(symbol) => SerializedTagName::Private(*symbol), TagName::Closure(symbol) => SerializedTagName::Closure(*symbol), }; @@ -354,7 +352,6 @@ impl Subs { TagName::Global(string.into()) } - SerializedTagName::Private(symbol) => TagName::Private(*symbol), SerializedTagName::Closure(symbol) => TagName::Closure(*symbol), }; @@ -410,12 +407,10 @@ impl TagNameCache { None => None, } } - TagName::Private(symbol) | TagName::Closure(symbol) => { - match self.symbols.iter().position(|s| s == symbol) { - Some(index) => Some(&mut self.symbols_slices[index]), - None => None, - } - } + TagName::Closure(symbol) => match self.symbols.iter().position(|s| s == symbol) { + Some(index) => Some(&mut self.symbols_slices[index]), + None => None, + }, } } @@ -425,7 +420,7 @@ impl TagNameCache { self.globals.push(uppercase.clone()); self.globals_slices.push(slice); } - TagName::Private(symbol) | TagName::Closure(symbol) => { + TagName::Closure(symbol) => { self.symbols.push(*symbol); self.symbols_slices.push(slice); } diff --git a/repl_eval/src/eval.rs b/repl_eval/src/eval.rs index 44acb07ae9..f50622043a 100644 --- a/repl_eval/src/eval.rs +++ b/repl_eval/src/eval.rs @@ -475,7 +475,7 @@ fn tag_name_to_expr<'a>(env: &Env<'a, '_>, tag_name: &TagName) -> Expr<'a> { env.arena .alloc_str(&tag_name.as_ident_str(env.interns, env.home)), ), - TagName::Private(_) | TagName::Closure(_) => unreachable!("User cannot type this"), + TagName::Closure(_) => unreachable!("User cannot type this"), } } diff --git a/reporting/src/error/mono.rs b/reporting/src/error/mono.rs index 7ca6608c7d..8215b05b1a 100644 --- a/reporting/src/error/mono.rs +++ b/reporting/src/error/mono.rs @@ -145,7 +145,7 @@ fn pattern_to_doc_help<'b>( ) -> RocDocBuilder<'b> { use roc_exhaustive::Literal::*; use roc_exhaustive::Pattern::*; - use roc_exhaustive::RenderAs; + use roc_exhaustive::{CtorName, RenderAs}; match pattern { Anything => alloc.text("_"), @@ -163,10 +163,9 @@ fn pattern_to_doc_help<'b>( match union.render_as { RenderAs::Guard => { // #Guard - debug_assert_eq!( - union.alternatives[tag_id.0 as usize].name, - TagName::Global("#Guard".into()) - ); + debug_assert!(union.alternatives[tag_id.0 as usize] + .name + .is_tag(&TagName::Global("#Guard".into())),); debug_assert!(args.len() == 2); let tag = pattern_to_doc_help(alloc, args[1].clone(), in_type_param); alloc.concat([ @@ -207,18 +206,17 @@ fn pattern_to_doc_help<'b>( .into_iter() .map(|v| pattern_to_doc_help(alloc, v, true)); - let tag = &union.alternatives[tag_id.0 as usize]; - let tag_name = match union.render_as { - RenderAs::Tag => alloc.tag_name(tag.name.clone()), - RenderAs::Opaque => match tag.name { - TagName::Private(opaque) => alloc.wrapped_opaque_name(opaque), - _ => unreachable!(), - }, + let ctor = &union.alternatives[tag_id.0 as usize]; + let tag_name = match (union.render_as, &ctor.name) { + (RenderAs::Tag, CtorName::Tag(tag)) => alloc.tag_name(tag.clone()), + (RenderAs::Opaque, CtorName::Opaque(opaque)) => { + alloc.wrapped_opaque_name(*opaque) + } _ => unreachable!(), }; // We assume the alternatives are sorted. If not, this assert will trigger - debug_assert!(tag_id == tag.tag_id); + debug_assert!(tag_id == ctor.tag_id); let docs = std::iter::once(tag_name).chain(arg_docs); diff --git a/reporting/src/error/type.rs b/reporting/src/error/type.rs index d9e3fb198b..90e7ad15b9 100644 --- a/reporting/src/error/type.rs +++ b/reporting/src/error/type.rs @@ -1449,17 +1449,6 @@ fn format_category<'b>( ]), alloc.text(" has the type:"), ), - TagApply { - tag_name: TagName::Private(name), - args_count: 0, - } => ( - alloc.concat([ - alloc.text(format!("{}his ", t)), - alloc.private_tag_name(*name), - alloc.text(" private tag"), - ]), - alloc.text(" has the type:"), - ), TagApply { tag_name: TagName::Global(name), @@ -1472,17 +1461,6 @@ fn format_category<'b>( ]), alloc.text(" has the type:"), ), - TagApply { - tag_name: TagName::Private(name), - args_count: _, - } => ( - alloc.concat([ - alloc.text("This "), - alloc.private_tag_name(*name), - alloc.text(" private tag application"), - ]), - alloc.text(" has the type:"), - ), TagApply { tag_name: TagName::Closure(_name), args_count: _, diff --git a/reporting/src/report.rs b/reporting/src/report.rs index d0d8b95bdc..4d0a655a2f 100644 --- a/reporting/src/report.rs +++ b/reporting/src/report.rs @@ -386,9 +386,7 @@ impl<'a> RocDocAllocator<'a> { pub fn tag_name(&'a self, tn: TagName) -> DocBuilder<'a, Self, Annotation> { match tn { TagName::Global(uppercase) => self.global_tag_name(uppercase), - TagName::Private(symbol) => self.private_tag_name(symbol), - TagName::Closure(symbol) => self.private_tag_name(symbol), - // TagName::Closure(_symbol) => unreachable!("closure tags are internal only"), + TagName::Closure(_symbol) => unreachable!("closure tags are internal only"), } }