{"record":{"id":"7bed1dd6cf9db964","repo":"huggingface/candle","slug":"slice-assign-empty-range-for-dim-i-start-incl","errorCode":null,"errorMessage":"slice-assign: empty range for dim {i}, {start_included} {end_excluded}","messagePattern":"slice-assign: empty range for dim (.+?), (.+?) (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-core/src/tensor.rs","lineNumber":2897,"sourceCode":"                self_dims.len(),\n                ranges.len()\n            )\n        }\n        let mut src = src.clone();\n        let mut mask = Self::ones(src.shape(), DType::U8, src.device())?;\n        for (i, range) in ranges.iter().enumerate() {\n            let start_included = match range.start_bound() {\n                std::ops::Bound::Unbounded => 0,\n                std::ops::Bound::Included(v) => *v,\n                std::ops::Bound::Excluded(v) => *v + 1,\n            };\n            let end_excluded = match range.end_bound() {\n                std::ops::Bound::Unbounded => self_dims[i],\n                std::ops::Bound::Included(v) => *v + 1,\n                std::ops::Bound::Excluded(v) => *v,\n            };\n            if end_excluded <= start_included {\n                bail!(\"slice-assign: empty range for dim {i}, {start_included} {end_excluded}\")\n            }\n            if self_dims[i] < end_excluded {\n                bail!(\n                    \"slice-assign: upper bound is out of range for dim {i}, {end_excluded} {}\",\n                    self_dims[i]\n                )\n            }\n            if end_excluded - start_included != src_dims[i] {\n                bail!(\n                    \"slice-assign: the range for dim {i} ({start_included}..{end_excluded}) does not match the size of src {}\", src_dims[i]\n                )\n            }\n            src = src.pad_with_zeros(i, start_included, self_dims[i] - end_excluded)?;\n            mask = mask.pad_with_zeros(i, start_included, self_dims[i] - end_excluded)?\n        }\n        mask.where_cond(/* on_true= */ &src, /* on_false= */ self)\n    }\n","sourceCodeStart":2879,"sourceCodeEnd":2915,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-core/src/tensor.rs#L2879-L2915","documentation":"For each dimension, slice_assign resolves the range's start and end bounds and requires a non-empty interval: end_excluded must be strictly greater than start_included. An empty range (e.g. 3..3 or 5..2) defines a zero-width region that cannot be assigned, so it bails naming the dim and the resolved bounds.","triggerScenarios":"Passing Range { start: 3, end: 3 } or an inverted range like 5..2 for some dim; a range built from computed start/end that collapsed to empty.","commonSituations":"Computing ranges from dynamic sizes where end == start after clamping; using an exclusive bound equal to the start; loop-generated ranges where a size was 0.","solutions":["Ensure every range has end > start; skip the dim (use 0..self_dim) if no restriction is needed","Validate ranges in caller code before invoking slice_assign","Check upstream computations that produce zero-length spans"],"exampleFix":"// before\nt.slice_assign(&[3..3, 0..4], &src)?;\n// after\nif start < end {\n    t.slice_assign(&[start..end, 0..4], &src)?;\n}","handlingStrategy":"validation","validationCode":"fn valid_range(r: &std::ops::Range<usize>) -> bool { r.start < r.end }\nassert!(ranges.iter().all(valid_range));","typeGuard":null,"tryCatchPattern":"if start >= end { return Ok(t.clone()); } // or skip dim\nt.slice_assign(&[start..end, 0..d1], &src)?;","preventionTips":["Guard computed ranges: skip assignment when start == end","Never emit inverted ranges; clamp with .max(start)","Add unit tests for boundary (empty) ranges"],"tags":["rust","candle","slice-assign","empty-range"],"backgroundTag":"slice-range-invalid","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}