bool to custom enum

This commit is contained in:
lcnr 2022-04-04 10:56:59 +02:00
parent ced65022da
commit 4a82bc9ea0
3 changed files with 18 additions and 11 deletions

View File

@ -30,6 +30,13 @@ pub struct Discr<'tcx> {
pub ty: Ty<'tcx>,
}
/// Used as an input to [`TyCtxt::uses_unique_generic_params`].
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum IgnoreRegions {
Yes,
No,
}
#[derive(Copy, Clone, Debug)]
pub enum NotUniqueParam<'tcx> {
DuplicateParam(ty::GenericArg<'tcx>),
@ -461,20 +468,18 @@ pub fn destructor_constraints(self, def: ty::AdtDef<'tcx>) -> Vec<ty::subst::Gen
pub fn uses_unique_generic_params(
self,
substs: SubstsRef<'tcx>,
ignore_regions: bool,
ignore_regions: IgnoreRegions,
) -> Result<(), NotUniqueParam<'tcx>> {
let mut seen = GrowableBitSet::default();
for arg in substs {
match arg.unpack() {
GenericArgKind::Lifetime(lt) => {
if !ignore_regions {
match lt.kind() {
ty::ReEarlyBound(p) => {
if !seen.insert(p.index) {
return Err(NotUniqueParam::DuplicateParam(lt.into()));
}
}
_ => return Err(NotUniqueParam::NotParam(lt.into())),
if ignore_regions == IgnoreRegions::No {
let ty::ReEarlyBound(p) = lt.kind() else {
return Err(NotUniqueParam::NotParam(lt.into()))
};
if !seen.insert(p.index) {
return Err(NotUniqueParam::DuplicateParam(lt.into()));
}
}
}

View File

@ -5,6 +5,7 @@
use rustc_middle::ty::error::TypeError;
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::util::IgnoreRegions;
use rustc_middle::ty::{self, Predicate, Ty, TyCtxt};
use rustc_span::Span;
use rustc_trait_selection::traits::query::dropck_outlives::AtExt;
@ -66,7 +67,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
self_type_did: DefId,
drop_impl_substs: SubstsRef<'tcx>,
) -> Result<(), ErrorGuaranteed> {
let Err(arg) = tcx.uses_unique_generic_params(drop_impl_substs, false) else {
let Err(arg) = tcx.uses_unique_generic_params(drop_impl_substs, IgnoreRegions::No) else {
return Ok(())
};

View File

@ -8,6 +8,7 @@
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::util::IgnoreRegions;
use rustc_middle::ty::{self, ImplPolarity, Ty, TyCtxt, TypeFoldable, TypeVisitor};
use rustc_session::lint;
use rustc_span::def_id::{DefId, LocalDefId};
@ -354,7 +355,7 @@ fn lint_auto_trait_impls(tcx: TyCtxt<'_>, trait_def_id: DefId, impls: &[LocalDef
// Impls which completely cover a given root type are fine as they
// disable auto impls entirely. So only lint if the substs
// are not a permutation of the identity substs.
match tcx.uses_unique_generic_params(substs, true) {
match tcx.uses_unique_generic_params(substs, IgnoreRegions::Yes) {
Ok(()) => {} // ok
Err(arg) => {
// Ideally: