{"record":{"id":"ce0ab2e4307b81ee","repo":"rust-lang/rust-analyzer","slug":"parameter-type-differs-from-impl-block-type","errorCode":null,"errorMessage":"Parameter type differs from impl block type","messagePattern":"Parameter type differs from impl block type","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":547,"sourceCode":"    let impl_ = match assoc_item.container(sema.db) {\n        hir::AssocItemContainer::Trait(_) => {\n            bail!(\"Cannot rename parameter to self for trait functions\");\n        }\n        hir::AssocItemContainer::Impl(impl_) => impl_,\n    };\n    let first_param_ty = first_param.ty();\n    let impl_ty = impl_.self_ty(sema.db);\n    let (ty, self_param) = if impl_ty.is_reference() {\n        // if the impl is a ref to the type we can just match the `&T` with self directly\n        (first_param_ty.clone(), \"self\")\n    } else {\n        first_param_ty.as_reference_inner().map_or((first_param_ty.clone(), \"self\"), |ty| {\n            (ty, if first_param_ty.is_mutable_reference() { \"&mut self\" } else { \"&self\" })\n        })\n    };\n\n    if ty != impl_ty {\n        bail!(\"Parameter type differs from impl block type\");\n    }\n\n    let InFile { file_id, value: param_source } = sema\n        .source(first_param.clone())\n        .ok_or_else(|| format_err!(\"No source for parameter found\"))?;\n\n    let def = Definition::Local(local);\n    let usages = def.usages(sema).all();\n    let mut source_change = SourceChange::default();\n    source_change.extend(usages.iter().map(|(file_id, references)| {\n        (\n            file_id.file_id(sema.db),\n            source_edit_from_references(\n                sema.db,\n                references,\n                def,\n                &Name::new_symbol_root(sym::self_),\n                file_id.edition(sema.db),","sourceCodeStart":529,"sourceCodeEnd":565,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L529-L565","documentation":"When renaming the first parameter to `self`, rust-analyzer checks that the parameter's type matches the `Self` type of the containing impl block (after stripping/adding references per the impl's self type). If the stripped parameter type differs from `impl_.self_ty()`, the resulting `self` receiver would be ill-typed, so the rename is rejected.","triggerScenarios":"Calling rename_to_self on a first parameter whose type is not the impl's self type (optionally behind a reference), e.g. in `impl Foo { fn f(x: &Bar) }` where `Bar != Foo`.","commonSituations":"Accidentally renaming a helper's parameter in a large impl block; copy-pasted method whose parameter type points to a different struct; type aliases or generics making the types subtly differ.","solutions":["Change the parameter's type to the impl's self type (e.g. `&Self` or `&Foo` matching the `impl Foo` block) before renaming to `self`.","Rename to `self` only when the signature is like `fn f(x: Foo)` / `fn f(x: &Foo)` inside `impl Foo`.","If the parameter intentionally has another type, keep it as a normal named parameter and update the body/call sites instead."],"exampleFix":"// before\nimpl Foo {\n    fn f(x: &Bar) {}\n}\n// after\nimpl Foo {\n    fn f(&self) {}\n}","handlingStrategy":"validation","validationCode":"fn can_rename_to_self(param_ty: &str, impl_ty: &str) -> bool {\n    let stripped = param_ty\n        .trim_start_matches(\"&mut \")\n        .trim_start_matches('&');\n    stripped == impl_ty || param_ty == format!(\"&{impl_ty}\") || param_ty == format!(\"&mut {impl_ty}\")\n}\n// call before rename: ensure can_rename_to_self(\"&Foo\", \"Foo\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the first parameter type matches the impl's self type (optionally by reference).","Prefer writing `&self`/`self` directly instead of a typed first parameter.","Check for type aliases obscuring the real type."],"tags":["rust","rename","impl","type-mismatch"],"backgroundTag":"receiver-type-mismatch","analyzedSha":"e8f7e90aa3e7b26aa9a000200f606c1078da99ec","analyzedAt":"2026-09-03T21:08:06.959Z","contentChangedAt":"2026-09-03T21:08:06.959Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}