Move check_ext_var to utility module

This commit is contained in:
Ayaz Hafiz 2022-08-09 08:55:21 -07:00
parent 0cc3eae2cb
commit 275391c065
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
3 changed files with 23 additions and 19 deletions

View file

@ -4,7 +4,7 @@ use roc_module::{
}; };
use roc_types::subs::{Content, FlatType, GetSubsSlice, Subs, Variable}; use roc_types::subs::{Content, FlatType, GetSubsSlice, Subs, Variable};
use crate::DeriveError; use crate::{util::check_empty_ext_var, DeriveError};
#[derive(Hash)] #[derive(Hash)]
pub enum FlatEncodable { pub enum FlatEncodable {
@ -56,22 +56,6 @@ impl FlatEncodableKey {
} }
} }
fn check_ext_var(
subs: &Subs,
ext_var: Variable,
is_empty_ext: impl Fn(&Content) -> bool,
) -> Result<(), DeriveError> {
let ext_content = subs.get_content_without_compacting(ext_var);
if is_empty_ext(ext_content) {
Ok(())
} else {
match ext_content {
Content::FlexVar(_) => Err(DeriveError::UnboundVar),
_ => Err(DeriveError::Underivable),
}
}
}
impl FlatEncodable { impl FlatEncodable {
pub(crate) fn from_var(subs: &Subs, var: Variable) -> Result<FlatEncodable, DeriveError> { pub(crate) fn from_var(subs: &Subs, var: Variable) -> Result<FlatEncodable, DeriveError> {
use DeriveError::*; use DeriveError::*;
@ -86,7 +70,7 @@ impl FlatEncodable {
_ => Err(Underivable), _ => Err(Underivable),
}, },
FlatType::Record(fields, ext) => { FlatType::Record(fields, ext) => {
check_ext_var(subs, ext, |ext| { check_empty_ext_var(subs, ext, |ext| {
matches!(ext, Content::Structure(FlatType::EmptyRecord)) matches!(ext, Content::Structure(FlatType::EmptyRecord))
})?; })?;
@ -106,7 +90,7 @@ impl FlatEncodable {
// [ A t1, B t1 t2 ] as R // [ A t1, B t1 t2 ] as R
// look the same on the surface, because `R` is only somewhere inside of the // look the same on the surface, because `R` is only somewhere inside of the
// `t`-prefixed payload types. // `t`-prefixed payload types.
check_ext_var(subs, ext, |ext| { check_empty_ext_var(subs, ext, |ext| {
matches!(ext, Content::Structure(FlatType::EmptyTagUnion)) matches!(ext, Content::Structure(FlatType::EmptyTagUnion))
})?; })?;

View file

@ -15,6 +15,7 @@
pub mod decoding; pub mod decoding;
pub mod encoding; pub mod encoding;
mod util;
use decoding::{FlatDecodable, FlatDecodableKey}; use decoding::{FlatDecodable, FlatDecodableKey};
use encoding::{FlatEncodable, FlatEncodableKey}; use encoding::{FlatEncodable, FlatEncodableKey};

View file

@ -0,0 +1,19 @@
use roc_types::subs::{Content, Subs, Variable};
use crate::DeriveError;
pub(crate) fn check_empty_ext_var(
subs: &Subs,
ext_var: Variable,
is_empty_ext: impl Fn(&Content) -> bool,
) -> Result<(), DeriveError> {
let ext_content = subs.get_content_without_compacting(ext_var);
if is_empty_ext(ext_content) {
Ok(())
} else {
match ext_content {
Content::FlexVar(_) => Err(DeriveError::UnboundVar),
_ => Err(DeriveError::Underivable),
}
}
}