rust-lang/rust-analyzer · error
Cannot alias reference to a lifetime identifier
Error message
Cannot alias reference to a lifetime identifier
What it means
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.
Source
Thrown at crates/ide/src/rename.rs:171
{
let new_name_str = new_name.display(db, edition).to_string();
return rename_elided_lifetime(position, lifetime_token, &new_name_str);
}
let defs = find_definitions(&sema, syntax, position, &new_name)?;
let alias_fallback =
alias_fallback(syntax, position, &new_name.display(db, edition).to_string());
let ops: RenameResult<Vec<SourceChange>> = match alias_fallback {
Some(_) => ok_if_any(
defs
// FIXME: This can use the `ide_db::rename_reference` (or def.rename) method once we can
// properly find "direct" usages/references.
.map(|(.., def, new_name, _)| {
match kind {
IdentifierKind::Ident => (),
IdentifierKind::Lifetime => {
bail!("Cannot alias reference to a lifetime identifier")
}
IdentifierKind::Underscore => bail!("Cannot alias reference to `_`"),
IdentifierKind::LowercaseSelf => {
bail!("Cannot rename alias reference to `self`")
}
};
let mut usages = def.usages(&sema).all();
// FIXME: hack - removes the usage that triggered this rename operation.
match usages.references.get_mut(&file_id).and_then(|refs| {
refs.iter()
.position(|ref_| ref_.range.contains_inclusive(position.offset))
.map(|idx| refs.remove(idx))
}) {
Some(_) => (),
None => never!(),
};
View on GitHub (pinned to e8f7e90aa3)
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.
Example fix
// before rename(pos, "'a")?; // Cannot alias reference to a lifetime identifier // after rename(pos, "alias_a")?; // use foo::bar as alias_a;
Defensive patterns
Strategy: validation
Validate before calling
// Reject lifetime-shaped names for alias renames
if new_name.starts_with('\'') {
return Err("alias names cannot be lifetimes");
} Type guard
fn is_plain_ident(name: &str) -> bool {
let mut chars = name.chars();
matches!(chars.next(), Some(c) if c.is_alphabetic() || c == '_')
&& chars.all(|c| c.is_alphanumeric() || c == '_')
&& !matches!(name, "self" | "_")
} Try / catch
match rename(db, pos, new_name, &config) {
Ok(change) => apply(change),
Err(e) if e.to_string().contains("Cannot alias reference to a lifetime") => {
prompt_user("Aliases require a plain identifier");
}
Err(e) => report(e),
} Prevention
- 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
When it happens
Trigger: 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").
Common situations: 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.
Related errors
- Cannot alias reference to `_`
- Cannot rename alias reference to `self`
- Renaming aliases is currently unsupported
- No file available to rename
- Invalid name `{}`: cannot rename to a keyword
AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03).
Data as JSON: /api/errors/ef448dd5bbe2e782.
Report an issue: GitHub.