Skip to content

[rustc] ICE in stability_implications: DefId::expect_local called on non-local DefId from core when using reuse with Iterator::fold under staged_api #153599

@YuanchengJiang

Description

@YuanchengJiang

Description

rustc panics with DefId::expect_local: ... isn't local when compiling a crate that uses #![feature(staged_api)] together with #![feature(fn_delegation)] and a reuse delegation that includes Iterator::fold. The stability_implications query walks the #[unstable] implications of delegated items and calls expect_local on Iterator::fold's generic type parameter B, which is a non-local DefId from core, triggering an unconditional panic.

Reproducer

#![feature(fn_delegation)]
#![allow(incomplete_features)]
#![feature(staged_api)]
#![unstable(feature = "foo", issue = "none")]

pub mod m {
    #[unstable(feature = "foo", issue = "none")]
    pub struct W<I, F> {
        pub inner: std::iter::Map<I, F>,
    }

    #[unstable(feature = "foo", issue = "none")]
    impl<B, I: Iterator, F: FnMut(I::Item) -> B> Iterator for W<I, F> {
        type Item = B;
        reuse Iterator::{next, fold} { self.inner }
    }
}

Command

rustc reproduce.rs

Expected behavior

The compiler either compiles successfully or emits a graceful diagnostic. It should not panic when computing stability implications for delegated items that reference non-local DefIds from the standard library.

Actual behavior

thread 'rustc' panicked at compiler/rustc_passes/src/stability.rs:309:55:
DefId::expect_local: `DefId(2:10014 ~ core[18bb]::iter::traits::iterator::Iterator::fold::B)` isn't local
error: the compiler unexpectedly panicked. This is a bug

Root cause

The call chain is:

check_mod_unstable_api_usage
  → check_unused_or_stable_features
    → stability_implications (stability.rs:1059)
      → stability_implications (stability.rs:309)
        → DefId::expect_local  ← panic: non-local DefId from core

stability_implications iterates over #[unstable] feature implications in the current crate. When fn_delegation's reuse Iterator::{next, fold} is processed, the compiler associates the delegated fold item with Iterator::fold's generic type parameter B (a DefId belonging to core, not the local crate). stability_implications then calls expect_local on this DefId without first checking DefId::is_local, causing the unconditional panic. The fix should guard the expect_local call with an is_local check, or ensure that fn_delegation does not register non-local DefIds as implications of local unstable features.

Environment

  • Compiler: rustc 1.95.0-nightly
  • Host target: x86_64-unknown-linux-gnu
  • rustc commit: 9b1f8ff42d110b0ca138116745be921df5dc97e7
  • Requires: #![feature(fn_delegation, staged_api)]

Metadata

Metadata

Assignees

Labels

A-stabilityArea: `#[stable]`, `#[unstable]` etc.C-bugCategory: This is a bug.F-fn_delegation`#![feature(fn_delegation)]`F-staged_api`#![feature(staged_api)]`I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.requires-internal-featuresThis issue requires the use of internal features.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions