rust-lang/rust · error
`UseTree::ident` can only be used on a simple import
Error message
`UseTree::ident` can only be used on a simple import
What it means
`panic!("\`UseTree::ident\` can only be used on a simple import")` fires when `UseTree::ident()` is called on a `UseTreeKind::Glob` (`use foo::*`) or `UseTreeKind::Nested` (`use foo::{a, b}`). The method is only meaningful for `UseTreeKind::Simple` — globs have no single ident and nested trees are groups. Calling it on other variants is a programmer error.
Source
Thrown at compiler/rustc_ast/src/ast.rs:3379
/// A tree of paths sharing common prefixes.
/// Used in `use` items both at top-level and inside of braces in import groups.
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct UseTree {
pub prefix: Path,
pub kind: UseTreeKind,
}
impl UseTree {
/// If the `UseTree` is just an identifier, return that.
/// Panics if it's a glob (`*`) or a nested use tree.
pub fn ident(&self) -> Ident {
match self.kind {
UseTreeKind::Simple(Some(rename)) => rename,
UseTreeKind::Simple(None) => {
self.prefix.segments.last().expect("empty prefix in a simple import").ident
}
_ => panic!("`UseTree::ident` can only be used on a simple import"),
}
}
/// Returns the full span from the start of the path to the
/// closing `}` or nested spans, `*` of glob spans or the end of the
/// identifier of simple spans.
pub fn span(&self) -> Span {
self.prefix.span.to(self.hi_span())
}
/// Returns the trailing element's span. So for a nested
/// span you get the entire `{}`-block, for a glob you
/// get the span of the `*` itself, and for simple use trees
/// you get the identifier to rename the import to or the full
/// path if no rename is specified.
pub fn hi_span(&self) -> Span {
match self.kind {
UseTreeKind::Simple(None) => self.prefix.span,View on GitHub (pinned to 22057b88b0)
Solutions
- Branch on `self.kind` first; call `ident()` only inside the `UseTreeKind::Simple` arm.
- For globs/nested trees, handle them explicitly (iterate `Nested.items`, or record the glob span) instead of falling through to `ident()`.
- If you must get the trailing path element generically, use `self.prefix.segments.last()` after confirming the variant.
Example fix
// before
let name = use_tree.ident();
// after
let name = match use_tree.kind {
UseTreeKind::Simple(_) => use_tree.ident(),
UseTreeKind::Glob(_) => /* handle glob */
UseTreeKind::Nested { .. } => /* handle nested group */
}; Defensive patterns
Strategy: type-guard
Validate before calling
match use_tree.kind {
UseTreeKind::Simple(_) => { let _id = use_tree.ident(); /* ok */ }
UseTreeKind::Glob | UseTreeKind::Nested(_) => { /* not a simple import */ }
} Type guard
fn is_simple_import(tree: &UseTree) -> bool {
matches!(tree.kind, UseTreeKind::Simple(_))
} Prevention
- Read the doc comment: `ident()` panics on glob or nested use trees.
- Always dispatch on `UseTreeKind` before accessing the ident.
- For glob (`*`) or `Nested`, iterate `use_tree.items` instead of calling `ident()`.
When it happens
Trigger: Any code path that calls `UseTree::ident()` on a use tree without first checking the variant — e.g. a visitor that walks `use` declarations uniformly and unconditionally extracts `ident()`. Triggered by `use crate::{a, b};`, `use crate::*;`, or any braced/glob import passed to `ident()`.
Common situations: Lint or analysis passes over `use` items that forget to branch on `UseTreeKind`. Procedural macros rewriting imports generically. Refactors that previously only saw simple imports and later encounter globs/nested groups. Not reproducible from valid user code alone — only via API misuse.
Related errors
- empty prefix in a simple import
- if we had an explicit self, we wouldn't be here
- attribute is missing tokens: {self:?}
- Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}
- cloning statement `NodeId`s is prohibited by default, the vi
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/677e3eebe8b89a3c.json.
Report an issue: GitHub.