{"record":{"id":"73d32cba176eadeb","repo":"rust-lang/rust","slug":"return-depth-from-strip-n-refs-should-be-smaller-t","errorCode":null,"errorMessage":"return depth from strip_n_refs should be smaller than the input","messagePattern":"return depth from strip_n_refs should be smaller than the input","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs","lineNumber":757,"sourceCode":"\n                        let (borrowed_prefix, borrowed_index);\n\n                        // only suggest `insert` and `entry` if index is of type K or &{n}K or *{n}K (when there is a Borrow impl for this case).\n                        // We use `peel_refs` because borrow lifetimes may differ in both index and\n                        // key. I.e, if they are of the same base type:\n                        if index_ty.peel_refs() == key_ty.peel_refs() {\n                            let (index_refs, key_refs) =\n                                (count_ty_refs(index_ty), count_ty_refs(key_ty));\n\n                            let (deref_prefix, deref_index) = if index_refs >= key_refs {\n                                // index is &{n}K\n                                strip_n_refs(index, index_refs - key_refs)\n                                    .map(|val| (\"\".to_string(), val))\n                                    .unwrap_or_else(|(depth, val)| {\n                                        (\n                                            if key_refs == 0 {\n                                                \"*\".repeat(\n                                                    (index_refs-key_refs).checked_sub(depth).expect(\"return depth from strip_n_refs should be smaller than the input\")\n                                                )\n                                            } else {\n                                                String::new() //if key K is a ref, autoderef finish this for us.\n                                            },\n                                            val,\n                                        )\n                                    })\n                            } else {\n                                // in this case the minimal ref addition works for all subcases\n                                (\"&\".repeat(key_refs - index_refs), index)\n                            };\n\n                            self.err.multipart_suggestion(\n                                format!(\"use `.insert()` to insert a value into a `{}`\", self.ty),\n                                vec![\n                                    // val.insert({deref_prefix}{deref_index}, rv);\n                                    (\n                                        val.span.shrink_to_hi().with_hi(deref_index.span.lo()),","sourceCodeStart":739,"sourceCodeEnd":775,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs#L739-L775","documentation":"mutability_errors.rs:757: while suggesting `.insert()` for an index expression, the code computes (index_refs - key_refs).checked_sub(depth).expect(\"return depth from strip_n_refs should be smaller than the input\"). depth is the number of refs strip_n_refs actually stripped; the invariant guarantees depth <= index_refs - key_refs, so the subtraction never underflows.","triggerScenarios":"The index-suggestion heuristic runs on an index expression whose ref-counts make strip_n_refs return a depth larger than index_refs - key_refs, violating the arithmetic invariant.","commonSituations":"A borrowck diagnostic ICE for a HashMap/BTreeMap indexing suggestion involving mismatched reference layers; an internal heuristic bug.","solutions":["File a rustc ICE with the minimized indexing expression and the surrounding map type.","Simplify the index/key expressions (remove layers of & or *) so the suggestion heuristic takes a simpler path.","Bisect with cargo bisect-rustc.","Try the equivalent code on stable."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat as a borrowck diagnostic ICE; file a report with the minimized map-indexing expression.","Simplify index/key expressions by removing redundant reference layers to avoid the heuristic path.","Bisect with cargo bisect-rustc across nightlies.","Try equivalent code on stable to confirm a regression."],"tags":["rust","borrowck","diagnostics","ice"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}