1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::rc::Rc;
use std::sync::Arc;
use mago_codex::ttype::TType;
use mago_codex::ttype::atomic::TAtomic;
use mago_codex::ttype::atomic::array::TArray;
use mago_codex::ttype::atomic::array::key::ArrayKey;
use mago_codex::ttype::atomic::array::keyed::TKeyedArray;
use mago_codex::ttype::atomic::array::list::TList;
use mago_codex::ttype::get_int;
use mago_codex::ttype::union::TUnion;
use mago_reporting::Annotation;
use mago_reporting::Issue;
use mago_span::HasSpan;
use mago_syntax::ast::Expression;
use mago_syntax::ast::Unset;
use crate::analyzable::Analyzable;
use crate::artifacts::AnalysisArtifacts;
use crate::code::IssueCode;
use crate::context::Context;
use crate::context::block::BlockContext;
use crate::error::AnalysisError;
use crate::utils::expression::get_expression_id;
impl<'ast, 'arena> Analyzable<'ast, 'arena> for Unset<'arena> {
fn analyze<'ctx>(
&'ast self,
context: &mut Context<'ctx, 'arena>,
block_context: &mut BlockContext<'ctx>,
artifacts: &mut AnalysisArtifacts,
) -> Result<(), AnalysisError> {
let was_inside_unset = block_context.flags.inside_unset();
block_context.flags.set_inside_unset(true);
for value in &self.values {
let was_inside_general_use = block_context.flags.inside_general_use();
block_context.flags.set_inside_general_use(true);
value.analyze(context, block_context, artifacts)?;
block_context.flags.set_inside_general_use(was_inside_general_use);
let value_id = get_expression_id(
value,
block_context.scope.get_class_like_name(),
context.resolved_names,
Some(context.codebase),
);
if let Some(value_id) = &value_id {
block_context.remove_variable(value_id, true, context);
block_context.references_possibly_from_confusing_scope.remove(value_id);
}
'array_access: {
let Expression::ArrayAccess(array_access) = value else {
break 'array_access;
};
let Some(array_id) = get_expression_id(
array_access.array,
block_context.scope.get_class_like_name(),
context.resolved_names,
Some(context.codebase),
) else {
break 'array_access;
};
let Some(key_type) = artifacts.get_expression_type(array_access.index) else {
break 'array_access;
};
let Some(array_variable) = block_context.locals.remove(&array_id) else {
break 'array_access;
};
let mut atomics = vec![];
let array_key = key_type.get_single_array_key();
for atomic in Rc::unwrap_or_clone(array_variable).types.into_owned() {
if let TAtomic::Scalar(scalar) = &atomic {
let scalar_str = scalar.get_id();
context.collector.report_with_code(
IssueCode::InvalidUnset,
Issue::error(format!(
"Cannot apply `unset` to an offset of non-array type `{scalar_str}`."
))
.with_annotation(
Annotation::primary(array_access.array.span())
.with_message(format!("This has type `{scalar_str}`, not an array."))
)
.with_annotation(
Annotation::secondary(self.unset.span)
.with_message("`unset` used here on a non-array type.")
)
.with_note(
"`unset` on an array offset requires the base variable to be an array or an object that implements `ArrayAccess`."
)
.with_note(
format!("Using `unset` on an offset of type `{scalar_str}` will cause a runtime error.")
)
.with_help(
"Ensure the variable is an array before attempting to unset an element."
),
);
atomics.push(atomic);
continue;
}
let TAtomic::Array(array) = atomic else {
atomics.push(atomic);
continue;
};
match array {
TArray::List(array) => {
let TList { element_type, known_elements, known_count, non_empty } = array;
let Some(mut known_elements) = known_elements else {
atomics.push(TAtomic::Array(TArray::Keyed(TKeyedArray {
known_items: None,
parameters: Some((Arc::new(get_int()), element_type)),
non_empty: if let Some(known_count) = known_count {
known_count > 1 // we removed 1, so we are non-empty if we had more than 1
} else {
false
},
})));
continue;
};
let Some(ArrayKey::Integer(target_index)) = &array_key else {
if array_key.is_none() {
atomics.push(TAtomic::Array(TArray::List(TList {
known_elements: Some(
// We don't know the key value, so we can't unset it.
// Mark all items as potentially undefined.
known_elements
.into_iter()
.map(|(index, mut element)| {
// Mark the item as potentially undefined.
element.0 = true;
(index, element)
})
.collect(),
),
element_type,
known_count,
// Mark the list as potentially undefined.
non_empty: true,
})));
} else {
// Keep everything as is, attempting to remove a string key from a list
// makes no sense.
// An error will be emitted when we are analyzing the expression above, so ignore it here.
atomics.push(TAtomic::Array(TArray::List(TList {
known_elements: Some(known_elements),
element_type,
known_count,
non_empty,
})));
}
continue;
};
let is_fixed_list = element_type.is_never();
let maintain_list = is_fixed_list && {
let elements_count = known_elements.len();
let last_index = elements_count - 1;
*target_index == (last_index as i64)
};
let mut element_removed = false;
known_elements.retain(|index, _| {
if *target_index < 0 {
// this is a negative index, which means we can't remove it
// from the list
true
} else {
let index = *index as i64;
if index == *target_index {
element_removed = true;
false
} else {
true
}
}
});
if !element_removed {
atomics.push(TAtomic::Array(TArray::List(TList {
known_elements: Some(known_elements),
element_type,
known_count,
non_empty,
})));
continue;
}
let non_empty = !known_elements.is_empty();
let known_count = Some(known_elements.len());
let known_elements = if known_elements.is_empty() {
// Completely empty now.
None
} else {
Some(known_elements)
};
if maintain_list {
atomics.push(TAtomic::Array(TArray::List(TList {
known_count,
non_empty,
known_elements,
element_type,
})));
} else {
// we removed an integer index from a list such as:
// `list{1, 2, 3, ...<int>}`, which means the keys are no longer sequential.
// this makes the list no longer a list array, but rather a keyed array.
// we need to convert the list into a keyed array.
atomics.push(TAtomic::Array(TArray::Keyed(TKeyedArray {
known_items: known_elements.map(|known_elements| {
known_elements
.into_iter()
.map(|(index, element)| (ArrayKey::Integer(index as i64), element))
.collect()
}),
parameters: if element_type.is_never() {
None
} else {
Some((Arc::new(get_int()), element_type))
},
non_empty,
})));
}
}
TArray::Keyed(array) => {
let TKeyedArray { known_items, parameters, non_empty } = array;
let Some(mut known_items) = known_items else {
atomics.push(TAtomic::Array(TArray::Keyed(TKeyedArray {
known_items: None,
parameters,
// We don't have any known items, so we can't unset anything.
// Mark the keyed array as potentially empty.
non_empty: false,
})));
continue;
};
let Some(array_key) = &array_key else {
atomics.push(TAtomic::Array(TArray::Keyed(TKeyedArray {
known_items: Some(
// We don't know the key value, so we can't unset it.
// Mark all items as potentially undefined.
known_items
.into_iter()
.map(|(key, mut item)| {
// Mark the item as potentially undefined.
item.0 = true;
(key, item)
})
.collect(),
),
parameters,
non_empty,
})));
continue;
};
let Some(_) = known_items.remove(array_key) else {
atomics.push(TAtomic::Array(TArray::Keyed(TKeyedArray {
known_items: Some(known_items),
parameters,
non_empty,
})));
continue;
};
let (known_items, non_empty) = if known_items.is_empty() {
// Completely empty now.
(None, false)
} else {
(Some(known_items), true)
};
atomics.push(TAtomic::Array(TArray::Keyed(TKeyedArray {
known_items,
parameters,
non_empty,
})));
}
}
}
let rc = Rc::new(TUnion::from_vec(atomics));
block_context.locals.insert(array_id, rc.clone());
block_context.remove_variable_from_conflicting_clauses(context, array_id, Some(rc.as_ref()));
};
}
block_context.flags.set_inside_unset(was_inside_unset);
Ok(())
}
}