{"record":{"id":"3a596eccaf755242","repo":"rust-lang/rust-analyzer","slug":"cannot-rename-self","errorCode":null,"errorMessage":"Cannot rename `Self`","messagePattern":"Cannot rename `Self`","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide-db/src/rename.rs","lineNumber":119,"sourceCode":"                bail!(\"Cannot rename a non-local definition\")\n            }\n            krate.edition(sema.db)\n        } else {\n            Edition::LATEST\n        };\n\n        match *self {\n            Definition::Module(module) => rename_mod(sema, module, new_name),\n            Definition::ToolModule(_) => {\n                bail!(\"Cannot rename a tool module\")\n            }\n            Definition::BuiltinType(_) => {\n                bail!(\"Cannot rename builtin type\")\n            }\n            Definition::BuiltinAttr(_) => {\n                bail!(\"Cannot rename a builtin attr.\")\n            }\n            Definition::SelfType(_) => bail!(\"Cannot rename `Self`\"),\n            Definition::Macro(mac) => rename_reference(\n                sema,\n                Definition::Macro(mac),\n                new_name,\n                rename_definition,\n                edition,\n                config,\n            ),\n            def => rename_reference(sema, def, new_name, rename_definition, edition, config),\n        }\n    }\n\n    /// Textual range of the identifier which will change when renaming this\n    /// `Definition`. Note that builtin types can't be\n    /// renamed and extern crate names will report its range, though a rename will introduce\n    /// an alias instead.\n    pub fn range_for_rename(self, sema: &Semantics<'_, RootDatabase>) -> Option<FileRange> {\n        let syn_ctx_is_root = |(range, ctx): (_, SyntaxContext)| ctx.is_root().then_some(range);","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide-db/src/rename.rs#L101-L137","documentation":"`Definition::rename` refuses to rename `Definition::SelfType`. `Self` is a keyword-like alias for the implementing type, not a real name in scope; rewriting its occurrences would require changing the underlying type's name instead. The library bails rather than silently doing nothing or producing wrong edits.","triggerScenarios":"Calling `Definition::rename` (crates/ide-db/src/rename.rs:119) on a definition resolved to `SelfType`, e.g. placing the cursor on `Self` in `impl Foo { fn new() -> Self }` and renaming.","commonSituations":"Developer invokes rename on `Self` inside an impl block or trait definition and the IDE returns this error instead of a workspace edit.","solutions":["Resolve the impl target type and rename that type definition instead of `Self`.","Use find-references on `Self` to locate the impl, then rename the concrete type.","Filter out `Definition::SelfType(_)` in the caller before dispatching to rename."],"exampleFix":"// before\n// rename `Self` directly -> error\n// after: rename the implementing type\nlet ty = impl_self_ty(sema); // resolve SelfType to Foo\nif let Some(adt_def) = ty.as_adt() {\n    Definition::from(adt_def).rename(sema, \"NewFoo\", ...)?;\n}","handlingStrategy":"validation","validationCode":"if matches!(def, Definition::SelfType(_)) {\n    return Err(anyhow!(\"rename the implementing type instead of `Self`\"));\n}","typeGuard":"fn is_self_type(def: &Definition) -> bool {\n    matches!(def, Definition::SelfType(_))\n}","tryCatchPattern":"match def.rename(sema, new_name, ...) {\n    Err(e) if e.to_string().contains(\"Cannot rename `Self`\") => redirect_to_impl_target_rename(),\n    other => other?,\n}","preventionTips":["Resolve `Self` to the impl's self type and rename that definition instead.","Disable rename UI on the `Self` token in editors.","Teach users that `Self` is an alias, not a name."],"tags":["rust","ide","rename","self-type"],"backgroundTag":"rename-not-supported-for-definition","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"}