{"record":{"id":"7e3d10ecb6426454","repo":"rust-lang/rust","slug":"empty-prefix-in-a-simple-import","errorCode":null,"errorMessage":"empty prefix in a simple import","messagePattern":"empty prefix in a simple import","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_ast/src/ast.rs","lineNumber":3376,"sourceCode":"    Glob(Span),\n}\n\n/// A tree of paths sharing common prefixes.\n/// Used in `use` items both at top-level and inside of braces in import groups.\n#[derive(Clone, Encodable, Decodable, Debug, Walkable)]\npub struct UseTree {\n    pub prefix: Path,\n    pub kind: UseTreeKind,\n}\n\nimpl UseTree {\n    /// If the `UseTree` is just an identifier, return that.\n    /// Panics if it's a glob (`*`) or a nested use tree.\n    pub fn ident(&self) -> Ident {\n        match self.kind {\n            UseTreeKind::Simple(Some(rename)) => rename,\n            UseTreeKind::Simple(None) => {\n                self.prefix.segments.last().expect(\"empty prefix in a simple import\").ident\n            }\n            _ => panic!(\"`UseTree::ident` can only be used on a simple import\"),\n        }\n    }\n\n    /// Returns the full span from the start of the path to the\n    /// closing `}` or nested spans, `*` of glob spans or the end of the\n    /// identifier of simple spans.\n    pub fn span(&self) -> Span {\n        self.prefix.span.to(self.hi_span())\n    }\n\n    /// Returns the trailing element's span. So for a nested\n    /// span you get the entire `{}`-block, for a glob you\n    /// get the span of the `*` itself, and for simple use trees\n    /// you get the identifier to rename the import to or the full\n    /// path if no rename is specified.\n    pub fn hi_span(&self) -> Span {","sourceCodeStart":3358,"sourceCodeEnd":3394,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_ast/src/ast.rs#L3358-L3394","documentation":"UseTree::ident() (ast.rs:3376) panics with expect(\"empty prefix in a simple import\") when called on a Simple use tree with no rename whose prefix path has zero segments. The method contract is: it returns the trailing identifier of a simple import; for a glob or nested tree it panics with a different message. An empty prefix on a Simple tree means malformed AST.","triggerScenarios":"Programmatically constructing or lowering an ast::UseTree with UseTreeKind::Simple(None) and an empty prefix.segments, then calling .ident(); or a parser/resolver bug producing such a tree.","commonSituations":"Tooling or proc-macro-adjacent code that builds UseTree nodes synthetically; a malformed `use` statement; a bug in a frontend that strips the prefix path. Well-formed source never triggers this.","solutions":["Before calling .ident(), ensure the tree is Simple and prefix.segments is non-empty.","If you construct UseTree nodes, always populate prefix with at least one segment for Simple imports.","If this fires from rustc itself on real source, file an ICE with the minimized `use` statement.","Switch to a method that does not assume a single identifier (handle Simple(rename), Glob, and Nested explicitly)."],"exampleFix":"// before\nlet name = use_tree.ident(); // panics if prefix is empty\n\n// after\nlet name = match use_tree.kind {\n    UseTreeKind::Simple(Some(rename)) => rename,\n    UseTreeKind::Simple(None) => use_tree.prefix.segments\n        .last()\n        .expect(\"simple import needs a prefix segment\").ident,\n    _ => panic!(\"ident() only valid for simple imports\"),\n};","handlingStrategy":"validation","validationCode":"fn simple_ident_or_none(tree: &UseTree) -> Option<Ident> {\n    match tree.kind {\n        UseTreeKind::Simple(Some(rename)) => Some(rename),\n        UseTreeKind::Simple(None) => tree.prefix.segments.last().map(|s| s.ident),\n        _ => None,\n    }\n}","typeGuard":"fn is_simple_with_prefix(tree: &UseTree) -> bool {\n    matches!(tree.kind, UseTreeKind::Simple(_)) && !tree.prefix.segments.is_empty()\n}","tryCatchPattern":"let name = use_tree.prefix.segments.last()\n    .map(|s| s.ident)\n    .unwrap_or_else(|| panic!(\"empty prefix in a simple import at {:?}\", use_tree.prefix.span));","preventionTips":["Never call UseTree::ident() on a tree you did not construct; check kind and prefix.segments first.","When building UseTree nodes, always give Simple imports at least one prefix segment.","Handle Simple(rename), Simple(None), Glob, and Nested as distinct cases rather than assuming ident().","If it fires from rustc on real source, file an ICE with the minimized `use` statement."],"tags":["rust","ast","use-import","panic"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}