rust-lang/rust-analyzer · error

Cannot rename alias reference to `self`

Error message

Cannot rename alias reference to `self`

What it means

Renaming a `use`-tree alias to `self` is rejected. `self` as LowercaseSelf identifier would produce `use foo::bar as self`, which is not legal Rust (self-imports have dedicated syntax and semantics the alias fallback does not support).

Source

Thrown at crates/ide/src/rename.rs:175

    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!(),
                    };

                    let mut source_change = SourceChange::default();
                    source_change.extend(usages.references.get_mut(&file_id).iter().map(|refs| {
                        (
                            position.file_id,

View on GitHub (pinned to e8f7e90aa3)

Solutions

  1. Choose any identifier other than `self` for the alias.
  2. For importing a module as `self`, write `use foo::self;` or `use foo;` directly in source; rust-analyzer rename does not generate it.

Example fix

// before
rename(pos, "self")?; // error
// after
rename(pos, "foo_module")?; // use foo::bar as foo_module;
Defensive patterns

Strategy: validation

Validate before calling

// Reject `self` as a new alias name
if new_name == "self" {
    return Err("alias cannot be `self`");
}

Type guard

fn is_renameable_name(name: &str) -> bool {
    !matches!(name, "self" | "Self" | "_" | "crate" | "super")
        && name.chars().all(|c| c.is_alphanumeric() || c == '_')
}

Try / catch

match rename(db, pos, new_name, &config) {
    Ok(change) => apply(change),
    Err(e) if e.to_string().contains("Cannot rename alias reference to `self`") => {
        prompt_user("`self` is not a valid alias name");
    }
    Err(e) => report(e),
}

Prevention

When it happens

Trigger: Calling rename with position on a `use` tree path segment (alias_fallback active) and new_name == "self" (IdentifierKind::LowercaseSelf).

Common situations: Refactor scripts that rename imports to lowercase names; users pressing F2 on an import and typing `self` expecting a `use foo::self` behavior.

Related errors


AI-assisted analysis of rust-lang/rust-analyzer@e8f7e90aa3 (2026-09-03). Data as JSON: /api/errors/9c9e93b2b867b7f8. Report an issue: GitHub.