This commit is contained in:
Folkert 2020-09-06 18:53:26 +02:00
parent ceb938c9af
commit 46755973d6
8 changed files with 59 additions and 13 deletions

View File

@ -268,11 +268,15 @@ fn constrain_pattern(
loc_expr.region,
);
state.constraints.push(Constraint::Eq(
Type::Variable(*expr_var),
expr_expected.clone(),
Category::DefaultValue(label.clone()),
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::PatternDefault,
Type::Variable(*expr_var),
PExpected::ForReason(
PReason::OptionalField,
pat_type.clone(),
loc_expr.region,
),
));
state.vars.push(*expr_var);

View File

@ -505,4 +505,32 @@ mod gen_records {
i64
);
}
#[test]
fn optional_field_singleton_record() {
assert_evals_to!(
indoc!(
r#"
when { x : 4 } is
{ x ? 3 } -> x
"#
),
4,
i64
);
}
#[test]
fn optional_field_empty_record() {
assert_evals_to!(
indoc!(
r#"
when { } is
{ x ? 3 } -> x
"#
),
3,
i64
);
}
}

View File

@ -124,6 +124,7 @@ pub fn helper_without_uniqueness<'a>(
);
let (mut procs, param_map) = procs.get_specialized_procs_help(mono_env.arena);
dbg!(&main_body);
let main_body = roc_mono::inc_dec::visit_declaration(
mono_env.arena,
param_map,
@ -317,6 +318,7 @@ pub fn helper_with_uniqueness<'a>(
);
let (mut procs, param_map) = procs.get_specialized_procs_help(mono_env.arena);
dbg!(&main_body);
let main_body = roc_mono::inc_dec::visit_declaration(
mono_env.arena,
param_map,
@ -475,7 +477,7 @@ macro_rules! assert_evals_to {
assert_llvm_evals_to!($src, $expected, $ty, (|val| val));
}
{
// assert_opt_evals_to!($src, $expected, $ty, (|val| val));
assert_opt_evals_to!($src, $expected, $ty, (|val| val));
}
};
($src:expr, $expected:expr, $ty:ty, $transform:expr) => {
@ -484,7 +486,7 @@ macro_rules! assert_evals_to {
assert_llvm_evals_to!($src, $expected, $ty, $transform);
}
{
// assert_opt_evals_to!($src, $expected, $ty, $transform);
assert_opt_evals_to!($src, $expected, $ty, $transform);
}
};
($src:expr, $expected:expr, $ty:ty, $transform:expr, $leak:expr) => {
@ -493,7 +495,7 @@ macro_rules! assert_evals_to {
assert_llvm_evals_to!($src, $expected, $ty, $transform, $leak);
}
{
// assert_opt_evals_to!($src, $expected, $ty, $transform, $leak);
assert_opt_evals_to!($src, $expected, $ty, $transform, $leak);
}
};
}

View File

@ -617,6 +617,7 @@ fn to_relevant_branch_help<'a>(
start.extend(sub_positions);
start.extend(end);
}
Wrapped::EmptyRecord => todo!(),
}
Some(Branch {
@ -1183,7 +1184,7 @@ fn decide_to_branching<'a>(
.expect("jump not in list of jumps");
expr.clone()
}
Leaf(Inline(expr)) => expr,
Leaf(Inline(expr)) => dbg!(expr),
Chain {
test_chain,
success,

View File

@ -535,6 +535,7 @@ impl CallType {
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Wrapped {
EmptyRecord,
SingleElementRecord,
RecordOrSingleTagUnion,
MultiTagUnion,
@ -551,7 +552,7 @@ impl Wrapped {
pub fn opt_from_layout(layout: &Layout<'_>) -> Option<Self> {
let result = match layout {
Layout::Struct(fields) => match fields.len() {
0 => todo!("how to handle empty records?"),
0 => Some(Wrapped::EmptyRecord),
1 => Some(Wrapped::SingleElementRecord),
_ => Some(Wrapped::RecordOrSingleTagUnion),
},
@ -559,7 +560,7 @@ impl Wrapped {
Layout::Union(tags) | Layout::RecursiveUnion(tags) => match tags {
[] => todo!("how to handle empty tag unions?"),
[single] => match single.len() {
0 => todo!("how to handle empty records?"),
0 => Some(Wrapped::EmptyRecord),
1 => Some(Wrapped::SingleElementRecord),
_ => Some(Wrapped::RecordOrSingleTagUnion),
},
@ -3547,11 +3548,14 @@ pub fn from_can_pattern<'a>(
let mut destructs = destructs.clone();
destructs.sort_by(|a, b| a.value.label.cmp(&b.value.label));
dbg!(&destructs);
let mut it = destructs.iter();
let mut opt_destruct = it.next();
// sorted fields based on the type
let sorted_fields = crate::layout::sort_record_fields(env.arena, *whole_var, env.subs);
dbg!(&sorted_fields);
let mut field_layouts = Vec::with_capacity_in(sorted_fields.len(), env.arena);

View File

@ -527,6 +527,7 @@ pub fn sort_record_fields<'a>(
seen: MutSet::default(),
};
dbg!(var);
match roc_types::pretty_print::chase_ext_record(subs, var, &mut fields_map) {
Ok(()) | Err((_, Content::FlexVar(_))) => {
// Sort the fields by label

View File

@ -611,7 +611,7 @@ pub fn chase_ext_record(
use crate::subs::Content::*;
use crate::subs::FlatType::*;
match subs.get_without_compacting(var).content {
match dbg!(subs.get_without_compacting(var).content) {
Structure(Record(sub_fields, sub_ext)) => {
fields.extend(sub_fields.into_iter());

View File

@ -705,11 +705,17 @@ fn unify_flat_type(
(EmptyRecord, EmptyRecord) => merge(subs, ctx, Structure(left.clone())),
(Record(fields, ext), EmptyRecord) if has_only_optional_fields(&mut fields.values()) => {
unify_pool(subs, pool, *ext, ctx.second)
let mut outcome = unify_pool(subs, pool, *ext, ctx.second);
outcome.extend(merge(subs, ctx, Structure(FlatType::EmptyRecord)));
outcome
}
(EmptyRecord, Record(fields, ext)) if has_only_optional_fields(&mut fields.values()) => {
unify_pool(subs, pool, ctx.first, *ext)
let mut outcome = unify_pool(subs, pool, ctx.first, *ext);
outcome.extend(merge(subs, ctx, Structure(FlatType::EmptyRecord)));
outcome
}
(Record(fields1, ext1), Record(fields2, ext2)) => {