Skip to content

Commit

Permalink
fixed types so tests are happy
Browse files Browse the repository at this point in the history
  • Loading branch information
faldor20 committed Mar 17, 2024
1 parent 4612226 commit 089c499
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
45 changes: 26 additions & 19 deletions crates/compiler/derive/src/decoding/record/finalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ pub(super) fn finalizer(
.zip(field_vars.iter().rev())
.zip(result_field_vars.iter().rev())
{
let cond_expr = attempt_empty_decode_if_missing(
// [Ok field_var,Err DecodeError]
// when rec.f0 is
// Err _ ->
// when Decode.decodeWith [] Decode.decoder fmt is
// rec2 -> rec2.result
// Ok a -> Ok a
let (attempt_empty_decode_expr, attempt_empty_decode_var) = attempt_empty_decode_if_missing(
state_record_var,
env,
field_var,
Expand All @@ -130,7 +136,7 @@ pub(super) fn finalizer(
let ok_branch = WhenBranch {
patterns: vec![WhenBranchPattern {
pattern: Loc::at_zero(Pattern::AppliedTag {
whole_var: result_field_var,
whole_var: attempt_empty_decode_var,
ext_var: Variable::EMPTY_TAG_UNION,
tag_name: "Ok".into(),
arguments: vec![(field_var, Loc::at_zero(Pattern::Identifier(*symbol)))],
Expand Down Expand Up @@ -167,12 +173,12 @@ pub(super) fn finalizer(
};

body = Expr::When {
loc_cond: Box::new(Loc::at_zero(cond_expr)),
cond_var: result_field_var,
loc_cond: Box::new(Loc::at_zero(attempt_empty_decode_expr)),
cond_var: attempt_empty_decode_var,
expr_var: return_type_var,
region: Region::zero(),
branches: vec![ok_branch, err_branch],
branches_cond_var: result_field_var,
branches_cond_var: attempt_empty_decode_var,
exhaustive: ExhaustiveMark::known_exhaustive(),
};
}
Expand Down Expand Up @@ -245,16 +251,16 @@ fn attempt_empty_decode_if_missing(
fmt_arg_symbol: Symbol,
decode_err_var: Variable,
symbol: &Symbol,
) -> Expr {
) -> (Expr, Variable) {
let (decode_expr, rec_result, rec_dot_result) = decode_with(
env,
field_var,
empty_list(Variable::U8),
fmt_arg_var,
fmt_arg_symbol,
decode_err_var,
);
let decode_when = {
let (decode_expr, rec_result, rec_dot_result) = decode_with(
env,
field_var,
empty_list(Variable::U8),
fmt_arg_var,
fmt_arg_symbol,
decode_err_var,
);
let decoder_rec_symb = env.new_symbol("decRec");

let branch = WhenBranch {
Expand Down Expand Up @@ -285,7 +291,7 @@ fn attempt_empty_decode_if_missing(
}
};

// when rec.first is
// when rec.f0 is
let cond_expr = Expr::RecordAccess {
record_var: state_record_var,
ext_var: env.new_ext_var(ExtensionKind::Record),
Expand All @@ -295,7 +301,7 @@ fn attempt_empty_decode_if_missing(
};

// Example: `Ok x -> Ok x`
let ok_branch = ok_to_ok_branch(result_field_var, result_field_var, field_var, symbol, env);
let ok_branch = ok_to_ok_branch(result_field_var, rec_dot_result, field_var, symbol, env);

// Example: `_ -> Decode.partial [] Decode.decoder fmt `
let err_branch = WhenBranch {
Expand All @@ -308,13 +314,14 @@ fn attempt_empty_decode_if_missing(
redundant: RedundantMark::known_non_redundant(),
};

Expr::When {
let expr = Expr::When {
loc_cond: Box::new(Loc::at_zero(cond_expr)),
cond_var: result_field_var,
expr_var: result_field_var,
expr_var: rec_dot_result,
region: Region::zero(),
branches: vec![ok_branch, err_branch],
branches_cond_var: result_field_var,
exhaustive: ExhaustiveMark::known_exhaustive(),
}
};
(expr, rec_dot_result)
}
30 changes: 19 additions & 11 deletions crates/compiler/test_derive/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ fn record_2_fields() {
assert_snapshot!(golden, @r###"
# derived for { first : Str, second : Str }
# Decoder { first : val, second : val1 } fmt where fmt implements DecoderFormatting, val implements Decoding, val1 implements Decoding
# List U8, fmt -[[custom(22)]]-> { rest : List U8, result : [Err [TooShort], Ok { first : val, second : val1 }] } where fmt implements DecoderFormatting, val implements Decoding, val1 implements Decoding
# List U8, fmt -[[custom(25)]]-> { rest : List U8, result : [Err [TooShort], Ok { first : val, second : val1 }] } where fmt implements DecoderFormatting, val implements Decoding, val1 implements Decoding
# Specialization lambda sets:
# @<1>: [[custom(22)]]
# @<1>: [[custom(25)]]
#Derived.decoder_{first,second} =
custom
\#Derived.bytes3, #Derived.fmt3 ->
\#Derived.bytes3, #Derived.fmt4 ->
decodeWith
#Derived.bytes3
(record
Expand All @@ -139,8 +139,8 @@ fn record_2_fields() {
when #Derived.field is
"first" ->
Keep (custom
\#Derived.bytes, #Derived.fmt ->
when decodeWith #Derived.bytes decoder #Derived.fmt is
\#Derived.bytes, #Derived.fmt2 ->
when decodeWith #Derived.bytes decoder #Derived.fmt2 is
#Derived.rec ->
{
result: when #Derived.rec.result is
Expand All @@ -151,8 +151,8 @@ fn record_2_fields() {
})
"second" ->
Keep (custom
\#Derived.bytes2, #Derived.fmt2 ->
when decodeWith #Derived.bytes2 decoder #Derived.fmt2 is
\#Derived.bytes2, #Derived.fmt3 ->
when decodeWith #Derived.bytes2 decoder #Derived.fmt3 is
#Derived.rec2 ->
{
result: when #Derived.rec2.result is
Expand All @@ -162,15 +162,23 @@ fn record_2_fields() {
rest: #Derived.rec2.rest
})
_ -> Skip
\#Derived.stateRecord ->
when #Derived.stateRecord.first is
\#Derived.stateRecord, #Derived.fmt ->
when when #Derived.stateRecord.first is
Ok #Derived.first -> Ok #Derived.first
_ ->
when decodeWith [] decoder #Derived.fmt is
#Derived.decRec2 -> #Derived.decRec2.result is
Ok #Derived.first ->
when #Derived.stateRecord.second is
when when #Derived.stateRecord.second is
Ok #Derived.second -> Ok #Derived.second
_ ->
when decodeWith [] decoder #Derived.fmt is
#Derived.decRec -> #Derived.decRec.result is
Ok #Derived.second ->
Ok { second: #Derived.second, first: #Derived.first }
_ -> Err TooShort
_ -> Err TooShort)
#Derived.fmt3
#Derived.fmt4
"###
)
})
Expand Down

0 comments on commit 089c499

Please sign in to comment.