Skip to content

Commit d3404d2

Browse files
Add with_opt_const_effect_param helper, simplify
1 parent 89c2c85 commit d3404d2

File tree

7 files changed

+37
-30
lines changed

7 files changed

+37
-30
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed, StashKey};
88
use rustc_hir as hir;
99
use rustc_hir::def::{self, CtorKind, Namespace, Res};
1010
use rustc_hir::def_id::DefId;
11-
use rustc_hir::HirId;
1211
use rustc_hir_analysis::autoderef::Autoderef;
1312
use rustc_infer::{
1413
infer,
@@ -373,7 +372,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
373372
) -> Ty<'tcx> {
374373
let (fn_sig, def_id) = match *callee_ty.kind() {
375374
ty::FnDef(def_id, args) => {
376-
self.enforce_context_effects(call_expr.hir_id, call_expr.span, def_id, args);
375+
self.enforce_context_effects(call_expr.span, def_id, args);
377376
let fn_sig = self.tcx.fn_sig(def_id).instantiate(self.tcx, args);
378377

379378
// Unit testing: function items annotated with
@@ -770,7 +769,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
770769
#[tracing::instrument(level = "debug", skip(self, span))]
771770
pub(super) fn enforce_context_effects(
772771
&self,
773-
call_expr_hir: HirId,
774772
span: Span,
775773
callee_did: DefId,
776774
callee_args: GenericArgsRef<'tcx>,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
165165
span: Span,
166166
method: MethodCallee<'tcx>,
167167
) {
168-
self.enforce_context_effects(hir_id, span, method.def_id, method.args);
168+
self.enforce_context_effects(span, method.def_id, method.args);
169169
self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
170170
self.write_args(hir_id, method.args);
171171
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
282282
span: provided_arg.span,
283283
});
284284
} else {
285-
self.enforce_context_effects(
286-
provided_arg.hir_id,
287-
provided_arg.span,
288-
def_id,
289-
args,
290-
)
285+
self.enforce_context_effects(provided_arg.span, def_id, args)
291286
}
292287
}
293288
} else {

compiler/rustc_middle/src/ty/util.rs

+17
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,23 @@ impl<'tcx> TyCtxt<'tcx> {
813813
None => self.consts.true_,
814814
}
815815
}
816+
817+
/// Constructs generic args for an item, optionally appending a const effect param type
818+
pub fn with_opt_const_effect_param(
819+
self,
820+
caller_def_id: LocalDefId,
821+
callee_def_id: DefId,
822+
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
823+
) -> ty::GenericArgsRef<'tcx> {
824+
let generics = self.generics_of(callee_def_id);
825+
assert_eq!(generics.parent, None);
826+
827+
let opt_const_param = generics.host_effect_index.is_some().then(|| {
828+
ty::GenericArg::from(self.expected_const_effect_param_for_body(caller_def_id))
829+
});
830+
831+
self.mk_args_from_iter(args.into_iter().map(|arg| arg.into()).chain(opt_const_param))
832+
}
816833
}
817834

818835
struct OpaqueTypeExpander<'tcx> {

compiler/rustc_mir_build/src/build/matches/test.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
494494
}
495495

496496
let eq_def_id = self.tcx.require_lang_item(LangItem::PartialEq, Some(source_info.span));
497-
498-
let mut args: Vec<ty::GenericArg<'tcx>> = vec![ty.into(), ty.into()];
499-
// If `PartialEq` is `#[const_trait]`, then add a const effect param
500-
if self.tcx.generics_of(eq_def_id).host_effect_index.is_some() {
501-
args.push(self.tcx.expected_const_effect_param_for_body(self.def_id).into());
502-
}
503-
504-
let method = trait_method(self.tcx, eq_def_id, sym::eq, args);
497+
let method = trait_method(
498+
self.tcx,
499+
eq_def_id,
500+
sym::eq,
501+
self.tcx.with_opt_const_effect_param(self.def_id, eq_def_id, [ty, ty]),
502+
);
505503

506504
let bool_ty = self.tcx.types.bool;
507505
let eq_result = self.temp(bool_ty, source_info.span);

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,19 @@ impl<'tcx> ConstToPat<'tcx> {
265265
// error, because that's never worked, due to compiler
266266
// using `PartialEq::eq` in this scenario in the past.)
267267
let partial_eq_trait_id = tcx.require_lang_item(hir::LangItem::PartialEq, Some(self.span));
268-
let mut args: Vec<ty::GenericArg<'tcx>> = vec![ty.into(), ty.into()];
269-
// If `PartialEq` is `#[const_trait]`, then add a const effect param
270-
if tcx.generics_of(partial_eq_trait_id).host_effect_index.is_some() {
271-
args.push(
272-
tcx.expected_const_effect_param_for_body(tcx.hir().enclosing_body_owner(self.id))
273-
.into(),
274-
);
275-
}
276-
277268
let partial_eq_obligation = Obligation::new(
278269
tcx,
279270
ObligationCause::dummy(),
280271
self.param_env,
281-
ty::TraitRef::new(tcx, partial_eq_trait_id, args),
272+
ty::TraitRef::new(
273+
tcx,
274+
partial_eq_trait_id,
275+
tcx.with_opt_const_effect_param(
276+
tcx.hir().enclosing_body_owner(self.id),
277+
partial_eq_trait_id,
278+
[ty, ty],
279+
),
280+
),
282281
);
283282

284283
// This *could* accept a type that isn't actually `PartialEq`, because region bounds get

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ trait PartialEq<Rhs: ?Sized = Self> {
344344
}
345345
}
346346

347-
impl<A: ?Sized, B: ?Sized> PartialEq<&B> for &A
347+
impl<A: ?Sized, B: ?Sized> const PartialEq<&B> for &A
348348
where
349-
A: PartialEq<B>,
349+
A: ~const PartialEq<B>,
350350
{
351351
fn eq(&self, other: &&B) -> bool {
352352
PartialEq::eq(*self, *other)

0 commit comments

Comments
 (0)