From d3404d2b987c2bc1ee4b2eb73f1793fa201a2b0d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 28 Nov 2023 21:17:55 +0000 Subject: [PATCH] Add with_opt_const_effect_param helper, simplify --- compiler/rustc_hir_typeck/src/callee.rs | 4 +--- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 2 +- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 7 +------ compiler/rustc_middle/src/ty/util.rs | 17 +++++++++++++++++ .../rustc_mir_build/src/build/matches/test.rs | 14 ++++++-------- .../src/thir/pattern/const_to_pat.rs | 19 +++++++++---------- .../effects/minicore.rs | 4 ++-- 7 files changed, 37 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index 0c6c6926c4e..d3abe0d7e1a 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -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>, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index fae192e230c..23b9ac4139d 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -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); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 87c93afe540..f1720dc98e6 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -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 { diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index afa6eac4e2f..40d592ddef0 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -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>>, + ) -> 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> { diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 0b7fad26f69..eedb8cc4f39 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -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> = 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); diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index 4db9e71ed25..4e2c932d6c6 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -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> = 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 diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs index 007d681f3eb..ecf45c97dcd 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs @@ -344,9 +344,9 @@ trait PartialEq { } } -impl PartialEq<&B> for &A +impl const PartialEq<&B> for &A where - A: PartialEq, + A: ~const PartialEq, { fn eq(&self, other: &&B) -> bool { PartialEq::eq(*self, *other)