Support deriving decoder for record with arbitrary ext var

This commit is contained in:
Ayaz Hafiz 2022-08-17 09:51:44 -05:00
parent b5e59d22e3
commit 2f24c81dab
No known key found for this signature in database
GPG Key ID: 0E2A37416A25EF58
2 changed files with 34 additions and 12 deletions

View File

@ -1,7 +1,10 @@
use roc_module::{ident::Lowercase, symbol::Symbol};
use roc_types::subs::{Content, FlatType, Subs, Variable};
use crate::{util::debug_name_record, DeriveError};
use crate::{
util::{check_derivable_ext_var, debug_name_record},
DeriveError,
};
#[derive(Hash)]
pub enum FlatDecodable {
@ -38,10 +41,11 @@ impl FlatDecodable {
_ => Err(Underivable),
},
FlatType::Record(fields, ext) => {
let fields_iter = match fields.unsorted_iterator(subs, ext) {
Ok(it) => it,
Err(_) => return Err(Underivable),
};
let (fields_iter, ext) = fields.unsorted_iterator_and_ext(subs, ext);
check_derivable_ext_var(subs, ext, |ext| {
matches!(ext, Content::Structure(FlatType::EmptyRecord))
})?;
let mut field_names = Vec::with_capacity(fields.len());
for (field_name, record_field) in fields_iter {

View File

@ -6,14 +6,14 @@
use crate::{
test_key_eq, test_key_neq,
util::{check_immediate, check_underivable, derive_test},
util::{check_derivable, check_immediate, check_underivable, derive_test},
v,
};
use insta::assert_snapshot;
use roc_module::symbol::Symbol;
use roc_types::subs::Variable;
use roc_derive_key::{DeriveBuiltin::Decoder, DeriveError};
use roc_derive_key::{decoding::FlatDecodableKey, DeriveBuiltin::Decoder, DeriveError, DeriveKey};
test_key_eq! {
Decoder,
@ -43,11 +43,6 @@ test_key_neq! {
v!(EMPTY_RECORD), v!({ a: v!(U8), })
}
#[test]
fn optional_record_field_derive_error() {
check_underivable(Decoder, v!({ ?a: v!(U8), }), DeriveError::Underivable);
}
#[test]
fn immediates() {
check_immediate(Decoder, v!(U8), Symbol::DECODE_U8);
@ -66,6 +61,29 @@ fn immediates() {
check_immediate(Decoder, v!(STR), Symbol::DECODE_STRING);
}
#[test]
fn optional_record_field_derive_error() {
check_underivable(Decoder, v!({ ?a: v!(U8), }), DeriveError::Underivable);
}
#[test]
fn derivable_record_ext_flex_var() {
check_derivable(
Decoder,
v!({ a: v!(STR), }* ),
DeriveKey::Decoder(FlatDecodableKey::Record(vec!["a".into()])),
);
}
#[test]
fn derivable_record_with_record_ext() {
check_derivable(
Decoder,
v!({ b: v!(STR), }{ a: v!(STR), } ),
DeriveKey::Decoder(FlatDecodableKey::Record(vec!["a".into(), "b".into()])),
);
}
#[test]
fn list() {
derive_test(Decoder, v!(Symbol::LIST_LIST v!(STR)), |golden| {