Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions pyrefly/lib/alt/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,18 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
self.heap.mk_unpack(ty)
}
Expr::Slice(x) => {
let elt_exprs = [x.lower.as_ref(), x.upper.as_ref(), x.step.as_ref()];
let elts = elt_exprs
.iter()
.filter_map(|e| e.map(|e| self.expr_infer(e, errors)))
.collect::<Vec<_>>();
let none = self.heap.mk_none();
let elts = vec![
x.lower
.as_ref()
.map_or_else(|| none.clone(), |e| self.expr_infer(e, errors)),
x.upper
.as_ref()
.map_or_else(|| none.clone(), |e| self.expr_infer(e, errors)),
x.step
.as_ref()
.map_or_else(|| none.clone(), |e| self.expr_infer(e, errors)),
];
self.specialize(&self.stdlib.slice_class_object(), elts, x.range(), errors)
}
Expr::IpyEscapeCommand(x) => {
Expand Down Expand Up @@ -2492,6 +2499,61 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
// TODO: Handle subscription of intersections properly.
base = x.1;
}
let is_builtin_sequence = match &base {
Type::Tuple(_) => true,
Type::ClassType(cls) | Type::SelfType(cls) => {
cls.is_builtin("list")
|| (self.as_tuple(cls).is_some()
&& !self.class_overrides_tuple_getitem(cls))
}
_ => false,
};

if let Expr::Slice(slice_expr) = slice
&& is_builtin_sequence
{
let index_dunder = Name::new_static("__index__");
for (expr, is_step) in [
(&slice_expr.lower, false),
(&slice_expr.upper, false),
(&slice_expr.step, true),
]
.into_iter()
.filter_map(|(expr, is_step)| expr.as_deref().map(|expr| (expr, is_step)))
{
let ty = self.expr_infer(expr, errors);
if is_step
&& matches!(&ty, Type::Literal(lit) if lit.value.as_index_i64() == Some(0))
{
return self.error(
errors,
expr.range(),
ErrorKind::BadIndex,
"Slice step cannot be zero".to_owned(),
);
}
let valid = match &ty {
Type::Any(_) | Type::None => true,
Type::Union(u) => u.members.iter().all(|ty| {
matches!(ty, Type::Any(_) | Type::None)
|| matches!(ty, Type::Literal(lit) if lit.value.as_index_i64().is_some())
|| self.has_attr(ty, &index_dunder)
}),
Type::Literal(lit) if lit.value.as_index_i64().is_some() => true,
_ => self.has_attr(&ty, &index_dunder),
};
if !valid {
return self.error(
errors,
expr.range(),
ErrorKind::BadIndex,
"Slice indices must be integers or have an `__index__` method"
.to_owned(),
);
}
}
}

match base {
Type::Forall(forall) => {
if matches!(forall.body, Forallable::TypeAlias(_)) {
Expand Down
3 changes: 1 addition & 2 deletions pyrefly/lib/test/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -969,11 +969,10 @@ a, b = "x"
);

testcase!(
bug = "Should detect zero step size in slice",
test_slice_zero_step,
r#"
items = [1, 2, 3, 4]
bad = items[::0]
bad = items[::0] # E: Slice step cannot be zero
"#,
);

Expand Down
18 changes: 18 additions & 0 deletions pyrefly/lib/test/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ def test(x: tuple[int, str, bool], y: tuple[int, ...], start: int, stop: int, st
"#,
);

testcase!(
test_slice_decimal_step,
r#"
def test(xs: list[int], ys: tuple[int, ...]) -> None:
xs[::1.5] # E: Slice indices must be integers or have an `__index__` method
ys[::1.5] # E: Slice indices must be integers or have an `__index__` method
"#,
);

testcase!(
test_slice_zero_step,
r#"
def test(xs: list[int], ys: tuple[int, ...]) -> None:
xs[::0] # E: Slice step cannot be zero
ys[::0] # E: Slice step cannot be zero
"#,
);

testcase!(
test_slice_subset,
r#"
Expand Down
Loading