Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic in nightly rustc with type interference for const_generics #82034

Closed
Zettroke opened this issue Feb 12, 2021 · 4 comments · Fixed by #88166
Closed

Panic in nightly rustc with type interference for const_generics #82034

Zettroke opened this issue Feb 12, 2021 · 4 comments · Fixed by #88166
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Zettroke
Copy link

Code

#![feature(const_generics)]
#![feature(const_evaluatable_checked)]
#![allow(incomplete_features)]
pub trait IsTrue {}
pub trait IsFalse {}

pub struct Assert<const CHECK: bool> {}

impl IsTrue for Assert<true> {}
impl IsFalse for Assert<false> {}

pub struct SliceConstWriter<'a, const N: usize> {
    ptr: &'a mut [u8]
}
impl<'a, const N: usize> SliceConstWriter<'a, {N}> {
    pub fn from_slice(vec: &'a mut [u8]) -> Self {
        Self {
            ptr: vec
        }
    }

    pub fn convert<const NN: usize>(mut self) -> SliceConstWriter<'a, {NN}> {
        SliceConstWriter {
            ptr: self.ptr
        }
    }
}

impl<'a, const N: usize> SliceConstWriter<'a, {N}> where Assert::<{N >= 2}>: IsTrue {
    // broken
    pub fn write_u8(mut self) -> SliceConstWriter<'a, {N-2}> {
        self.convert()
    }
    
    //working
    // pub fn write_u8(mut self) -> SliceConstWriter<'a, {N-2}> {
    //     SliceConstWriter {
    //         ptr: self.ptr
    //     }
    // }
}


#[cfg(test)]
mod tests {
    use crate::SliceConstWriter;

    #[test]
    fn it_works() {
        let mut buff = [0u8; 128];
        let mut a = SliceConstWriter::<10>::from_slice(&mut buff);

        let mut a = a.write_u8().write_u8().write_u8().write_u8().write_u8();
    }
}

Reproduces in playground

Meta

rustc --version --verbose:

rustc 1.52.0-nightly (e9920ef77 2021-02-11)
binary: rustc
commit-hash: e9920ef7749d11fc71cc32ca4ba055bcfeaab945
commit-date: 2021-02-11
host: x86_64-pc-windows-gnu
release: 1.52.0-nightly
LLVM version: 11.0.1

Error output

error: internal compiler error: compiler\rustc_middle\src\ich\impls_ty.rs:94:17: StableHasher: unexpected region '_#2r

thread 'rustc' panicked at 'Box<Any>', /rustc/e9920ef7749d11fc71cc32ca4ba055bcfeaab945\library\std\src\panic.rs:59:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

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: rustc 1.52.0-nightly (e9920ef77 2021-02-11) running on x86_64-pc-windows-gnu

note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type lib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [type_op_ascribe_user_type] evaluating `type_op_ascribe_user_type` `Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }, CanonicalVarInfo { kind: Region(U0) }, CanonicalVarInfo { kind: Region(U0) }, CanonicalVarInfo { kind: Region(U0) }, CanonicalVarInfo { kind: Const(U0) }, Canon
icalVarInfo { kind: Ty(General(U0)) }], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [Binder(ConstEvaluatable(WithOptConstParam { did: DefId(0:29 ~ const_writer[f037]::{impl#3}::write_u8::{constant#0}), const_param_did: None }, [ReLateBound(DebruijnIndex(1), BoundRegion { kind: BrAnon(0) }), Const
{ ty: usize, val: Param(N/#1) }])), Binder(ConstEvaluatable(WithOptConstParam { did: DefId(0:26 ~ const_writer[f037]::{impl#3}::{constant#0}), const_param_did: None }, [ReLateBound(DebruijnIndex(1), BoundRegion { kind: BrAnon(0) }), Const { ty: usize, val: Param(N/#1) }])), Binder(TraitPredicate(<Assert<{N >= 2
}> as IsTrue>))], reveal: UserFacing }, value: AscribeUserType { mir_ty: fn(SliceConstWriter<'_, N>) -> SliceConstWriter<'_, {N-2}> {SliceConstWriter::<'_, N>::convert::<{N-2}>}, def_id: DefId(0:20 ~ const_writer[f037]::{impl#2}::convert), user_substs: UserSubsts { substs: [ReLateBound(DebruijnIndex(0), BoundRe
gion { kind: BrAnon(3) }), Const { ty: ^5, val: Bound(DebruijnIndex(0), 4) }, Const { ty: usize, val: Unevaluated(WithOptConstParam { did: DefId(0:30 ~ const_writer[f037]::{impl#3}::write_u8::{constant#1}), const_param_did: Some(DefId(0:21 ~ const_writer[f037]::{impl#2}::convert::NN)) }, [ReLateBound(DebruijnIn
dex(0), BoundRegion { kind: BrAnon(0) }), Const { ty: usize, val: Param(N/#1) }], None) }], user_self_ty: None } } } }`
#1 [mir_borrowck] borrow-checking `SliceConstWriter::<'a, N>::write_u8`
end of query stack
error: aborting due to previous error; 1 warning emitted

Backtrace

error: internal compiler error: compiler\rustc_middle\src\ich\impls_ty.rs:94:17: StableHasher: unexpected region '_#2r

thread 'rustc' panicked at 'Box<Any>', /rustc/e9920ef7749d11fc71cc32ca4ba055bcfeaab945\library\std\src\panic.rs:59:5
stack backtrace:
   0: std::panicking::begin_panic
   1: std::panic::panic_any
   2: rustc_errors::HandlerInner::bug
   3: rustc_errors::Handler::bug
   4: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   5: rustc_middle::ty::context::tls::with_opt::{{closure}}
   6: rustc_middle::ty::context::tls::with_opt
   7: rustc_middle::util::bug::opt_span_bug_fmt
   8: rustc_middle::util::bug::bug_fmt
   9: rustc_middle::ich::impls_ty::<impl rustc_data_structures::stable_hasher::HashStable<rustc_middle::ich::hcx::StableHashingContext> for rustc_middle::ty::sty::RegionKind>::hash_stable
  10: std::thread::local::LocalKey<T>::with
  11: <T as rustc_query_system::dep_graph::dep_node::DepNodeParams<Ctxt>>::to_fingerprint
  12: rustc_query_system::query::plumbing::get_query_impl
  13: rustc_trait_selection::traits::fulfill::FulfillProcessor::progress_changed_obligations
  14: rustc_data_structures::obligation_forest::ObligationForest<O>::process_obligations
  15: <rustc_trait_selection::traits::fulfill::FulfillmentContext as rustc_infer::traits::engine::TraitEngine>::select_where_possible
  16: rustc_infer::infer::canonical::query_response::<impl rustc_infer::infer::InferCtxt>::make_canonicalized_query_response
  17: <rustc_infer::infer::InferCtxtBuilder as rustc_trait_selection::infer::InferCtxtBuilderExt>::enter_canonical_trait_query
  18: rustc_traits::type_op::type_op_ascribe_user_type
  19: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::type_op_ascribe_user_type>::compute
  20: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  21: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  22: rustc_data_structures::stack::ensure_sufficient_stack
  23: rustc_query_system::query::plumbing::force_query_with_job
  24: rustc_query_system::query::plumbing::get_query_impl
  25: rustc_trait_selection::traits::query::type_op::ascribe_user_type::<impl rustc_trait_selection::traits::query::type_op::QueryTypeOp for rustc_middle::traits::query::type_op::AscribeUserType>::perform_query
  26: rustc_trait_selection::traits::query::type_op::QueryTypeOp::fully_perform_into
  27: <rustc_middle::ty::ParamEnvAnd<Q> as rustc_trait_selection::traits::query::type_op::TypeOp>::fully_perform
  28: rustc_mir::borrow_check::type_check::type_check
  29: rustc_mir::borrow_check::nll::compute_regions
  30: rustc_mir::borrow_check::do_mir_borrowck
  31: rustc_infer::infer::InferCtxtBuilder::enter
  32: rustc_mir::borrow_check::mir_borrowck
  33: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::mir_borrowck>::compute
  34: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  35: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  36: rustc_data_structures::stack::ensure_sufficient_stack
  37: rustc_query_system::query::plumbing::force_query_with_job
  38: rustc_query_system::query::plumbing::get_query_impl
  39: rustc_query_system::query::plumbing::ensure_query_impl
  40: rustc_interface::passes::analysis
  41: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute
  42: rustc_middle::dep_graph::<impl rustc_query_system::dep_graph::DepKind for rustc_middle::dep_graph::dep_node::DepKind>::with_deps
  43: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl
  44: rustc_data_structures::stack::ensure_sufficient_stack
  45: rustc_query_system::query::plumbing::force_query_with_job
  46: rustc_query_system::query::plumbing::get_query_impl
  47: rustc_interface::passes::QueryContext::enter
  48: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter
  49: rustc_span::with_source_map
  50: rustc_interface::interface::create_compiler_and_run
  51: scoped_tls::ScopedKey<T>::set

Description

Caught this error while playing with const generics.

After some experiments I concluded error caused by type inference for SliceConstWriter::convert method

Error very elusive. Even replacing test block with main function "fixed" it

@Zettroke Zettroke added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 12, 2021
@jonas-schievink jonas-schievink added F-generic_const_exprs `#![feature(generic_const_exprs)]` F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels Feb 12, 2021
@fanninpm
Copy link

Confusingly, I haven't (yet) been able to reproduce this without using cargo. @Alexendoo @JohnTitor mind taking a look at this?

@Alexendoo
Copy link
Member

Sure, if you pass --verbose to cargo it'll tell you what rustc command it's running, then you can whittle away flags

Here rustc -C incremental=foo gets it to fail (if you add a main)

fanninpm added a commit to fanninpm/glacier that referenced this issue Feb 16, 2021
@fanninpm
Copy link

@Alexendoo thanks! rustc --test -C incremental=foo did the trick.

@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Feb 16, 2021
@hellow554
Copy link
Contributor

Is this a dup of #77708 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants