Get rid of redundant NonStructuralMatchTyKind
This commit is contained in:
parent
1152e70363
commit
c1f54c30bb
4 changed files with 28 additions and 73 deletions
|
@ -122,37 +122,31 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
|
||||||
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
|
fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option<String> {
|
||||||
traits::search_for_structural_match_violation(self.span, self.tcx(), ty, false).map(
|
traits::search_for_structural_match_violation(self.span, self.tcx(), ty, false).map(
|
||||||
|non_sm_ty| {
|
|non_sm_ty| {
|
||||||
with_no_trimmed_paths!(match non_sm_ty.kind {
|
with_no_trimmed_paths!(match non_sm_ty.kind() {
|
||||||
traits::NonStructuralMatchTyKind::Adt(adt) => self.adt_derive_msg(adt),
|
ty::Adt(adt, _) => self.adt_derive_msg(*adt),
|
||||||
traits::NonStructuralMatchTyKind::Dynamic => {
|
ty::Dynamic(..) => {
|
||||||
"trait objects cannot be used in patterns".to_string()
|
"trait objects cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::Opaque => {
|
ty::Opaque(..) => {
|
||||||
"opaque types cannot be used in patterns".to_string()
|
"opaque types cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::Closure => {
|
ty::Closure(..) => {
|
||||||
"closures cannot be used in patterns".to_string()
|
"closures cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::Generator => {
|
ty::Generator(..) | ty::GeneratorWitness(..) => {
|
||||||
"generators cannot be used in patterns".to_string()
|
"generators cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::Float => {
|
ty::Float(..) => {
|
||||||
"floating-point numbers cannot be used in patterns".to_string()
|
"floating-point numbers cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::FnPtr => {
|
ty::FnPtr(..) => {
|
||||||
"function pointers cannot be used in patterns".to_string()
|
"function pointers cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::RawPtr => {
|
ty::RawPtr(..) => {
|
||||||
"raw pointers cannot be used in patterns".to_string()
|
"raw pointers cannot be used in patterns".to_string()
|
||||||
}
|
}
|
||||||
traits::NonStructuralMatchTyKind::Param => {
|
_ => {
|
||||||
bug!("use of a constant whose type is a parameter inside a pattern")
|
bug!("use of a value of `{non_sm_ty}` inside a pattern")
|
||||||
}
|
|
||||||
traits::NonStructuralMatchTyKind::Projection => {
|
|
||||||
bug!("use of a constant whose type is a projection inside a pattern")
|
|
||||||
}
|
|
||||||
traits::NonStructuralMatchTyKind::Foreign => {
|
|
||||||
bug!("use of a value of a foreign type inside a pattern")
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
|
@ -61,7 +61,6 @@ pub use self::specialize::specialization_graph::FutureCompatOverlapError;
|
||||||
pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
|
pub use self::specialize::specialization_graph::FutureCompatOverlapErrorKind;
|
||||||
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
|
pub use self::specialize::{specialization_graph, translate_substs, OverlapError};
|
||||||
pub use self::structural_match::search_for_structural_match_violation;
|
pub use self::structural_match::search_for_structural_match_violation;
|
||||||
pub use self::structural_match::{NonStructuralMatchTy, NonStructuralMatchTyKind};
|
|
||||||
pub use self::util::{
|
pub use self::util::{
|
||||||
elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
|
elaborate_obligations, elaborate_predicates, elaborate_predicates_with_span,
|
||||||
elaborate_trait_ref, elaborate_trait_refs,
|
elaborate_trait_ref, elaborate_trait_refs,
|
||||||
|
|
|
@ -6,31 +6,10 @@ use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::lang_items::LangItem;
|
use rustc_hir::lang_items::LangItem;
|
||||||
use rustc_middle::ty::query::Providers;
|
use rustc_middle::ty::query::Providers;
|
||||||
use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
use rustc_middle::ty::{self, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
use std::ops::ControlFlow;
|
use std::ops::ControlFlow;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct NonStructuralMatchTy<'tcx> {
|
|
||||||
pub ty: Ty<'tcx>,
|
|
||||||
pub kind: NonStructuralMatchTyKind<'tcx>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum NonStructuralMatchTyKind<'tcx> {
|
|
||||||
Adt(AdtDef<'tcx>),
|
|
||||||
Param,
|
|
||||||
Dynamic,
|
|
||||||
Foreign,
|
|
||||||
Opaque,
|
|
||||||
Closure,
|
|
||||||
Generator,
|
|
||||||
Projection,
|
|
||||||
Float,
|
|
||||||
FnPtr,
|
|
||||||
RawPtr,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This method traverses the structure of `ty`, trying to find an
|
/// This method traverses the structure of `ty`, trying to find an
|
||||||
/// instance of an ADT (i.e. struct or enum) that doesn't implement
|
/// instance of an ADT (i.e. struct or enum) that doesn't implement
|
||||||
/// the structural-match traits, or a generic type parameter
|
/// the structural-match traits, or a generic type parameter
|
||||||
|
@ -64,7 +43,7 @@ pub fn search_for_structural_match_violation<'tcx>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
ty: Ty<'tcx>,
|
ty: Ty<'tcx>,
|
||||||
valtree_semantics: bool,
|
valtree_semantics: bool,
|
||||||
) -> Option<NonStructuralMatchTy<'tcx>> {
|
) -> Option<Ty<'tcx>> {
|
||||||
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), valtree_semantics })
|
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default(), valtree_semantics })
|
||||||
.break_value()
|
.break_value()
|
||||||
}
|
}
|
||||||
|
@ -140,7 +119,7 @@ impl<'tcx> Search<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
type BreakTy = NonStructuralMatchTy<'tcx>;
|
type BreakTy = Ty<'tcx>;
|
||||||
|
|
||||||
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||||
debug!("Search visiting ty: {:?}", ty);
|
debug!("Search visiting ty: {:?}", ty);
|
||||||
|
@ -148,32 +127,25 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
let (adt_def, substs) = match *ty.kind() {
|
let (adt_def, substs) = match *ty.kind() {
|
||||||
ty::Adt(adt_def, substs) => (adt_def, substs),
|
ty::Adt(adt_def, substs) => (adt_def, substs),
|
||||||
ty::Param(_) => {
|
ty::Param(_) => {
|
||||||
let kind = NonStructuralMatchTyKind::Param;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::Dynamic(..) => {
|
ty::Dynamic(..) => {
|
||||||
let kind = NonStructuralMatchTyKind::Dynamic;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::Foreign(_) => {
|
ty::Foreign(_) => {
|
||||||
let kind = NonStructuralMatchTyKind::Foreign;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::Opaque(..) => {
|
ty::Opaque(..) => {
|
||||||
let kind = NonStructuralMatchTyKind::Opaque;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::Projection(..) => {
|
ty::Projection(..) => {
|
||||||
let kind = NonStructuralMatchTyKind::Projection;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::Closure(..) => {
|
ty::Closure(..) => {
|
||||||
let kind = NonStructuralMatchTyKind::Closure;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::Generator(..) | ty::GeneratorWitness(..) => {
|
ty::Generator(..) | ty::GeneratorWitness(..) => {
|
||||||
let kind = NonStructuralMatchTyKind::Generator;
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
ty::FnDef(..) => {
|
ty::FnDef(..) => {
|
||||||
// Types of formals and return in `fn(_) -> _` are also irrelevant;
|
// Types of formals and return in `fn(_) -> _` are also irrelevant;
|
||||||
|
@ -198,10 +170,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
if !self.valtree_semantics {
|
if !self.valtree_semantics {
|
||||||
return ControlFlow::CONTINUE;
|
return ControlFlow::CONTINUE;
|
||||||
} else {
|
} else {
|
||||||
return ControlFlow::Break(NonStructuralMatchTy {
|
return ControlFlow::Break(ty);
|
||||||
ty,
|
|
||||||
kind: NonStructuralMatchTyKind::FnPtr,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,10 +192,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
// pointer. Therefore, one can still use `C` in a pattern.
|
// pointer. Therefore, one can still use `C` in a pattern.
|
||||||
return ControlFlow::CONTINUE;
|
return ControlFlow::CONTINUE;
|
||||||
} else {
|
} else {
|
||||||
return ControlFlow::Break(NonStructuralMatchTy {
|
return ControlFlow::Break(ty);
|
||||||
ty,
|
|
||||||
kind: NonStructuralMatchTyKind::FnPtr,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,10 +200,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
if !self.valtree_semantics {
|
if !self.valtree_semantics {
|
||||||
return ControlFlow::CONTINUE;
|
return ControlFlow::CONTINUE;
|
||||||
} else {
|
} else {
|
||||||
return ControlFlow::Break(NonStructuralMatchTy {
|
return ControlFlow::Break(ty);
|
||||||
ty,
|
|
||||||
kind: NonStructuralMatchTyKind::Float,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,8 +226,7 @@ impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
|
|
||||||
if !self.type_marked_structural(ty) {
|
if !self.type_marked_structural(ty) {
|
||||||
debug!("Search found ty: {:?}", ty);
|
debug!("Search found ty: {:?}", ty);
|
||||||
let kind = NonStructuralMatchTyKind::Adt(adt_def);
|
return ControlFlow::Break(ty);
|
||||||
return ControlFlow::Break(NonStructuralMatchTy { ty, kind });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// structural-match does not care about the
|
// structural-match does not care about the
|
||||||
|
|
|
@ -854,7 +854,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
|
||||||
// We use the same error code in both branches, because this is really the same
|
// We use the same error code in both branches, because this is really the same
|
||||||
// issue: we just special-case the message for type parameters to make it
|
// issue: we just special-case the message for type parameters to make it
|
||||||
// clearer.
|
// clearer.
|
||||||
match non_structural_match_ty.ty.kind() {
|
match non_structural_match_ty.kind() {
|
||||||
ty::Param(_) => {
|
ty::Param(_) => {
|
||||||
// Const parameters may not have type parameters as their types,
|
// Const parameters may not have type parameters as their types,
|
||||||
// because we cannot be sure that the type parameter derives `PartialEq`
|
// because we cannot be sure that the type parameter derives `PartialEq`
|
||||||
|
@ -911,10 +911,10 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
|
||||||
E0741,
|
E0741,
|
||||||
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
|
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
|
||||||
the type of a const parameter",
|
the type of a const parameter",
|
||||||
non_structural_match_ty.ty,
|
non_structural_match_ty,
|
||||||
);
|
);
|
||||||
|
|
||||||
if ty == non_structural_match_ty.ty {
|
if ty == non_structural_match_ty {
|
||||||
diag.span_label(
|
diag.span_label(
|
||||||
hir_ty.span,
|
hir_ty.span,
|
||||||
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
|
format!("`{ty}` doesn't derive both `PartialEq` and `Eq`"),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue