Unwrap option arguments of inlined functions directly when they can't be nested
Why
An inlined function such as Option.getOr is compiled for any 'a, so its body unwraps its option with Primitive_option.valFromOption, in case the payload is itself an option. The caller knows the payload type. A switch written at the call site reads the value directly when the payload can't be undefined; the inlined copy now does the same.
The translation of a call marks each option argument whose payload type can't be undefined with an identity primitive, Pnot_nested_option. When the call is inlined, the body is rewritten for that call: unwrapping a marked parameter becomes a plain read. The marks are dropped after the last inlining pass.
addedLambda Pnot_nested_option
let intOr = (opt: option<int>) => opt->Option.getOr(0)lambda
modifiedJS an inlined Option helper
let intOr = (opt: option<int>) => opt->Option.getOr(0)let nestedOr = (opt: option<option<int>>) => opt->Option.getOr(Some(0))
js
Review
- Why is the fact kept per call and not per variable? A variable can reach a call with a type of its own:
f((Obj.magic(y): option<int>))or a coercion translates to plainy. Recordingyas not nested would also turn a correctswitch yelsewhere into a plain read. So only the inlined body is rewritten, andykeepsvalFromOptioneverywhere else. - What stays?
Option.mapstill wraps its result withPrimitive_option.some, since the body doesn't know the result type either. That needs a second mark, on the result, and is left for later.