Skip to content

exhaustiveness checker mishandles const reference to ADT in match pattern #53708

Closed

Description

Update from @pnkfelix:

Our behavior on this test case (playpen) has changed, but it is still not correct.

#[derive(PartialEq, Eq)]
struct S;

fn main() {
    const C: &S = &S;
    match C {
        C => {}
    }
}

emits error:

error[E0004]: non-exhaustive patterns: `&S` not covered
 --> src/main.rs:6:11
  |
6 |     match C {
  |           ^ pattern `&S` not covered
  |
  = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

If you work-around the exhaustiveness checker bug by e.g. adding an unreachable arm, then the compiler accepts the code (and as far as I can tell, assigns it the correct runtime semantics).

Original bug report follows:


struct S;

fn main() {
    const C: &S = &S;
    match C {
        C => {}
    }
}

gives:
error: internal compiler error: librustc_mir/hair/pattern/_match.rs:432: bad constructor ConstantValue(Const { ty: &S, val: Scalar(Ptr(Pointer { alloc_id: AllocId(1), offset: Size { raw: 0 } })) }) for adt S

The relevant stack is:

  14: rustc_mir::hair::pattern::_match::Constructor::variant_index_for_adt
  15: rustc_mir::hair::pattern::_match::constructor_sub_pattern_tys
  16: rustc_mir::hair::pattern::_match::is_useful_specialized
  17: <core::iter::Map<I, F> as core::iter::iterator::Iterator>::try_fold
  18: rustc_mir::hair::pattern::_match::is_useful
  19: rustc_mir::hair::pattern::_match::is_useful_specialized
  20: <core::iter::Map<I, F> as core::iter::iterator::Iterator>::try_fold
  21: rustc_mir::hair::pattern::_match::is_useful
  22: rustc_mir::hair::pattern::check_match::check_arms
  23: rustc_mir::hair::pattern::_match::MatchCheckCtxt::create_and_enter
  24: <rustc_mir::hair::pattern::check_match::MatchVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  25: <rustc_mir::hair::pattern::check_match::MatchVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  26: <rustc_mir::hair::pattern::check_match::MatchVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_body
  27: rustc::session::Session::track_errors
  28: rustc_mir::hair::pattern::check_match::check_match
  29: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::check_match<'tcx>>::compute
  30: rustc::dep_graph::graph::DepGraph::with_task_impl
  31: <rustc::ty::query::plumbing::JobOwner<'a, 'tcx, Q>>::start
  32: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  33: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  34: rustc::hir::intravisit::Visitor::visit_nested_body
  35: rustc::hir::Crate::visit_all_item_likes
  36: rustc_mir::hair::pattern::check_match::check_crate

It looks like this will take some effort to fix, I patched constructor_sub_pattern_tys to return vec![], but then rustc erroneously reports
error[E0004]: non-exhaustive patterns: '&S' not covered
and with an enum:

enum E {A}

fn main() {
    const C: &E = &E::A;
    match C {
        C => {}
        _ => {}
    }
}

it hits:
error: internal compiler error: librustc\traits\codegen\mod.rs:169: Encountered errors '[FulfillmentError(Obligation(predicate=Binder(TraitPredicate(<E as std::cmp::PartialEq>)),depth=1),Unimplemented)]' resolving bounds after type-checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-const-evalArea: Constant evaluation (MIR interpretation)A-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.P-mediumMedium priorityT-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