Skip to content

Commit

Permalink
Add with_opt_const_effect_param helper, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Nov 28, 2023
1 parent 89c2c85 commit d3404d2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 30 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
14 changes: 6 additions & 8 deletions compiler/rustc_mir_build/src/build/matches/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d3404d2

Please sign in to comment.