Add with_opt_const_effect_param helper, simplify

This commit is contained in:
Michael Goulet 2023-11-28 21:17:55 +00:00
parent 89c2c85fe1
commit d3404d2b98
7 changed files with 37 additions and 30 deletions

View file

@ -8,7 +8,6 @@ use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{self, CtorKind, Namespace, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::HirId;
use rustc_hir_analysis::autoderef::Autoderef;
use rustc_infer::{
infer,
@ -373,7 +372,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> Ty<'tcx> {
let (fn_sig, def_id) = match *callee_ty.kind() {
ty::FnDef(def_id, args) => {
self.enforce_context_effects(call_expr.hir_id, call_expr.span, def_id, args);
self.enforce_context_effects(call_expr.span, def_id, args);
let fn_sig = self.tcx.fn_sig(def_id).instantiate(self.tcx, args);
// Unit testing: function items annotated with
@ -770,7 +769,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
#[tracing::instrument(level = "debug", skip(self, span))]
pub(super) fn enforce_context_effects(
&self,
call_expr_hir: HirId,
span: Span,
callee_did: DefId,
callee_args: GenericArgsRef<'tcx>,

View file

@ -165,7 +165,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span,
method: MethodCallee<'tcx>,
) {
self.enforce_context_effects(hir_id, span, method.def_id, method.args);
self.enforce_context_effects(span, method.def_id, method.args);
self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
self.write_args(hir_id, method.args);
}

View file

@ -282,12 +282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: provided_arg.span,
});
} else {
self.enforce_context_effects(
provided_arg.hir_id,
provided_arg.span,
def_id,
args,
)
self.enforce_context_effects(provided_arg.span, def_id, args)
}
}
} else {

View file

@ -813,6 +813,23 @@ impl<'tcx> TyCtxt<'tcx> {
None => self.consts.true_,
}
}
/// Constructs generic args for an item, optionally appending a const effect param type
pub fn with_opt_const_effect_param(
self,
caller_def_id: LocalDefId,
callee_def_id: DefId,
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
) -> ty::GenericArgsRef<'tcx> {
let generics = self.generics_of(callee_def_id);
assert_eq!(generics.parent, None);
let opt_const_param = generics.host_effect_index.is_some().then(|| {
ty::GenericArg::from(self.expected_const_effect_param_for_body(caller_def_id))
});
self.mk_args_from_iter(args.into_iter().map(|arg| arg.into()).chain(opt_const_param))
}
}
struct OpaqueTypeExpander<'tcx> {

View file

@ -494,14 +494,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
let eq_def_id = self.tcx.require_lang_item(LangItem::PartialEq, Some(source_info.span));
let mut args: Vec<ty::GenericArg<'tcx>> = vec![ty.into(), ty.into()];
// If `PartialEq` is `#[const_trait]`, then add a const effect param
if self.tcx.generics_of(eq_def_id).host_effect_index.is_some() {
args.push(self.tcx.expected_const_effect_param_for_body(self.def_id).into());
}
let method = trait_method(self.tcx, eq_def_id, sym::eq, args);
let method = trait_method(
self.tcx,
eq_def_id,
sym::eq,
self.tcx.with_opt_const_effect_param(self.def_id, eq_def_id, [ty, ty]),
);
let bool_ty = self.tcx.types.bool;
let eq_result = self.temp(bool_ty, source_info.span);

View file

@ -265,20 +265,19 @@ impl<'tcx> ConstToPat<'tcx> {
// error, because that's never worked, due to compiler
// using `PartialEq::eq` in this scenario in the past.)
let partial_eq_trait_id = tcx.require_lang_item(hir::LangItem::PartialEq, Some(self.span));
let mut args: Vec<ty::GenericArg<'tcx>> = vec![ty.into(), ty.into()];
// If `PartialEq` is `#[const_trait]`, then add a const effect param
if tcx.generics_of(partial_eq_trait_id).host_effect_index.is_some() {
args.push(
tcx.expected_const_effect_param_for_body(tcx.hir().enclosing_body_owner(self.id))
.into(),
);
}
let partial_eq_obligation = Obligation::new(
tcx,
ObligationCause::dummy(),
self.param_env,
ty::TraitRef::new(tcx, partial_eq_trait_id, args),
ty::TraitRef::new(
tcx,
partial_eq_trait_id,
tcx.with_opt_const_effect_param(
tcx.hir().enclosing_body_owner(self.id),
partial_eq_trait_id,
[ty, ty],
),
),
);
// This *could* accept a type that isn't actually `PartialEq`, because region bounds get

View file

@ -344,9 +344,9 @@ trait PartialEq<Rhs: ?Sized = Self> {
}
}
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &A
where
A: PartialEq<B>,
A: ~const PartialEq<B>,
{
fn eq(&self, other: &&B) -> bool {
PartialEq::eq(*self, *other)