Add Inspect.Inspect to subs and auto-derive

This commit is contained in:
Richard Feldman 2023-10-18 08:04:58 -04:00 committed by Brendan Hansknecht
parent d36eba98fc
commit 443f6593c5
No known key found for this signature in database
GPG Key ID: 0EA784685083E75B
2 changed files with 109 additions and 0 deletions

View File

@ -386,6 +386,7 @@ impl ObligationCache {
DeriveDecoding::ABILITY => DeriveDecoding::is_derivable_builtin_opaque(opaque),
DeriveEq::ABILITY => DeriveEq::is_derivable_builtin_opaque(opaque),
DeriveHash::ABILITY => DeriveHash::is_derivable_builtin_opaque(opaque),
DeriveInspect::ABILITY => DeriveInspect::is_derivable_builtin_opaque(opaque),
_ => false,
};
@ -851,6 +852,111 @@ trait DerivableVisitor {
}
}
struct DeriveInspect;
impl DerivableVisitor for DeriveInspect {
const ABILITY: Symbol = Symbol::INSPECT_INSPECT;
const ABILITY_SLICE: SubsSlice<Symbol> = Subs::AB_INSPECT;
#[inline(always)]
fn is_derivable_builtin_opaque(symbol: Symbol) -> bool {
(is_builtin_number_alias(symbol) && !is_builtin_nat_alias(symbol))
|| is_builtin_bool_alias(symbol)
}
#[inline(always)]
fn visit_recursion(_var: Variable) -> Result<Descend, NotDerivable> {
Ok(Descend(true))
}
#[inline(always)]
fn visit_apply(var: Variable, symbol: Symbol) -> Result<Descend, NotDerivable> {
if matches!(
symbol,
Symbol::LIST_LIST | Symbol::SET_SET | Symbol::DICT_DICT | Symbol::STR_STR,
) {
Ok(Descend(true))
} else {
Err(NotDerivable {
var,
context: NotDerivableContext::NoContext,
})
}
}
#[inline(always)]
fn visit_record(
_subs: &Subs,
_var: Variable,
_fields: RecordFields,
) -> Result<Descend, NotDerivable> {
Ok(Descend(true))
}
#[inline(always)]
fn visit_tuple(
_subs: &Subs,
_var: Variable,
_elems: TupleElems,
) -> Result<Descend, NotDerivable> {
Ok(Descend(true))
}
#[inline(always)]
fn visit_tag_union(_var: Variable) -> Result<Descend, NotDerivable> {
Ok(Descend(true))
}
#[inline(always)]
fn visit_recursive_tag_union(_var: Variable) -> Result<Descend, NotDerivable> {
Ok(Descend(true))
}
#[inline(always)]
fn visit_function_or_tag_union(_var: Variable) -> Result<Descend, NotDerivable> {
Ok(Descend(true))
}
#[inline(always)]
fn visit_empty_record(_var: Variable) -> Result<(), NotDerivable> {
Ok(())
}
#[inline(always)]
fn visit_empty_tag_union(_var: Variable) -> Result<(), NotDerivable> {
Ok(())
}
#[inline(always)]
fn visit_alias(var: Variable, symbol: Symbol) -> Result<Descend, NotDerivable> {
if is_builtin_number_alias(symbol) {
if is_builtin_nat_alias(symbol) {
Err(NotDerivable {
var,
context: NotDerivableContext::Encode(NotDerivableEncode::Nat),
})
} else {
Ok(Descend(false))
}
} else {
Ok(Descend(true))
}
}
#[inline(always)]
fn visit_ranged_number(_var: Variable, _range: NumericRange) -> Result<(), NotDerivable> {
Ok(())
}
#[inline(always)]
fn visit_floating_point_content(
_var: Variable,
_subs: &mut Subs,
_content_var: Variable,
) -> Result<Descend, NotDerivable> {
Ok(Descend(false))
}
}
struct DeriveEncoding;
impl DerivableVisitor for DeriveEncoding {
const ABILITY: Symbol = Symbol::ENCODE_ENCODING;

View File

@ -1726,6 +1726,8 @@ impl Subs {
pub const AB_HASH: SubsSlice<Symbol> = SubsSlice::new(3, 1);
#[rustfmt::skip]
pub const AB_EQ: SubsSlice<Symbol> = SubsSlice::new(4, 1);
#[rustfmt::skip]
pub const AB_INSPECT: SubsSlice<Symbol> = SubsSlice::new(5, 1);
// END INIT-SymbolSubsSlice
pub fn new() -> Self {
@ -1754,6 +1756,7 @@ impl Subs {
symbol_names.push(Symbol::HASH_HASHER);
symbol_names.push(Symbol::HASH_HASH_ABILITY);
symbol_names.push(Symbol::BOOL_EQ);
symbol_names.push(Symbol::INSPECT_INSPECT);
// END INIT-SymbolNames
// IFTTT INIT-VariableSubsSlice