Skip to content

Commit

Permalink
Account for for<'a> types when checking for non-structural type in …
Browse files Browse the repository at this point in the history
…constant as pattern

When we encounter a constant in a pattern, we check if it is non-structural. If so, we check if the type implements `PartialEq`, but for types with escaping bound vars the check would be incorrect as is, so we break early. This is ok because these types would be filtered anyways.

Fix rust-lang#134764.
  • Loading branch information
estebank committed Dec 26, 2024
1 parent 409998c commit 0abcca4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
13 changes: 10 additions & 3 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::Obligation;
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::thir::{FieldPat, Pat, PatKind};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypeVisitor, ValTree};
use rustc_middle::ty::{
self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitableExt, TypeVisitor, ValTree,
};
use rustc_middle::{mir, span_bug};
use rustc_span::def_id::DefId;
use rustc_span::{Span, sym};
Expand Down Expand Up @@ -387,7 +389,9 @@ fn extend_type_not_partial_eq<'tcx>(

impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor<'tcx> {
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
if let ty::Adt(def, _args) = ty.kind() {
if let ty::Adt(def, _args) = ty.kind()
&& !ty.has_escaping_bound_vars()
{
let ty_def_id = def.did();
let ty_def_span = self.tcx.def_span(ty_def_id);
let (impls_partial_eq, derived, structural, impl_def_id) =
Expand All @@ -412,7 +416,6 @@ fn extend_type_not_partial_eq<'tcx>(
_ => {}
};
}
use rustc_middle::ty::TypeSuperVisitable;
ty.super_visit_with(self)
}
}
Expand Down Expand Up @@ -468,6 +471,10 @@ fn type_has_partial_eq_impl<'tcx>(
let partial_eq_trait_id = tcx.require_lang_item(hir::LangItem::PartialEq, None);
let structural_partial_eq_trait_id = tcx.require_lang_item(hir::LangItem::StructuralPeq, None);

if ty.has_escaping_bound_vars() {
return (false, false, false, None);
}

let partial_eq_obligation = Obligation::new(
tcx,
ObligationCause::dummy(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(structural_match)]
impl<T: ?Sized> std::marker::StructuralPartialEq for O<T> { }

enum O<T: ?Sized> {
Some(*const T),
None,
}

const C: O<dyn for<'a> Fn(Box<dyn Fn(&'a u8)>)> = O::None;

fn main() {
match O::None {
C => (), //~ ERROR constant of non-structural type
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: constant of non-structural type `O<dyn for<'a> Fn(Box<dyn Fn(&'a u8)>)>` in a pattern
--> $DIR/non_structural_with_escaping_bounds.rs:13:9
|
LL | const C: O<dyn for<'a> Fn(Box<dyn Fn(&'a u8)>)> = O::None;
| ----------------------------------------------- constant defined here
...
LL | C => (),
| ^ constant of non-structural type
|
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
= note: `std::alloc::Global` must be annotated with `#[derive(PartialEq)]` to be usable in patterns
= note: `std::alloc::Global` must be annotated with `#[derive(PartialEq)]` to be usable in patterns

error: aborting due to 1 previous error

0 comments on commit 0abcca4

Please sign in to comment.