Skip to content
← Back

src/syntax/process/compile-expressions/undisposed_source_check.ghul

1
namespace Syntax.Process is
2
use IR.Values.Value
3
4
use Logging
5
6
// Reports a sequence that holds a resource being read in a way that
7
// can stop before it ends, with nothing holding it to dispose. A
8
// read to the end lets the sequence close itself, so only a read
9
// that can stop early is reported: an early-exit `Ghul.Pipes`
10
// function (first, find, any, take, ...), or a `for` loop whose body
11
// can leave it. The call producing the sequence has to be written
12
// directly as that read's source, or reached from it through other
13
// `Ghul.Pipes` stages; a result held by a `let use`, or anywhere
14
// else first, is not seen.
15
class UNDISPOSED_SOURCE_CHECK(
16
_logger: Logger,
17
_innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
18
) is
19
check_call(call: Trees.Expressions.CALL, value: Value?) is
20
if
21
let pipes_call: IR.Values.Call.GLOBAL = value /\
22
_innate_symbol_lookup.stops_early(pipes_call.function) /\
23
call.arguments.count > 0
24
then
25
_check_source(call.arguments.expressions[0])
26
fi
27
si
28
29
check_loop(source: Trees.Expressions.Expression, loop: Trees.Statements.Statement, body: Trees.Statements.LIST) is
30
let finder = LOOP_EXIT_FINDER(loop)
31
32
body.walk(finder)
33
34
if finder.found then
35
_check_source(source)
36
fi
37
si
38
39
// Follows `expression` down through the `Ghul.Pipes` stages built on
40
// it to the call at the bottom. The walk stops without reporting at
41
// a stage that reads all of its source, which lets the source close
42
// itself, and at an early-exit stage, which is reported where it is
43
// called.
44
_check_source(expression: Trees.Expressions.Expression) is
45
let source mut = expression
46
47
while
48
let
49
call: Trees.Expressions.CALL = source /\ call.arguments.count > 0,
50
stage: IR.Values.Call.GLOBAL = call.value /\ _innate_symbol_lookup.is_pipes_function(stage.function)
51
do
52
if
53
_innate_symbol_lookup.stops_early(stage.function) \/
54
_innate_symbol_lookup.reads_whole_source(stage.function)
55
then
56
return
57
fi
58
59
source = call.arguments.expressions[0]
60
od
61
62
if
63
let value = source.value, function = _called_function(value) /\ _innate_symbol_lookup.holds_resource(function)
64
then
65
let location = source.location
66
67
_logger.warn(location, "undisposed-source", "{function.name} is not disposed if this read stops early")
68
_logger.hint(location, "undisposed-source", "hold its iterator with use")
69
fi
70
si
71
72
_called_function(value: Value) -> Semantic.Symbols.Function? =>
73
if let call: IR.Values.Call.STATIC = value then
74
call.function
75
elif let call: IR.Values.Call.GLOBAL = value then
76
call.function
77
elif let call: IR.Values.Call.INSTANCE = value then
78
call.function
79
elif let call: IR.Values.Call.STRUCT = value then
80
call.function
81
else
82
null
83
fi
84
si
85
86
// Whether a loop body can leave the loop before its source ends: a
87
// `return`, or a `break` that targets this loop. A `break` inside a
88
// nested loop targets that loop unless it names this one, and a
89
// function literal's body is a body of its own.
90
class LOOP_EXIT_FINDER(_loop: Trees.Statements.Statement): Visitor is
91
found: bool public
92
_depth: int
93
94
pre(function: Trees.Expressions.FUNCTION) -> bool => true
95
96
pre(`for: Trees.Statements.FOR) -> bool is
97
_depth = _depth + 1
98
return false
99
si
100
101
visit(`for: Trees.Statements.FOR) is
102
_depth = _depth - 1
103
si
104
105
pre(`do: Trees.Statements.DO) -> bool is
106
_depth = _depth + 1
107
return false
108
si
109
110
visit(`do: Trees.Statements.DO) is
111
_depth = _depth - 1
112
si
113
114
visit(`break: Trees.Statements.BREAK) is
115
if let target = `break.resolved_target then
116
if target == _loop then
117
found = true
118
fi
119
elif _depth == 0 then
120
found = true
121
fi
122
si
123
124
visit(`return: Trees.Statements.RETURN) is
125
found = true
126
si
127
si
128
si