Skip to content

ICE: called Option::unwrap() on a None value in compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs #125757

Closed
@cushionbadak

Description

@cushionbadak

Code

(hand-reduced)

  • #[derive(Default)] added at struct MyStruct, to reduce unrelated error message
#![feature(specialization)]
#![allow(incomplete_features)]

#[derive(Default)]
struct MyStruct {}

trait MyTrait {
    type MyType: Default;
}

impl MyTrait for i32 {
    default type MyType = MyStruct;
}

struct Wrapper2<'a, T, const C: <i32 as MyTrait>::MyType> {
    x: &'a T,
}

impl<'a, const C: usize> Wrapper2<'a, i8, C> {}

fn main() {}
(original)

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

struct MyStruct {}

trait MyTrait {
    type MyType: Default;
}

impl MyTrait for i32 {
    default type MyType = MyStruct;
    //~^ ERROR: the trait bound `MyStruct: Default` is not satisfied
}

fn main() {
    let _x: <i32 as MyTrait>::MyType = <i32 as MyTrait>::MyType::default();
}


//@ run-rustfix
fn foo<T: Default>(list: &mut Vec<T>) {
    let mut cloned_items = Vec::new();
    for v in list.iter() {
        cloned_items.push(v.clone())
    }
    list.push(T::default());
    //~^ ERROR cannot borrow `*list` as mutable because it is also borrowed as immutable
    drop(cloned_items);
}
fn bar<T: std::fmt::Display>(x: T) {
    let a = &x;
    let b = a.clone();
    drop(x);
    //~^ ERROR cannot move out of `x` because it is borrowed
    println!("{b}");
}
#[derive(Debug)]
struct A;
fn qux(x: A) {
    let a = &x;
    let b = a.clone();
    drop(x);
    //~^ ERROR cannot move out of `x` because it is borrowed
    println!("{b:?}");
}
fn main() {
    foo(&mut vec![1, 2, 3]);
    bar("");
    qux(A);
}


// Test for issue 81576
// Remove generic arguments if no method is found for all possible generic argument

use std::marker::PhantomData;

struct Wrapper2<'a, T, const C: <i32 as MyTrait>::MyType> {
    x: &'a T,
}

impl<'a, const C: usize> Wrapper2<'a, i8, C> {
    fn method(&self) {}
}

impl<'a, const C: usize> Wrapper2<'a, i16, C> {
    fn method(&self) {}
}

impl<'a, const C: usize> Wrapper2<'a, i32, C> {
    fn method(&self) {}
}
struct Wrapper<T>(T);

impl Wrapper<i8> {
    fn method(&self) {}
}

impl Wrapper<i16> {
    fn method(&self) {}
}

impl Wrapper<i32> {
    fn method(&self) {}
}

impl Wrapper<i64> {
    fn method(&self) {}
}

impl Wrapper<u8> {
    fn method(&self) {}
}

impl Wrapper<u16> {
    fn method(&self) {}
}

struct Point<T> {
    x: T,
    y: T,
}

impl Point<std::fmt::Display> {
    fn distance(&self) -> f64 {
        self.x.hypot(self.y)
    }
}

struct Other;

impl Other {
    fn other(&self) {}
}

struct Struct<T> {
    _phatom: PhantomData<T>,
}

impl<T> Default for Struct<T> {
    fn default() -> Self {
        Self { _phatom: PhantomData }
    }
}

impl<T: Clone + Copy + PartialEq + Eq + PartialOrd + Ord> Struct<T> {
    fn method(&self) {}
}

fn main() {
    let point_f64 = Point { x: 1_f64, y: 1_f64 };
    let d = point_f64.distance();
    let point_i32 = Point { x: 1_i32, y: 1_i32 };
    let d = point_i32.distance();
    //~^ ERROR no method named `distance` found for struct `Point<i32>
    let d = point_i32.other();
    //~^ ERROR no method named `other` found for struct `Point
    let v = vec![1, 2, 3];
    v.iter().map(Box::new(|x| x * x) as Box<dyn Fn(&i32) -> i32>).extend(std::iter::once(100));
    //~^ ERROR no method named `extend` found for struct `Map
    let wrapper = Wrapper(true);
    wrapper.method();
    //~^ ERROR no method named `method` found for struct `Wrapper<bool>
    wrapper.other();
    //~^ ERROR no method named `other` found for struct `Wrapper
    let boolean = true;
    let wrapper = Wrapper2::<'_, _, 3> { x: &boolean };
    wrapper.method();
    //~^ ERROR no method named `method` found for struct `Wrapper2<'_, bool, 3>
    wrapper.other();
    //~^ ERROR no method named `other` found for struct `Wrapper2
    let a = vec![1, 2, 3];
    a.not_found();
    //~^ ERROR no method named `not_found` found for struct `Vec
    let s = Struct::<f64>::default();
    s.method();
    //~^ ERROR the method `method` exists for struct `Struct<f64>`, but its trait bounds were not satisfied
}

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (debd22da6 2024-05-29)
binary: rustc
commit-hash: debd22da66cfa97c74040ebf68e420672ac8560e
commit-date: 2024-05-29
host: x86_64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.6

Error output

Command: rustc

error: `<i32 as MyTrait>::MyType` is forbidden as the type of a const generic parameter
  --> r_note_and_explain_655790.rs:15:33
   |
15 | struct Wrapper2<'a, T, const C: <i32 as MyTrait>::MyType> {
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the only supported types are integers, `bool` and `char`
Backtrace

thread 'rustc' panicked at compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs:666:37:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::unwrap_failed
   4: <rustc_infer::infer::error_reporting::TypeErrCtxt>::expected_projection
   5: <rustc_infer::infer::error_reporting::TypeErrCtxt>::note_and_explain_type_err
   6: <rustc_infer::infer::error_reporting::TypeErrCtxt>::note_type_err
   7: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::type_err_ctxt_ext::TypeErrCtxtExt>::report_selection_error
   8: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::type_err_ctxt_ext::InferCtxtPrivExt>::report_fulfillment_error
   9: <rustc_infer::infer::error_reporting::TypeErrCtxt as rustc_trait_selection::traits::error_reporting::type_err_ctxt_ext::TypeErrCtxtExt>::report_fulfillment_errors
  10: <rustc_trait_selection::traits::engine::ObligationCtxt>::assumed_wf_types_and_report_errors
  11: rustc_hir_analysis::check::wfcheck::check_well_formed
      [... omitted 1 frame ...]
  12: rustc_middle::query::plumbing::query_ensure_error_guaranteed::<rustc_query_system::query::caches::VecCache<rustc_hir::hir_id::OwnerId, rustc_middle::query::erase::Erased<[u8; 1]>>, ()>
  13: rustc_hir_analysis::check::wfcheck::check_mod_type_wf
      [... omitted 1 frame ...]
  14: rustc_hir_analysis::check_crate
  15: rustc_interface::passes::run_required_analyses
  16: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  17: <rustc_interface::queries::QueryResult<&rustc_middle::ty::context::GlobalCtxt>>::enter::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  18: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: 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: please make sure that you have updated to the latest nightly

note: please attach the file at `/Volumes/T7/workspace/240529_100chaos_tree_combine_typ/icefiles/rustc-ice-2024-05-30T06_24_54-87773.txt` to your bug report

query stack during panic:
#0 [check_well_formed] checking that `<impl at r_note_and_explain_655790.rs:19:1: 19:45>` is well-formed
#1 [check_mod_type_wf] checking that types are well-formed in top-level module
#2 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

Note

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.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