Skip to content

Using const value from trait to construct array produces error #52462

Closed
@Bert-Proesmans

Description

@Bert-Proesmans
Playground code
use std::u32::MAX;

trait RPCService {
    type Method;
    const MAX_METHODS: usize = 4;
    
    // RPCService::MAX_METHODS -> ERROR
    // Self::MAX_METHODS -> ERROR
    // MAX_METHODS -> ERROR
    fn get_methods() -> [(&'static str, u32); Self::MAX_METHODS];
}

#[repr(u32)]
enum ConnectMethods {
    Bind = 0,
    Connect = 1,
}

struct ConnectService {}
impl ConnectService {
    pub const METHOD_BIND: ConnectMethods = ConnectMethods::Bind;
    pub const METHOD_CONNECT: ConnectMethods = ConnectMethods::Connect;
}

impl RPCService for ConnectService {
    type Method = ConnectMethods;
    
    fn get_methods() -> [(&'static str, u32); Self::MAX_METHODS] {
        [
        ("Bind", ConnectMethods::Bind as u32),
        ("Connect", ConnectMethods::Connect as u32),
        ("", MAX),
        ("", MAX),
        ]
    }
}


fn main() {
    let bind_id = ConnectService::METHOD_BIND;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (file:///playground)
error[E0599]: no associated item named `MAX_METHODS` found for type `Self` in the current scope
  --> src/main.rs:10:47
   |
10 |     fn get_methods() -> [(&'static str, u32); Self::MAX_METHODS];
   |                                               ^^^^^^^^^^^^^^^^^ associated item not found in `Self`
   |
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following trait defines an item `MAX_METHODS`, perhaps you need to implement it:
           candidate #1: `RPCService`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Like it shows above, the compiler is confused by the MAX_METHODS item within the trait that declared it. I suppose the trait impl resolves fine, because no error is produced for that span.
Within my own crate i even get an ICE, but i'm unsure if it's the same error or caused by it. This ICE is what made me build the playground example.

My crate's ICE
error[E0599]: no associated item named `MAX_METHODS` found for type `Self` in the current scope
   --> src\service.rs:141:47
    |
141 |     fn get_methods() -> [(&'static str, u32); Self::MAX_METHODS];
    |                                               ^^^^^^^^^^^^^^^^^ associated item not found in `Self`
    |
    = help: items from traits can only be used if the trait is implemented and in scope
    = note: the following trait defines an item `MAX_METHODS`, perhaps you need to implement it:
            candidate #1: `service::RPCService`

error: internal compiler error: librustc\traits\codegen\mod.rs:63: Encountered ambiguity selecting `Binder(<[type error] as service::RPCService>)` during codegen, presuming due to overflow

thread 'main' panicked at 'Box<Any>', librustc_errors\lib.rs:554:9
stack backtrace:
   0: <std::sys::windows::args::Args as core::ops::drop::Drop>::drop
   1: <std::sync::condvar::Condvar as core::ops::drop::Drop>::drop
   2: std::panicking::take_hook
   3: std::panicking::take_hook
   4: <rustc::ty::SymbolName as core::fmt::Display>::fmt
   5: std::panicking::rust_panic_with_hook
   6: <rustc_errors::emitter::ColorConfig as core::fmt::Debug>::fmt
   7: rustc_errors::Handler::bug
   8: rustc::session::config::OutputTypes::values
   9: rustc::ty::context::tls::track_diagnostic
  10: rustc::ty::context::tls::track_diagnostic
  11: rustc::ty::context::tls::track_diagnostic
  12: rustc::session::bug_fmt
  13: rustc::session::bug_fmt
  14: rustc::ty::context::tls::track_diagnostic
  15: rustc::traits::codegen::codegen_fulfill_obligation
  16: rustc::ty::query::on_disk_cache::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc::ty::query::on_disk_cache::CacheDecoder<'a, 'tcx, 'x>>::read_str
  17: rustc::ty::context::tls::track_diagnostic
  18: rustc::ty::context::tls::track_diagnostic
  19: rustc::dep_graph::graph::DepGraph::assert_ignored
  20: rustc::ty::context::tls::track_diagnostic
  21: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  22: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  23: rustc::ty::instance::Instance::resolve
  24: <rustc_mir::interpret::eval_context::Frame<'mir, 'tcx> as core::cmp::PartialEq>::eq
  25: <rustc_mir::interpret::eval_context::Frame<'mir, 'tcx> as core::cmp::PartialEq>::eq
  26: <&'a rustc_mir::transform::const_prop::ConstPropagator<'a, 'b, 'tcx> as rustc_target::abi::LayoutOf>::layout_of
  27: <rustc_mir::transform::const_prop::ConstPropagator<'b, 'a, 'tcx> as rustc::mir::visit::Visitor<'tcx>>::visit_statement
  28: <rustc_mir::transform::const_prop::ConstProp as rustc_mir::transform::MirPass>::run_pass
  29: <rustc_mir::transform::MirSource as core::fmt::Debug>::fmt
  30: rustc_mir::transform::optimized_mir
  31: rustc::ty::query::on_disk_cache::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc::ty::query::on_disk_cache::CacheDecoder<'a, 'tcx, 'x>>::read_str
  32: rustc::ty::context::tls::track_diagnostic
  33: rustc::ty::context::tls::track_diagnostic
  34: rustc::dep_graph::graph::DepGraph::assert_ignored
  35: rustc::ty::context::tls::track_diagnostic
  36: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  37: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  38: rustc::ty::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::maybe_optimized_mir
  39: <rustc_mir::interpret::eval_context::Frame<'mir, 'tcx> as core::cmp::PartialEq>::eq
  40: rustc_mir::interpret::const_eval::value_to_const_value
  41: rustc_mir::interpret::const_eval::const_eval_provider
  42: rustc::ty::query::on_disk_cache::__ty_decoder_impl::<impl serialize::serialize::Decoder for rustc::ty::query::on_disk_cache::CacheDecoder<'a, 'tcx, 'x>>::read_str
  43: rustc::ty::context::tls::track_diagnostic
  44: rustc::ty::context::tls::track_diagnostic
  45: rustc::dep_graph::graph::DepGraph::assert_ignored
  46: rustc::ty::context::tls::track_diagnostic
  47: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  48: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  49: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_const
  50: rustc::ty::structural_impls::<impl rustc::ty::context::Lift<'tcx> for rustc::ty::sty::FnSig<'a>>::lift_to_tcx
  51: <rustc::traits::project::AssociatedTypeNormalizer<'a, 'b, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  52: <rustc_typeck::outlives::implicit_infer::InferVisitor<'cx, 'tcx> as rustc::hir::itemlikevisit::ItemLikeVisitor<'tcx>>::visit_item
  53: <rustc_typeck::constrained_type_params::Parameter as core::fmt::Debug>::fmt
  54: <rustc_typeck::outlives::implicit_infer::InferVisitor<'cx, 'tcx> as rustc::hir::itemlikevisit::ItemLikeVisitor<'tcx>>::visit_item
  55: <rustc_typeck::check::method::probe::ProbeScope as core::fmt::Debug>::fmt
  56: <rustc_typeck::collect::has_late_bound_regions::LateBoundRegionsDetector<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_lifetime
  57: <rustc_typeck::check::Diverges as core::fmt::Debug>::fmt
  58: <rustc_typeck::check::writeback::Resolver<'cx, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_region
  59: <rustc_typeck::check::writeback::Resolver<'cx, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_region
  60: rustc::ty::context::tls::track_diagnostic
  61: rustc::ty::context::tls::track_diagnostic
  62: rustc::dep_graph::graph::DepGraph::assert_ignored
  63: rustc::ty::context::tls::track_diagnostic
  64: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  65: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  66: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::try_print_query_stack
  67: <rustc_typeck::check::method::probe::ProbeScope as core::fmt::Debug>::fmt
  68: <rustc_typeck::check::autoderef::AutoderefKind as core::fmt::Debug>::fmt
  69: rustc_typeck::check_crate
  70: <humantime::duration::Error as std::error::Error>::cause
  71: <rustc_driver::derive_registrar::Finder as rustc::hir::itemlikevisit::ItemLikeVisitor<'v>>::visit_item
  72: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  73: rustc_driver::driver::compile_input
  74: rustc_driver::run_compiler
  75: rustc_driver::target_features::add_configuration
  76: <humantime::duration::Error as std::error::Error>::cause
  77: _rust_maybe_catch_panic
  78: rustc_driver::profile::dump
  79: rustc_driver::main
  80: <unknown>
  81: std::panicking::update_panic_count
  82: _rust_maybe_catch_panic
  83: std::rt::lang_start_internal
  84: <unknown>
  85: <unknown>
  86: BaseThreadInitThunk
  87: RtlUserThreadStart
query stack during panic:
#0 [codegen_fulfill_obligation] checking if `service::RPCService` fulfills its obligations
#1 [optimized_mir] processing `<services::bnet::connection_service::ConnectionService as service::RPCService>::get_methods::{{constant}}`
#2 [const_eval] const-evaluating `<services::bnet::connection_service::ConnectionService as service::RPCService>::get_methods::{{constant}}`
#3 [check_impl_item_well_formed] processing `<services::bnet::connection_service::ConnectionService as service::RPCService>::get_methods`
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0599`.

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

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.29.0-nightly (1ecf6929d 2018-07-16) running on x86_64-pc-windows-msvc

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

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

error: Could not compile `incite`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions