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

Bare Trait Object Return in Trait Method Causes Panic #138277

Open
leonardogiovannoni opened this issue Mar 9, 2025 · 4 comments
Open

Bare Trait Object Return in Trait Method Causes Panic #138277

leonardogiovannoni opened this issue Mar 9, 2025 · 4 comments
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardogiovannoni
Copy link

This code panics in nightly 1.87, but not in stable 1.85 and beta 1.86.

Code

use std::ops::{Deref, DerefMut};

pub trait Token: Sized {
    type Context: Context<Token = Self>;

    fn load<'ctx>(self, ctx: &'ctx Self::Context) -> <Self::Context as Context>::Payload<'ctx> {
        ctx.packet(self)
    }

}

pub trait Context: Sized + Clone + Send {
    type Token: Token<Context = Self>;
    type Payload<'ctx>: Payload<'ctx, Context = Self, Token = Self::Token>
    where
        Self: 'ctx;

    fn packet<'ctx>(&'ctx self, token: Self::Token) -> Self::Payload<'ctx>;
   
}

pub trait Payload<'a>: AsRef<[u8]> + AsMut<[u8]> + Deref<Target = [u8]> + DerefMut<Target = [u8]> {
    type Context: Context;
    type Token: Token<Context = Self::Context>;
}

pub trait Foo: Send + Sized {
    type Ctx: Context;
    type Tk: Token<Context = Self::Ctx>;


    fn recv_local(&mut self) -> anyhow::Result<Payload<'_>> {
        let token = self.recv()?;
        Ok(token.load(self.context()))
    }
    /// Receives a packet and returns a token.
    fn recv(&mut self) -> anyhow::Result<Self::Tk>;

    fn context(&self) -> &Self::Ctx;
}


fn main() {
    println!("Hello, world!");
}

In 1.85 and 1.86 I receive instead the following error:

error[E0782]: expected a type, found a trait
  --> src/main.rs:33:48
   |
33 |     fn recv_local(&mut self) -> anyhow::Result<Payload<'_>> {
   |                                                ^^^^^^^^^^^
   |
help: you can add the `dyn` keyword if you want a trait object
   |
33 |     fn recv_local(&mut self) -> anyhow::Result<dyn Payload<'_>> {
   |                                                +++

For more information about this error, try `rustc --explain E0782`.
error: could not compile `ice` (bin "ice") due to 1 previous error

Meta

rustc --version --verbose:

rustc 1.87.0-nightly (efea9896f 2025-03-08)
binary: rustc
commit-hash: efea9896f506baa08f40444e07774e827646d57a
commit-date: 2025-03-08
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Error output

thread 'rustc' panicked at compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs:210:61:
$ident: found TraitItem(TraitItem { ident: recv_local#0, owner_id: DefId(0:23 ~ ice[c768]::Foo::recv_local), generics: Generics { params: [GenericP
stack backtrace:
   0:     0x7f379c3b9884 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h92d4bfce6f763b00
   1:     0x7f379cc05bae - core::fmt::write::hb4817f6dd96fad0f
   2:     0x7f379dd8c051 - std::io::Write::write_fmt::h314f413d021b4307
   3:     0x7f379c3b96e2 - std::sys::backtrace::BacktraceLock::print::h68d0c48567bf96cd
   4:     0x7f379c3bc008 - std::panicking::default_hook::{{closure}}::h15992f1912031b9e
   5:     0x7f379c3bbb27 - std::panicking::default_hook::hf25af0ac62c164b1
   6:     0x7f379b50bbbf - std[aea8b8668118ef2c]::panicking::update_hook::<alloc[a7bb905a442d6351]::boxed::Box<rustc_driver_impl[329d2250e30901ce]:
   7:     0x7f379c3bc873 - std::panicking::rust_panic_with_hook::he1f54632065b4203
   8:     0x7f379c3bc56a - std::panicking::begin_panic_handler::{{closure}}::h2b38fb544238cd15
   9:     0x7f379c3b9d59 - std::sys::backtrace::__rust_end_short_backtrace::he36cb2712646bcd0
  10:     0x7f379c3bc22d - rust_begin_unwind
  11:     0x7f379905bcc0 - core::panicking::panic_fmt::h546b0be70e72d2d1
  12:     0x7f379b5d9111 - rustc_hir[6b05b39687205c32]::hir::expect_failed::<&rustc_hir[6b05b39687205c32]::hir::Node>
  13:     0x7f379de8782e - <dyn rustc_hir_analysis[7d047a4039fbdcf6]::hir_ty_lowering::HirTyLowerer>::prohibit_or_lint_bare_trait_object_ty
  14:     0x7f379d498bb6 - <dyn rustc_hir_analysis[7d047a4039fbdcf6]::hir_ty_lowering::HirTyLowerer>::lower_ty
  15:     0x7f379d49bc84 - <dyn rustc_hir_analysis[7d047a4039fbdcf6]::hir_ty_lowering::HirTyLowerer>::lower_path
  16:     0x7f379d49825a - <dyn rustc_hir_analysis[7d047a4039fbdcf6]::hir_ty_lowering::HirTyLowerer>::lower_ty
  17:     0x7f379d48e63b - <dyn rustc_hir_analysis[7d047a4039fbdcf6]::hir_ty_lowering::HirTyLowerer>::lower_fn_ty
  18:     0x7f379d48ded1 - rustc_hir_analysis[7d047a4039fbdcf6]::collect::lower_fn_sig_recovering_infer_ret_ty
  19:     0x7f379d48be63 - rustc_hir_analysis[7d047a4039fbdcf6]::collect::fn_sig
  20:     0x7f379ce7cced - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_i
  21:     0x7f379ce7ccad - <rustc_query_impl[35192ced76ef19cc]::query_impl::fn_sig::dynamic_query::{closure#2} as core[e60e1eed10c732b4]::ops::func
  22:     0x7f379ceec5be - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicCo
  23:     0x7f379ceeaa4d - rustc_query_impl[35192ced76ef19cc]::query_impl::fn_sig::get_query_incr::__rust_end_short_backtrace
  24:     0x7f379d425ba5 - rustc_middle[abc93db11e30059e]::query::plumbing::query_get_at::<rustc_query_system[8750ea9310c2c1bc]::query::caches::Def
  25:     0x7f379d4452b0 - rustc_hir_analysis[7d047a4039fbdcf6]::check::check::check_item_type
  26:     0x7f379d43c172 - rustc_hir_analysis[7d047a4039fbdcf6]::check::wfcheck::check_well_formed
  27:     0x7f379d43aaf7 - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_i
  28:     0x7f379cfecf60 - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicCo
  29:     0x7f379cff65bd - rustc_query_impl[35192ced76ef19cc]::query_impl::check_well_formed::get_query_incr::__rust_end_short_backtrace
  30:     0x7f379d43789d - rustc_hir_analysis[7d047a4039fbdcf6]::check::wfcheck::check_mod_type_wf
  31:     0x7f379d43769d - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_i
  32:     0x7f379dc9b7fd - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicCo
  33:     0x7f379dc9c24a - rustc_query_impl[35192ced76ef19cc]::query_impl::check_mod_type_wf::get_query_incr::__rust_end_short_backtrace
  34:     0x7f379cf2fad3 - rustc_hir_analysis[7d047a4039fbdcf6]::check_crate
  35:     0x7f379cfef2b4 - rustc_interface[4c1044b8b3f8fa90]::passes::run_required_analyses
  36:     0x7f379db6c538 - rustc_interface[4c1044b8b3f8fa90]::passes::analysis
  37:     0x7f379db6c517 - rustc_query_impl[35192ced76ef19cc]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[35192ced76ef19cc]::query_i
  38:     0x7f379dee7c69 - rustc_query_system[8750ea9310c2c1bc]::query::plumbing::try_execute_query::<rustc_query_impl[35192ced76ef19cc]::DynamicCo
  39:     0x7f379dee7775 - rustc_query_impl[35192ced76ef19cc]::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
  40:     0x7f379dd3247c - rustc_interface[4c1044b8b3f8fa90]::passes::create_and_enter_global_ctxt::<core[e60e1eed10c732b4]::option::Option<rustc_i
  41:     0x7f379dd2246e - rustc_interface[4c1044b8b3f8fa90]::interface::run_compiler::<(), rustc_driver_impl[329d2250e30901ce]::run_compiler::{clo
  42:     0x7f379dbae7c8 - std[aea8b8668118ef2c]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[4c1044b8b3f8fa90]::util::run_in_th
  43:     0x7f379dbaf0b4 - <<std[aea8b8668118ef2c]::thread::Builder>::spawn_unchecked_<rustc_interface[4c1044b8b3f8fa90]::util::run_in_thread_with_globals<rustc_interface[4c1044b8b3f8fa90]::util::run_in_thread_pool_with_globals<rustc_interface[4c1044b8b3f8fa90]::interface::run_compiler<(), rustc_driver_impl[329d2250e30901ce]::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core[e60e1eed10c732b4]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  44:     0x7f379dbb04ab - std::sys::pal::unix::thread::Thread::new::thread_start::hbcfe3ea4381c14e0
  45:     0x7f3797c7e168 - start_thread
                               at /usr/src/debug/glibc-2.40-21.fc41.x86_64/nptl/pthread_create.c:448:8
  46:     0x7f3797d0214c - __GI___clone3
                               at /usr/src/debug/glibc-2.40-21.fc41.x86_64/misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0
  47:                0x0 - <unknown>

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 `/home/leonardogiovannoni/Src/Net/ice/rustc-ice-2025-03-09T15_06_36-37051.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [fn_sig] computing function signature of `Foo::recv_local`
#1 [check_well_formed] checking that `Foo` is well-formed
... and 2 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
error: could not compile `ice` (bin "ice")

Caused by:
  process didn't exit successfully: `/home/leonardogiovannoni/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name ice --edition=2024 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=83 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=3de3f006944ba14f -C extra-filename=-ec99d68baccf9512 --out-dir /home/leonardogiovannoni/Src/Net/ice/target/debug/deps -C incremental=/home/leonardogiovannoni/Src/Net/ice/target/debug/incremental -L dependency=/home/leonardogiovannoni/Src/Net/ice/target/debug/deps --extern anyhow=/home/leonardogiovannoni/Src/Net/ice/target/debug/deps/libanyhow-eee0032c520aff90.rlib` (exit status: 101)
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 `/home/leonardogiovannoni/Src/Net/ice/rustc-ice-2025-03-09T15_06_36-37051.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [fn_sig] computing function signature of `Foo::recv_local`
#1 [check_well_formed] checking that `Foo` is well-formed
... and 2 other queries... use `env RUST_BACKTRACE=1` to see the full query stack
error: could not compile `ice` (bin "ice")

Caused by:
  process didn't exit successfully: `/home/leonardogiovannoni/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name ice --edit
leonardogiovannoni@fedora:~/Src/Net/ice$ RUST_BACKTRACE=1 cargo build
   Compiling ice v0.1.0 (/home/leonardogiovannoni/Src/Net/ice)

thread 'rustc' panicked at compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs:210:61:
$ident: found TraitItem(TraitItem { ident: recv_local#0, owner_id: DefId(0:23 ~ ice[c768]::Foo::recv_local), generics: Generics { params: [GenericP
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: rustc_hir::hir::expect_failed::<&rustc_hir::hir::Node>
   3: <dyn rustc_hir_analysis::hir_ty_lowering::HirTyLowerer>::prohibit_or_lint_bare_trait_object_ty
   4: <dyn rustc_hir_analysis::hir_ty_lowering::HirTyLowerer>::lower_ty
   5: <dyn rustc_hir_analysis::hir_ty_lowering::HirTyLowerer>::lower_path
   6: <dyn rustc_hir_analysis::hir_ty_lowering::HirTyLowerer>::lower_ty
   7: <dyn rustc_hir_analysis::hir_ty_lowering::HirTyLowerer>::lower_fn_ty
   8: rustc_hir_analysis::collect::lower_fn_sig_recovering_infer_ret_ty
   9: rustc_hir_analysis::collect::fn_sig
      [... omitted 2 frames ...]
  10: rustc_middle::query::plumbing::query_get_at::<rustc_query_system::query::caches::DefIdCache<rustc_middle::query::erase::Erased<[u8; 24]>>>
  11: rustc_hir_analysis::check::check::check_item_type
  12: rustc_hir_analysis::check::wfcheck::check_well_formed
      [... omitted 1 frame ...]
  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::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  18: 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.

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 `/home/leonardogiovannoni/Src/Net/ice/rustc-ice-2025-03-09T15_07_03-37119.txt` to your bug report

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

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

query stack during panic:
#0 [fn_sig] computing function signature of `Foo::recv_local`
#1 [check_well_formed] checking that `Foo` is well-formed
#2 [check_mod_type_wf] checking that types are well-formed in top-level module
#3 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `ice` (bin "ice")

Caused by:
  process didn't exit successfully: `/home/leonardogiovannoni/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name ice --edition=2024 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=147 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=3de3f006944ba14f -C extra-filename=-ec99d68baccf9512 --out-dir /home/leonardogiovannoni/Src/Net/ice/target/debug/deps -C incremental=/home/leonardogiovannoni/Src/Net/ice/target/debug/incremental -L dependency=/home/leonardogiovannoni/Src/Net/ice/target/debug/deps --extern anyhow=/home/leonardogiovannoni/Src/Net/ice/target/debug/deps/libanyhow-eee0032c520aff90.rlib` (exit status: 101)

@leonardogiovannoni leonardogiovannoni 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 9, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 9, 2025
@cyrgani
Copy link
Contributor

cyrgani commented Mar 9, 2025

looks like a duplicate of #138211,
reduces to

pub trait Payload {}

pub trait Foo {
    fn recv_local() -> Option<Payload> {
        todo!()
    }
}

fn main() {}

@compiler-errors
Copy link
Member

This should be fixed by #138238.

@harshvyas54
Copy link

fn recv_local(&mut self) -> anyhow::Result<Box<dyn Payload<'_>>> {

did you tried above?

@harshvyas54
Copy link

if it is type issues then we can try below also
fn recv_local(&mut self) -> anyhow::Result<<Self::Ctx as Context>::Payload<'_>> {
let token = self.recv()?;
Ok(token.load(self.context()))
}

may be you are directly using trait return please try to change it with above two return type.

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) ❄️ needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. 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

5 participants