Store derivable ability members statically

This commit is contained in:
Ayaz Hafiz 2022-07-19 16:52:32 -04:00
parent 10001876bb
commit 6035e45f25
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
4 changed files with 13 additions and 9 deletions

View file

@ -733,7 +733,7 @@ fn canonicalize_opaque<'a>(
.map(|(member, def)| (member, def.value))
.collect(),
});
} else if ability.is_builtin_ability() {
} else if ability.is_derivable_ability() {
derived_abilities.push(Loc::at(region, ability));
supported_abilities.push(OpaqueSupports::Derived(ability));
} else {

View file

@ -47,7 +47,8 @@ const SYMBOL_HAS_NICHE: () =
#[cfg(debug_assertions)]
const PRETTY_PRINT_DEBUG_SYMBOLS: bool = true;
pub const BUILTIN_ABILITIES: &[Symbol] = &[Symbol::ENCODE_ENCODING];
pub const DERIVABLE_ABILITIES: &[(Symbol, &[Symbol])] =
&[(Symbol::ENCODE_ENCODING, &[Symbol::ENCODE_TO_ENCODER])];
/// In Debug builds only, Symbol has a name() method that lets
/// you look up its name in a global intern table. This table is
@ -86,8 +87,12 @@ impl Symbol {
self.module_id().is_builtin()
}
pub fn is_builtin_ability(self) -> bool {
BUILTIN_ABILITIES.contains(&self)
pub fn is_derivable_ability(self) -> bool {
self.derivable_ability().is_some()
}
pub fn derivable_ability(self) -> Option<&'static (Symbol, &'static [Symbol])> {
DERIVABLE_ABILITIES.iter().find(|(name, _)| *name == self)
}
pub fn module_string<'a>(&self, interns: &'a Interns) -> &'a ModuleName {

View file

@ -86,7 +86,7 @@ impl PendingDerivesTable {
} in derives
{
debug_assert!(
ability.is_builtin_ability(),
ability.is_derivable_ability(),
"Not a builtin - should have been caught during can"
);
let derive_key = RequestedDeriveKey { opaque, ability };

View file

@ -1,6 +1,6 @@
use roc_collections::all::MutSet;
use roc_module::ident::{Ident, Lowercase, ModuleName};
use roc_module::symbol::BUILTIN_ABILITIES;
use roc_module::symbol::DERIVABLE_ABILITIES;
use roc_problem::can::PrecedenceProblem::BothNonAssociative;
use roc_problem::can::{
BadPattern, ExtensionTypeKind, FloatErrorKind, IntErrorKind, Problem, RuntimeError, ShadowKind,
@ -934,9 +934,8 @@ pub fn can_problem<'b>(
}
fn list_builtin_abilities<'a>(alloc: &'a RocDocAllocator<'a>) -> RocDocBuilder<'a> {
let doc = alloc.concat([alloc.symbol_qualified(BUILTIN_ABILITIES[0])]);
debug_assert!(BUILTIN_ABILITIES.len() == 1);
doc
debug_assert!(DERIVABLE_ABILITIES.len() == 1);
alloc.concat([alloc.symbol_qualified(DERIVABLE_ABILITIES[0].0)])
}
fn to_invalid_optional_value_report<'b>(