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
use super::binary::{operand_contains_null, operand_is_non_bitwise, operand_is_non_numeric};
use super::helpers::{extract_simple_var, infer_int_range_arithmetic};
use super::ExpressionAnalyzer;
use crate::flow_state::FlowState;
use mir_issues::{IssueKind, Severity};
use mir_types::{Atomic, Type};
use php_ast::ast::{BinaryOp, UnaryPostfixOp, UnaryPrefixOp};
use php_ast::owned::{UnaryPostfixExpr, UnaryPrefixExpr};
/// Returns true when every member of `ty` is definitively `true` or `bool`
/// (not `false` — that case is reserved for a future FalseOperand kind).
fn operand_is_definitely_bool(ty: &Type) -> bool {
!ty.types.is_empty()
&& !ty.is_mixed()
&& ty
.types
.iter()
.all(|a| matches!(a, Atomic::TTrue | Atomic::TBool))
}
/// Returns true when `ty` is a non-empty literal string (PHP's string++ is deprecated/invalid).
fn operand_is_non_empty_literal_string(ty: &Type) -> bool {
!ty.types.is_empty()
&& !ty.is_mixed()
&& ty.types.iter().all(|a| match a {
Atomic::TLiteralString(s) => !s.is_empty(),
_ => false,
})
}
impl<'a> ExpressionAnalyzer<'a> {
pub(super) fn analyze_unary_prefix(
&mut self,
u: &UnaryPrefixExpr,
ctx: &mut FlowState,
) -> Type {
let operand_ty = self.analyze(&u.operand, ctx);
match u.op {
UnaryPrefixOp::BooleanNot => Type::single(Atomic::TBool),
UnaryPrefixOp::Negate | UnaryPrefixOp::Plus => {
// Same operand check as binary arithmetic's `+`/`-` — an array,
// object, enum case, or non-numeric string throws a real PHP
// `TypeError`, not the "coerce to 0" fallback `negate_type`'s
// catch-all assumes.
if operand_is_non_numeric(&operand_ty) {
self.emit(
IssueKind::InvalidOperand {
op: if u.op == UnaryPrefixOp::Negate {
"-"
} else {
"+"
}
.to_string(),
left: operand_ty.to_string(),
right: String::new(),
},
Severity::Warning,
u.operand.span,
);
}
if u.op == UnaryPrefixOp::Negate {
negate_type(&operand_ty)
} else {
operand_ty
}
}
UnaryPrefixOp::BitwiseNot => {
if operand_is_non_bitwise(&operand_ty) {
self.emit(
IssueKind::InvalidOperand {
op: "~".to_string(),
left: operand_ty.to_string(),
right: String::new(),
},
Severity::Warning,
u.operand.span,
);
} else if operand_contains_null(&operand_ty) {
self.emit(
IssueKind::PossiblyNullOperand {
op: "~".to_string(),
ty: operand_ty.to_string(),
},
Severity::Info,
u.operand.span,
);
}
Type::single(Atomic::TInt)
}
UnaryPrefixOp::PreIncrement | UnaryPrefixOp::PreDecrement => {
// Same operand check as postfix ++/-- — the same PHP warning/
// deprecation fires for both forms, only postfix was flagged.
if operand_is_definitely_bool(&operand_ty)
|| operand_is_non_empty_literal_string(&operand_ty)
{
self.emit(
IssueKind::InvalidOperand {
op: if u.op == UnaryPrefixOp::PreIncrement {
"++"
} else {
"--"
}
.to_string(),
left: operand_ty.to_string(),
right: String::new(),
},
Severity::Warning,
u.operand.span,
);
}
if let Some(var_name) = extract_simple_var(&u.operand) {
let ty = ctx.get_var(&var_name);
let new_ty = if ty.contains(|t| {
matches!(
t,
Atomic::TFloat | Atomic::TIntegralFloat | Atomic::TLiteralFloat(..)
)
}) {
Type::single(Atomic::TFloat)
} else {
let op = if u.op == UnaryPrefixOp::PreIncrement {
BinaryOp::Add
} else {
BinaryOp::Sub
};
let one = Type::single(Atomic::TLiteralInt(1));
infer_int_range_arithmetic(&ty, &one, op)
.unwrap_or_else(|| Type::single(Atomic::TInt))
};
ctx.set_var(&var_name, new_ty.clone());
let (line, col_start) = self.offset_to_line_col(u.operand.span.start);
let (line_end, col_end) = self.offset_to_line_col(u.operand.span.end);
ctx.record_var_location(&var_name, line, col_start, line_end, col_end);
new_ty
} else {
Type::single(Atomic::TInt)
}
}
}
}
pub(super) fn analyze_unary_postfix(
&mut self,
u: &UnaryPostfixExpr,
ctx: &mut FlowState,
) -> Type {
let operand_ty = self.analyze(&u.operand, ctx);
match u.op {
UnaryPostfixOp::PostIncrement | UnaryPostfixOp::PostDecrement => {
let op_str = if u.op == UnaryPostfixOp::PostIncrement {
"++"
} else {
"--"
};
// Flag increment/decrement on a definitely-bool operand (PHP
// silently no-ops; TFalse reserved for future FalseOperand kind)
// or on a non-empty literal string (deprecated in PHP 8.3+).
if operand_is_definitely_bool(&operand_ty)
|| operand_is_non_empty_literal_string(&operand_ty)
{
self.emit(
IssueKind::InvalidOperand {
op: op_str.to_string(),
left: operand_ty.to_string(),
right: String::new(),
},
Severity::Warning,
u.operand.span,
);
}
if let Some(var_name) = extract_simple_var(&u.operand) {
let new_ty = if operand_ty.contains(|t| {
matches!(
t,
Atomic::TFloat | Atomic::TIntegralFloat | Atomic::TLiteralFloat(..)
)
}) {
Type::single(Atomic::TFloat)
} else {
let op = if u.op == UnaryPostfixOp::PostIncrement {
BinaryOp::Add
} else {
BinaryOp::Sub
};
let one = Type::single(Atomic::TLiteralInt(1));
infer_int_range_arithmetic(&operand_ty, &one, op)
.unwrap_or_else(|| Type::single(Atomic::TInt))
};
ctx.set_var(&var_name, new_ty);
let (line, col_start) = self.offset_to_line_col(u.operand.span.start);
let (line_end, col_end) = self.offset_to_line_col(u.operand.span.end);
ctx.record_var_location(&var_name, line, col_start, line_end, col_end);
}
operand_ty
}
}
}
}
/// Infer the result type of the unary negation operator (`-$x`).
///
/// Propagates literal and range information through negation so that
/// e.g. `-7` gives `int<-7,-7>` rather than bare `int`, and
/// `-positive-int` gives `negative-int`.
fn negate_type(ty: &Type) -> Type {
if ty.is_mixed() {
return Type::mixed();
}
// Safe negation of an optional bound: None (unbounded) stays None; overflow
// (i64::MIN.checked_neg() == None) falls back to None (unbounded).
let neg_bound = |v: Option<i64>| v.and_then(|n| n.checked_neg());
let mut result = Type::empty();
for a in &ty.types {
let atom = match a {
Atomic::TLiteralInt(n) => {
if let Some(neg) = n.checked_neg() {
Atomic::TLiteralInt(neg)
} else {
// i64::MIN — falls back to bare int.
Atomic::TInt
}
}
Atomic::TPositiveInt => Atomic::TNegativeInt,
Atomic::TNegativeInt => Atomic::TPositiveInt,
// non-negative-int negated is int<-∞, 0> (no named type — use TIntRange).
Atomic::TNonNegativeInt => Atomic::TIntRange {
min: None,
max: Some(0),
},
Atomic::TInt => Atomic::TInt,
Atomic::TIntRange { min, max } => {
let new_min = neg_bound(*max);
let new_max = neg_bound(*min);
match (new_min, new_max) {
(Some(1), None) => Atomic::TPositiveInt,
(Some(0), None) => Atomic::TNonNegativeInt,
(None, Some(-1)) => Atomic::TNegativeInt,
(None, None) => Atomic::TInt,
_ => Atomic::TIntRange {
min: new_min,
max: new_max,
},
}
}
Atomic::TFloat | Atomic::TIntegralFloat | Atomic::TLiteralFloat(..) => Atomic::TFloat,
_ => {
// Non-numeric operands: fallback to int (PHP coerces to 0 then negates).
if ty.contains(|t| t.is_int()) {
Atomic::TInt
} else {
Atomic::TFloat
}
}
};
result.add_type(atom);
}
result
}