{"record":{"id":"ef448dd5bbe2e782","repo":"rust-lang/rust-analyzer","slug":"cannot-alias-reference-to-a-lifetime-identifier","errorCode":null,"errorMessage":"Cannot alias reference to a lifetime identifier","messagePattern":"Cannot alias reference to a lifetime identifier","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ide/src/rename.rs","lineNumber":171,"sourceCode":"    {\n        let new_name_str = new_name.display(db, edition).to_string();\n        return rename_elided_lifetime(position, lifetime_token, &new_name_str);\n    }\n\n    let defs = find_definitions(&sema, syntax, position, &new_name)?;\n    let alias_fallback =\n        alias_fallback(syntax, position, &new_name.display(db, edition).to_string());\n\n    let ops: RenameResult<Vec<SourceChange>> = match alias_fallback {\n        Some(_) => ok_if_any(\n            defs\n                // FIXME: This can use the `ide_db::rename_reference` (or def.rename) method once we can\n                // properly find \"direct\" usages/references.\n                .map(|(.., def, new_name, _)| {\n                    match kind {\n                        IdentifierKind::Ident => (),\n                        IdentifierKind::Lifetime => {\n                            bail!(\"Cannot alias reference to a lifetime identifier\")\n                        }\n                        IdentifierKind::Underscore => bail!(\"Cannot alias reference to `_`\"),\n                        IdentifierKind::LowercaseSelf => {\n                            bail!(\"Cannot rename alias reference to `self`\")\n                        }\n                    };\n                    let mut usages = def.usages(&sema).all();\n\n                    // FIXME: hack - removes the usage that triggered this rename operation.\n                    match usages.references.get_mut(&file_id).and_then(|refs| {\n                        refs.iter()\n                            .position(|ref_| ref_.range.contains_inclusive(position.offset))\n                            .map(|idx| refs.remove(idx))\n                    }) {\n                        Some(_) => (),\n                        None => never!(),\n                    };\n","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/rust-lang/rust-analyzer/blob/e8f7e90aa3e7b26aa9a000200f606c1078da99ec/crates/ide/src/rename.rs#L153-L189","documentation":"This error comes from the alias-rename fallback path, which rewrites a `use` item path segment into an alias (e.g. turning `use foo::bar;` into `use foo::bar as baz;`). Aliasing is only defined for plain identifiers: when the requested new name is classified as a lifetime (IdentifierKind::Lifetime), the fallback rejects it with this error.","triggerScenarios":"Calling rename with position on a `use` tree path segment (alias_fallback active) and new_name starting with `'` (a lifetime name), e.g. rename(…, \"'a\").","commonSituations":"Editor rename on a use-tree import where the user (or a tool macro) supplies a lifetime-shaped name; scripting rust-analyzer's rename API with generated names that accidentally begin with a quote.","solutions":["Pass a plain identifier (not starting with `'`) as new_name for alias renames.","Renaming lifetimes themselves is supported only when the cursor is on a lifetime token, not a use-tree alias — target the lifetime definition instead."],"exampleFix":"// before\nrename(pos, \"'a\")?; // Cannot alias reference to a lifetime identifier\n// after\nrename(pos, \"alias_a\")?; // use foo::bar as alias_a;\n","handlingStrategy":"validation","validationCode":"// Reject lifetime-shaped names for alias renames\nif new_name.starts_with('\\'') {\n    return Err(\"alias names cannot be lifetimes\");\n}","typeGuard":"fn is_plain_ident(name: &str) -> bool {\n    let mut chars = name.chars();\n    matches!(chars.next(), Some(c) if c.is_alphabetic() || c == '_')\n        && chars.all(|c| c.is_alphanumeric() || c == '_')\n        && !matches!(name, \"self\" | \"_\")\n}","tryCatchPattern":"match rename(db, pos, new_name, &config) {\n    Ok(change) => apply(change),\n    Err(e) if e.to_string().contains(\"Cannot alias reference to a lifetime\") => {\n        prompt_user(\"Aliases require a plain identifier\");\n    }\n    Err(e) => report(e),\n}","preventionTips":["Validate the new name is a bare identifier before calling rename","Never pass names beginning with `'` when renaming use-tree path segments","Restrict UI input for rename to identifier-valid characters"],"tags":["rust-analyzer","rename","alias","lifetime"],"backgroundTag":"invalid-rename-identifier","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"}