Skip to content

Strongly-typed str-wrapper const results in ICE in pattern match #87046

@neoeinstein

Description

@neoeinstein

Code

#![feature(const_fn_union)] #[derive(PartialEq, Eq)] #[repr(transparent)] pub struct Username(str); pub const ROOT_USER: &Username = Username::from_str("root"); impl Username { pub const fn from_str(raw: &str) -> &Self { union Transmute<'a> { raw: &'a str, typed: &'a Username, } unsafe { Transmute { raw }.typed } } pub const fn as_str(&self) -> &str { &self.0 } pub fn is_root(&self) -> bool { match self { ROOT_USER => true, _ => false, } } }

Changing Username::is_root and introducing ROOT_USER_STR compiles successfully:

pub const ROOT_USER_STR: &str = ROOT_USER.as_str(); impl Username {pub fn is_root(&self) -> bool { match self.as_str() { ROOT_USER_STR => true, _ => false, } } }

Meta

rustc --version --verbose (Was from the Rust Playground):

1.55.0-nightly (2021-07-08 8b87e85394aa583b01e5) 

Error output

error: internal compiler error: compiler/rustc_mir/src/const_eval/mod.rs:198:18: type Username should not have metadata, but had Meta(0x0000000000000004) 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.55.0-nightly (8b87e8539 2021-07-08) running on x86_64-unknown-linux-gnu note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [deref_const] deref constant #1 [check_match] match-checking `<impl at src/main.rs:9:1: 31:2>::is_root` #2 [analysis] running analysis passes on this crate end of query stack error: aborting due to previous error error: could not compile `playground` 
Backtrace

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1035:9 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::ty::context::tls::with_opt 5: rustc_middle::util::bug::opt_span_bug_fmt 6: rustc_middle::util::bug::bug_fmt 7: rustc_mir::const_eval::deref_const 8: rustc_query_system::query::plumbing::get_query_impl 9: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::deref_const 10: rustc_mir_build::thir::pattern::const_to_pat::ConstToPat::recur 11: rustc_infer::infer::InferCtxtBuilder::enter 12: rustc_mir_build::thir::pattern::const_to_pat::<impl rustc_mir_build::thir::pattern::PatCtxt>::const_to_pat 13: rustc_mir_build::thir::pattern::PatCtxt::lower_path 14: rustc_mir_build::thir::pattern::PatCtxt::lower_pattern 15: rustc_mir_build::thir::pattern::check_match::MatchVisitor::lower_pattern 16: <rustc_mir_build::thir::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr 17: <rustc_mir_build::thir::pattern::check_match::MatchVisitor as rustc_hir::intravisit::Visitor>::visit_expr 18: rustc_mir_build::thir::pattern::check_match::check_match 19: rustc_query_system::query::plumbing::get_query_impl 20: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::check_match 21: rustc_middle::ty::<impl rustc_middle::ty::context::TyCtxt>::par_body_owners 22: rustc_session::utils::<impl rustc_session::session::Session>::time 23: rustc_interface::passes::analysis 24: rustc_query_system::query::plumbing::get_query_impl 25: <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::analysis 26: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter 27: rustc_span::with_source_map 28: rustc_interface::interface::create_compiler_and_run 29: scoped_tls::ScopedKey<T>::set 

Metadata

Metadata

Assignees

No one assigned

    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