rust-lang/rust · critical

must be at least one segment

Error message

must be at least one segment

What it means

expect panic in delegation generics resolution when calling delegation.path.segments.last(). The path is assumed to always have at least one segment (any path through the resolver yields a final segment for the method name), so .last() is wrapped in expect("must be at least one segment"). Hitting it means the delegation's callee path arrived empty — an upstream invariant violation in path construction.

Source

Thrown at compiler/rustc_ast_lowering/src/delegation/generics.rs:326

                    .unwrap_or(ParentSegmentArgs::NotSpecified)
            } else {
                ParentSegmentArgs::Invalid
            }
        } else {
            ParentSegmentArgs::Invalid
        };

        Ok(GenericsResolution {
            parent_args,
            sig_parent_params,
            qself_is_none,
            qself_is_infer,
            free_to_trait_delegation,
            generate_self: free_to_trait_delegation && (qself_is_none || qself_is_infer),
            trait_impl: matches!(delegation_parent_kind, DefKind::Impl { of_trait: true }),
            sig_child_params: &tcx.generics_of(sig_id).own_params,
            child_args: self.get_user_args(
                delegation.path.segments.last().expect("must be at least one segment"),
            ),
        })
    }

    fn get_user_args<'a>(&self, segment: &'a PathSegment) -> Option<&'a AngleBracketedArgs> {
        let Some(args) = &segment.args else { return None };
        let GenericArgs::AngleBracketed(args) = args else {
            self.tcx().dcx().span_delayed_bug(
                segment.span(),
                "expected angle-bracketed generic args in delegation segment",
            );

            return None;
        };

        // Treat empty args `reuse foo::<> as bar` as `reuse foo as bar`,
        // the same logic applied when we call function `fn f<T>(t: T)`
        // like that `f::<>(())`, in HIR no `<>` will be generated.

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Report the ICE to rust-lang/rust, attaching the delegation macro invocation and the nightly commit.
  2. If using a proc-macro to emit `reuse`, verify each generated path has at least one segment (e.g. `Trait::method`).
  3. Rewrite the delegation as a direct path literal `reuse Foo::bar as baz;` to avoid the empty-path case.
  4. Disable the `delegation` feature temporarily and use manual forwarding methods.

Example fix

// before (macro emits a path with no segments)
reuse  as new_name;
// after
reuse Foo::method as new_name;
Defensive patterns

Strategy: validation

Validate before calling

// delegation path must have at least one segment; an empty path crashes
// path.segments.last().expect(...). Validate before resolution:
fn nonempty_path(path: &ast::Path) -> Result<&[ast::PathSegment]> {
    if path.segments.is_empty() {
        Err("delegation path must have at least one segment")
    } else {
        Ok(&path.segments)
    }
}
let segments = nonempty_path(&delegation.path)?;

Type guard

fn has_segments(path: &ast::Path) -> bool {
    !path.segments.is_empty()
}

Try / catch

let last = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| delegation.path.segments.last().expect("must be at least one segment")));
match last {
    Some(seg) => seg,
    None => return Err("delegation path is empty"),
}

Prevention

When it happens

Trigger: Triggered only with #![feature(delegation)] when a reuse/delegation item references a callee path with zero segments, e.g. `reuse as foo;` or a path produced by a proc-macro that emits an empty Path. Resolver/AST invariants normally forbid empty paths, so this implies a malformed AST.

Common situations: Nightly regressions in the delegation resolver; proc-macros generating `reuse` items with empty or malformed paths; experimental `delegation` code where a macro erases the path. Not reachable on stable Rust.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/bbb37a4030e94272.json. Report an issue: GitHub.