2020-01-05 23:43:45 +01:00
|
|
|
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
|
|
|
|
|
|
2023-02-11 23:05:11 +00:00
|
|
|
use crate::traits::{self, ObligationCause, ObligationCtxt};
|
2020-01-05 23:43:45 +01:00
|
|
|
|
2022-12-09 19:58:46 +00:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2020-01-05 23:43:45 +01:00
|
|
|
use rustc_hir as hir;
|
2023-02-11 23:05:11 +00:00
|
|
|
use rustc_infer::infer::canonical::Canonical;
|
2022-12-09 20:59:26 +00:00
|
|
|
use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
|
2023-02-12 19:32:07 +00:00
|
|
|
use rustc_infer::traits::query::NoSolution;
|
2022-12-09 20:59:26 +00:00
|
|
|
use rustc_infer::{infer::outlives::env::OutlivesEnvironment, traits::FulfillmentError};
|
2023-02-11 23:05:11 +00:00
|
|
|
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt, TypeVisitable};
|
|
|
|
|
use rustc_span::DUMMY_SP;
|
2020-02-22 11:44:18 +01:00
|
|
|
|
2022-12-09 19:58:46 +00:00
|
|
|
use super::outlives_bounds::InferCtxtExt;
|
|
|
|
|
|
2020-01-05 23:43:45 +01:00
|
|
|
pub enum CopyImplementationError<'tcx> {
|
2022-12-09 20:59:26 +00:00
|
|
|
InfrigingFields(Vec<(&'tcx ty::FieldDef, Ty<'tcx>, InfringingFieldsReason<'tcx>)>),
|
2020-01-05 23:43:45 +01:00
|
|
|
NotAnAdt,
|
|
|
|
|
HasDestructor,
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 20:59:26 +00:00
|
|
|
pub enum InfringingFieldsReason<'tcx> {
|
|
|
|
|
Fulfill(Vec<FulfillmentError<'tcx>>),
|
|
|
|
|
Regions(Vec<RegionResolutionError<'tcx>>),
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:41:02 +00:00
|
|
|
/// Checks that the fields of the type (an ADT) all implement copy.
|
|
|
|
|
///
|
|
|
|
|
/// If fields don't implement copy, return an error containing a list of
|
|
|
|
|
/// those violating fields. If it's not an ADT, returns `Err(NotAnAdt)`.
|
|
|
|
|
pub fn type_allowed_to_implement_copy<'tcx>(
|
2020-01-05 23:43:45 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
self_type: Ty<'tcx>,
|
2022-06-03 19:17:12 -07:00
|
|
|
parent_cause: ObligationCause<'tcx>,
|
2020-01-05 23:43:45 +01:00
|
|
|
) -> Result<(), CopyImplementationError<'tcx>> {
|
2020-08-03 00:49:11 +02:00
|
|
|
let (adt, substs) = match self_type.kind() {
|
2020-01-05 23:43:45 +01:00
|
|
|
// These types used to have a builtin impl.
|
|
|
|
|
// Now libcore provides that impl.
|
|
|
|
|
ty::Uint(_)
|
|
|
|
|
| ty::Int(_)
|
|
|
|
|
| ty::Bool
|
|
|
|
|
| ty::Float(_)
|
|
|
|
|
| ty::Char
|
|
|
|
|
| ty::RawPtr(..)
|
|
|
|
|
| ty::Never
|
2021-06-05 17:17:35 -04:00
|
|
|
| ty::Ref(_, _, hir::Mutability::Not)
|
|
|
|
|
| ty::Array(..) => return Ok(()),
|
2020-01-05 23:43:45 +01:00
|
|
|
|
|
|
|
|
ty::Adt(adt, substs) => (adt, substs),
|
|
|
|
|
|
|
|
|
|
_ => return Err(CopyImplementationError::NotAnAdt),
|
|
|
|
|
};
|
|
|
|
|
|
2022-11-30 20:41:02 +00:00
|
|
|
let copy_def_id = tcx.require_lang_item(hir::LangItem::Copy, Some(parent_cause.span));
|
2022-12-09 19:58:46 +00:00
|
|
|
|
2020-01-05 23:43:45 +01:00
|
|
|
let mut infringing = Vec::new();
|
2022-03-05 07:28:41 +11:00
|
|
|
for variant in adt.variants() {
|
2020-01-05 23:43:45 +01:00
|
|
|
for field in &variant.fields {
|
2022-11-30 20:41:02 +00:00
|
|
|
// Do this per-field to get better error messages.
|
|
|
|
|
let infcx = tcx.infer_ctxt().build();
|
|
|
|
|
let ocx = traits::ObligationCtxt::new(&infcx);
|
|
|
|
|
|
2023-01-11 20:26:12 +00:00
|
|
|
let unnormalized_ty = field.ty(tcx, substs);
|
|
|
|
|
if unnormalized_ty.references_error() {
|
2022-09-09 15:08:06 -05:00
|
|
|
continue;
|
2020-01-05 23:43:45 +01:00
|
|
|
}
|
2022-12-09 20:59:26 +00:00
|
|
|
|
|
|
|
|
let field_span = tcx.def_span(field.did);
|
|
|
|
|
let field_ty_span = match tcx.hir().get_if_local(field.did) {
|
|
|
|
|
Some(hir::Node::Field(field_def)) => field_def.ty.span,
|
|
|
|
|
_ => field_span,
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-06 15:57:29 -08:00
|
|
|
// FIXME(compiler-errors): This gives us better spans for bad
|
|
|
|
|
// projection types like in issue-50480.
|
|
|
|
|
// If the ADT has substs, point to the cause we are given.
|
|
|
|
|
// If it does not, then this field probably doesn't normalize
|
|
|
|
|
// to begin with, and point to the bad field's span instead.
|
2022-12-09 20:59:26 +00:00
|
|
|
let normalization_cause = if field
|
2022-03-05 07:28:41 +11:00
|
|
|
.ty(tcx, traits::InternalSubsts::identity_for_item(tcx, adt.did()))
|
2022-10-04 09:43:34 +00:00
|
|
|
.has_non_region_param()
|
2022-09-19 22:03:59 -05:00
|
|
|
{
|
2022-06-03 19:17:12 -07:00
|
|
|
parent_cause.clone()
|
2022-09-19 22:03:59 -05:00
|
|
|
} else {
|
2022-12-09 20:59:26 +00:00
|
|
|
ObligationCause::dummy_with_span(field_ty_span)
|
2022-09-19 22:03:59 -05:00
|
|
|
};
|
2023-01-11 20:26:12 +00:00
|
|
|
let ty = ocx.normalize(&normalization_cause, param_env, unnormalized_ty);
|
2022-11-30 20:41:02 +00:00
|
|
|
let normalization_errors = ocx.select_where_possible();
|
|
|
|
|
if !normalization_errors.is_empty() {
|
2023-01-11 20:26:12 +00:00
|
|
|
tcx.sess.delay_span_bug(field_span, format!("couldn't normalize struct field `{unnormalized_ty}` when checking Copy implementation"));
|
2022-11-30 20:41:02 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-09 20:59:26 +00:00
|
|
|
ocx.register_bound(
|
|
|
|
|
ObligationCause::dummy_with_span(field_ty_span),
|
|
|
|
|
param_env,
|
|
|
|
|
ty,
|
|
|
|
|
copy_def_id,
|
|
|
|
|
);
|
|
|
|
|
let errors = ocx.select_all_or_error();
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
|
infringing.push((field, ty, InfringingFieldsReason::Fulfill(errors)));
|
2022-11-30 20:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 19:58:46 +00:00
|
|
|
// Check regions assuming the self type of the impl is WF
|
|
|
|
|
let outlives_env = OutlivesEnvironment::with_bounds(
|
|
|
|
|
param_env,
|
|
|
|
|
Some(&infcx),
|
|
|
|
|
infcx.implied_bounds_tys(
|
|
|
|
|
param_env,
|
|
|
|
|
parent_cause.body_id,
|
|
|
|
|
FxIndexSet::from_iter([self_type]),
|
|
|
|
|
),
|
|
|
|
|
);
|
2022-11-30 20:41:02 +00:00
|
|
|
infcx.process_registered_region_obligations(
|
|
|
|
|
outlives_env.region_bound_pairs(),
|
|
|
|
|
param_env,
|
|
|
|
|
);
|
2022-12-09 20:59:26 +00:00
|
|
|
let errors = infcx.resolve_regions(&outlives_env);
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
|
infringing.push((field, ty, InfringingFieldsReason::Regions(errors)));
|
2022-11-30 20:41:02 +00:00
|
|
|
}
|
2020-01-05 23:43:45 +01:00
|
|
|
}
|
2022-09-19 22:03:59 -05:00
|
|
|
}
|
2022-11-30 20:41:02 +00:00
|
|
|
|
2020-01-05 23:43:45 +01:00
|
|
|
if !infringing.is_empty() {
|
|
|
|
|
return Err(CopyImplementationError::InfrigingFields(infringing));
|
|
|
|
|
}
|
2022-11-30 20:41:02 +00:00
|
|
|
|
2020-01-05 23:43:45 +01:00
|
|
|
if adt.has_dtor(tcx) {
|
|
|
|
|
return Err(CopyImplementationError::HasDestructor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2023-02-11 23:05:11 +00:00
|
|
|
|
2023-02-12 19:32:07 +00:00
|
|
|
pub fn check_tys_might_be_eq<'tcx>(
|
2023-02-11 23:05:11 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
canonical: Canonical<'tcx, (ParamEnv<'tcx>, Ty<'tcx>, Ty<'tcx>)>,
|
2023-02-12 19:32:07 +00:00
|
|
|
) -> Result<(), NoSolution> {
|
2023-02-11 23:05:11 +00:00
|
|
|
let (infcx, (param_env, ty_a, ty_b), _) =
|
|
|
|
|
tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &canonical);
|
|
|
|
|
let ocx = ObligationCtxt::new(&infcx);
|
|
|
|
|
|
|
|
|
|
let result = ocx.eq(&ObligationCause::dummy(), param_env, ty_a, ty_b);
|
|
|
|
|
// use `select_where_possible` instead of `select_all_or_error` so that
|
|
|
|
|
// we don't get errors from obligations being ambiguous.
|
|
|
|
|
let errors = ocx.select_where_possible();
|
|
|
|
|
|
2023-02-12 19:32:07 +00:00
|
|
|
if errors.len() > 0 || result.is_err() { Err(NoSolution) } else { Ok(()) }
|
2023-02-11 23:05:11 +00:00
|
|
|
}
|