Skip to content
← Back

src/semantic/value_return_pin.ghul

1
namespace Semantic is
2
use Types.Type
3
4
// Where a function literal's inferred return settles when its body
5
// produced a value type and its slot returns a reference type.
6
//
7
// A delegate assignment cannot carry the conversion: reaching the
8
// slot's type that way needs return covariance, and a value-type
9
// instantiation is invariant however the delegate is declared, which
10
// nothing checks at run time. So the conversion is made inside the
11
// literal, and the literal's return is the body's own implementation
12
// of what the slot returns - the slot's type once the arguments it
13
// left open follow from the body.
14
class VALUE_RETURN_PIN is
15
// The type to settle at, or null to leave the body's own type
16
// alone. Null whenever the question does not arise: no slot, a
17
// slot that is not a reference type, a body that is not a value
18
// type, or a body that does not implement what the slot returns.
19
of(slot: Type?, produced: Type?) -> Type? static is
20
if !slot? \/ !produced? then
21
return null
22
fi
23
24
if !applies_to_slot(slot) then
25
return null
26
fi
27
28
if !produced.is_value_type \/ !produced.is_settled then
29
return null
30
fi
31
32
return produced.find_ancestor(slot)
33
si
34
35
// Whether a slot's return type is one a value-typed body has to be
36
// converted to rather than assigned through. Its own type
37
// arguments need not have settled: what decides this is the shape,
38
// and a slot is a reference type or is not whatever they turn out
39
// to be.
40
applies_to_slot(slot: Type?) -> bool static =>
41
slot? /\
42
!slot.is_void /\
43
!slot.is_value_type /\
44
!slot.is_type_variable
45
si
46
si