mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Move check_ext_var to utility module
This commit is contained in:
parent
0cc3eae2cb
commit
275391c065
3 changed files with 23 additions and 19 deletions
|
@ -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))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
@ -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};
|
||||||
|
|
19
crates/compiler/derive_key/src/util.rs
Normal file
19
crates/compiler/derive_key/src/util.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue