Skip to content

ICE: contracts: No HirId for DefId #145333

@cushionbadak

Description

@cushionbadak

Code

#![feature(contracts)]
#![allow(incomplete_features)]

use core::contracts::*;

#[ensures(|ret| *ret)]
async fn _always_true(b: bool) -> bool {
    b
}

A mutant of tests/ui/contracts/contract-const-fn.rs, const fn is modified to async fn.

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (8e62bfd31 2025-08-12)
binary: rustc
commit-hash: 8e62bfd311791bfd9dca886abdfbab07ec54d8b4
commit-date: 2025-08-12
host: x86_64-apple-darwin
release: 1.91.0-nightly
LLVM version: 21.1.0

Error output

Command: rustc --edition=2021 --crate-type=lib

error: internal compiler error: /rustc-dev/8e62bfd311791bfd9dca886abdfbab07ec54d8b4/compiler/rustc_middle/src/hir/mod.rs:230:32: No HirId for DefId(0:5 ~ uncl1[c1b3]::_always_true::{closure#0})

thread 'rustc' (12855148) panicked at /rustc-dev/8e62bfd311791bfd9dca886abdfbab07ec54d8b4/compiler/rustc_middle/src/hir/mod.rs:230:32:
Box<dyn Any>
Backtrace

thread 'rustc' (12855148) panicked at /rustc-dev/8e62bfd311791bfd9dca886abdfbab07ec54d8b4/compiler/rustc_middle/src/hir/mod.rs:230:32:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>::{closure#0}
   3: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}
   4: rustc_middle::ty::context::tls::with_context_opt::<rustc_middle::ty::context::tls::with_opt<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
   5: rustc_middle::util::bug::bug_fmt
   6: <rustc_middle::hir::provide::{closure#0} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId)>>::call_once
      [... omitted 1 frame ...]
   7: <rustc_middle::hir::provide::{closure#5} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, rustc_span::def_id::LocalDefId)>>::call_once
      [... omitted 1 frame ...]
   8: <rustc_metadata::rmeta::encoder::EncodeContext>::encode_def_ids
   9: <rustc_metadata::rmeta::encoder::EncodeContext>::encode_crate_root
  10: rustc_metadata::rmeta::encoder::encode_metadata::{closure#3}::{closure#0}
  11: <rustc_metadata::rmeta::encoder::encode_metadata::{closure#3} as core::ops::function::FnOnce<(rustc_middle::ty::context::TyCtxt, &std::path::Path)>>::call_once
  12: rustc_metadata::rmeta::encoder::encode_metadata
  13: rustc_metadata::fs::encode_and_write_metadata
  14: <rustc_interface::queries::Linker>::codegen_and_build_linker
  15: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
  16: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please make sure that you have updated to the latest nightly

note: please attach the file at `/Volumes/T7/workspace/scratch_250716/rustc-ice-2025-08-13T04_32_16-88020.txt` to your bug report

note: compiler flags: --crate-type lib

query stack during panic:
#0 [local_def_id_to_hir_id] getting HIR ID of `_always_true::{closure#0}`
#1 [def_span] looking up span for `_always_true::{closure#0}`
end of query stack
error: aborting due to 1 previous error

Notes

  • ICE location: rustc_middle/src/hir/mod.rs Line-230

    pub fn provide(providers: &mut Providers) {
    providers.hir_crate_items = map::hir_crate_items;
    providers.crate_hash = map::crate_hash;
    providers.hir_module_items = map::hir_module_items;
    providers.local_def_id_to_hir_id = |tcx, def_id| match tcx.hir_crate(()).owners[def_id] {
    MaybeOwner::Owner(_) => HirId::make_owner(def_id),
    MaybeOwner::NonOwner(hir_id) => hir_id,
    MaybeOwner::Phantom => bug!("No HirId for {:?}", def_id),
    };

  • If you put empty main function fn main() {} at the end of the above ICE example and run rustc --edition=2021 bug.rs, then ICE disappears. In my observation, --crate-type=lib needed to trigger ICE.

Related Issues

  • Currently, two issues, issue-144594 and issue-123629 point to the identical ICE location, but they're not related to contracts feature.
  • Closed issue 136683 might be related to this one.

@rustbot label +F-contracts

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-HIRArea: The high-level intermediate representation (HIR)A-metadataArea: Crate metadataC-bugCategory: This is a bug.F-contracts`#![feature(contracts)]`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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions