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

Compiler crash during doc test with generic_const_exprs #94664

Closed
ssande7 opened this issue Mar 6, 2022 · 2 comments
Closed

Compiler crash during doc test with generic_const_exprs #94664

ssande7 opened this issue Mar 6, 2022 · 2 comments
Labels
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.

Comments

@ssande7
Copy link

ssande7 commented Mar 6, 2022

Trying to use generic_const_exprs (#76560) to enforce bounds on a const generic is causing a compiler crash for doc tests if #![feature(generic_const_exprs)] is not included in the doc test snippet. This does not occur in all cases - eg. the TryFrom example below causes a crash, but the From example does not. Seems to be related to the Result type.

Code

cargo new --lib const_generics_bug

lib.rs:

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

pub struct If<const B: bool>;
pub trait True {}
impl True for If<true> {}

#[derive(Debug)]
pub struct FooError;

pub struct Foo<const N: usize> where If<{N>0}>: True {
    pub bar: [i32; N],
}

impl<const N: usize> TryFrom<[i32;N]> for Foo<N> 
where If<{N>0}>: True,
{
    type Error = FooError;

    /// # Examples
    /// ```
    // /// #![feature(generic_const_exprs)]   // <-- Uncommenting this line fixes the crash
    /// use const_generics_bug::{FooError, Foo};
    /// fn main() -> Result<(), FooError> {
    ///     let _baz = Foo::<4>::try_from([1,2,3,4])?;
    ///     Ok(())
    /// }
    /// ```
    fn try_from(value: [i32;N]) -> Result<Self, Self::Error> {
        Ok(Self {
            bar: value,
        })
    }
}

// This test passes
#[test]
fn test() -> Result<(), FooError> {
    let _baz = Foo::<4>::try_from([1,2,3,4])?;
    Ok(())
}


// Uncommenting the below (and commenting out test() and impl TryFrom) works fine

// impl<const N: usize> From<[i32;N]> for Foo<N> 
// where If<{N>0}>: True,
// {
//     /// # Examples
//     /// ```
//     /// use const_generics_bug::Foo;
//     /// fn main() {
//     ///     let _baz = Foo::<4>::from([1,2,3,4]);
//     /// }
//     /// ```
//     fn from(value: [i32;N]) -> Self {
//         Self {
//             bar: value,
//         }
//     }
// }

Meta

rustc --version --verbose:

rustc 1.61.0-nightly (c274e4969 2022-03-05)
binary: rustc
commit-hash: c274e4969f058b1c644243181ece9f829efa7594
commit-date: 2022-03-05
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Error output

---- src/lib.rs - Foo<N>::try_from (line 22) stdout ----
error: internal compiler error: compiler/rustc_middle/src/ty/normalize_erasing_regions.rs:179:90: Failed to normalize std::result::Result<const_generics_bug::Foo<N>, <const_generics_bug::Foo<N> as std::convert::TryFrom<[i32; N]>>::Error>, maybe try to call `try_normalize_erasing_regions` instead

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1236:9

Backtrace

   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
   2: <rustc_errors::HandlerInner>::bug
   3: <rustc_errors::Handler>::bug
   4: rustc_middle::ty::context::tls::with_opt::<rustc_middle::util::bug::opt_span_bug_fmt<rustc_span::span_encoding::Span>::{closure#0}, ()>
   5: rustc_middle::util::bug::opt_span_bug_fmt::<rustc_span::span_encoding::Span>
   6: rustc_middle::util::bug::bug_fmt
   7: <rustc_middle::ty::Ty as rustc_middle::ty::fold::TypeFoldable>::try_fold_with::<rustc_middle::ty::normalize_erasing_regions::NormalizeAfterErasingRegionsFolder>
   8: <&rustc_middle::ty::list::List<rustc_middle::ty::Ty> as rustc_middle::ty::fold::TypeFoldable>::try_fold_with::<rustc_middle::ty::normalize_erasing_regions::NormalizeAfterErasingRegionsFolder>
   9: <rustc_middle::ty::instance::Instance>::fn_sig_for_fn_abi
  10: rustc_middle::ty::layout::fn_abi_of_instance
  11: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::plumbing::QueryCtxt, rustc_query_system::query::caches::DefaultCache<rustc_middle::ty::ParamEnvAnd<(rustc_middle::ty::instance::Instance, &rustc_middle::ty::list::List<rustc_middle::ty::Ty>)>, core::result::Result<&rustc_target::abi::call::FnAbi<rustc_middle::ty::Ty>, rustc_middle::ty::layout::FnAbiError>>>
  12: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::fn_abi_of_instance
  13: <rustc_codegen_llvm::context::CodegenCx as rustc_codegen_ssa::traits::declare::PreDefineMethods>::predefine_fn
  14: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  15: <rustc_query_system::dep_graph::graph::DepGraph<rustc_middle::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle::ty::context::TyCtxt, rustc_span::symbol::Symbol, rustc_codegen_ssa::ModuleCodegen<rustc_codegen_llvm::ModuleLlvm>>
  16: rustc_codegen_llvm::base::compile_codegen_unit
  17: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  18: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
  19: <rustc_interface::passes::QueryContext>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_errors::ErrorGuaranteed>>
  20: <rustc_interface::queries::Queries>::ongoing_codegen
  21: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorGuaranteed>>
  22: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
  23: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>
  24: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose 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.61.0-nightly (c274e4969 2022-03-05) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C codegen-units=1 -C embed-bitcode=no

query stack during panic:
#0 [fn_abi_of_instance] computing call ABI of `<const_generics_bug::Foo<4_usize> as core::convert::TryFrom<[i32; 4]>>::try_from`
end of query stack

@ssande7 ssande7 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 Mar 6, 2022
@BoxyUwU
Copy link
Member

BoxyUwU commented Mar 6, 2022

same as #79018 probably

@ssande7
Copy link
Author

ssande7 commented Mar 6, 2022

Sounds like that's probably the case. Feel free to close this if appropriate

@BoxyUwU BoxyUwU closed this as completed Mar 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

No branches or pull requests

2 participants