Skip to content

Rip out old effects var handling code from traits #131968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 4 additions & 49 deletions compiler/rustc_const_eval/src/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir::LangItem;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::mir::*;
use rustc_middle::traits::BuiltinImplSource;
use rustc_middle::ty::{self, AdtDef, GenericArgsRef, Ty};
use rustc_middle::{bug, mir};
use rustc_trait_selection::traits::{
ImplSource, Obligation, ObligationCause, ObligationCtxt, SelectionContext,
};
use tracing::{instrument, trace};
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
use tracing::instrument;

use super::ConstCx;

Expand Down Expand Up @@ -195,50 +192,8 @@ impl Qualif for NeedsNonConstDrop {
return false;
}

// FIXME(effects): If `destruct` is not a `const_trait`,
// or effects are disabled in this crate, then give up.
let destruct_def_id = cx.tcx.require_lang_item(LangItem::Destruct, Some(cx.body.span));
if !cx.tcx.has_host_param(destruct_def_id) || !cx.tcx.features().effects {
return NeedsDrop::in_any_value_of_ty(cx, ty);
}

let obligation = Obligation::new(
cx.tcx,
ObligationCause::dummy_with_span(cx.body.span),
cx.param_env,
ty::TraitRef::new(cx.tcx, destruct_def_id, [
ty::GenericArg::from(ty),
ty::GenericArg::from(cx.tcx.expected_host_effect_param_for_body(cx.def_id())),
]),
);

let infcx = cx.tcx.infer_ctxt().build();
let mut selcx = SelectionContext::new(&infcx);
let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
// If we couldn't select a const destruct candidate, then it's bad
return true;
};

trace!(?impl_src);

if !matches!(
impl_src,
ImplSource::Builtin(BuiltinImplSource::Misc, _) | ImplSource::Param(_)
) {
// If our const destruct candidate is not ConstDestruct or implied by the param env,
// then it's bad
return true;
}

if impl_src.borrow_nested_obligations().is_empty() {
return false;
}

// If we had any errors, then it's bad
let ocx = ObligationCtxt::new(&infcx);
ocx.register_obligations(impl_src.nested_obligations());
let errors = ocx.select_all_or_error();
!errors.is_empty()
// FIXME(effects): Reimplement const drop checking.
NeedsDrop::in_any_value_of_ty(cx, ty)
}

fn in_adt_inherently<'tcx>(
Expand Down
35 changes: 1 addition & 34 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,40 +537,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
//
// This check is here because there is currently no way to express a trait bound for `FnDef` types only.
if let ty::FnDef(def_id, _args) = *arg_ty.kind() {
let fn_once_def_id =
self.tcx.require_lang_item(hir::LangItem::FnOnce, Some(span));
let fn_once_output_def_id =
self.tcx.require_lang_item(hir::LangItem::FnOnceOutput, Some(span));
if self.tcx.has_host_param(fn_once_def_id) {
let const_param: ty::GenericArg<'tcx> =
([self.tcx.consts.false_, self.tcx.consts.true_])[idx].into();
self.register_predicate(traits::Obligation::new(
self.tcx,
self.misc(span),
self.param_env,
ty::TraitRef::new(self.tcx, fn_once_def_id, [
arg_ty.into(),
fn_sig.inputs()[0].into(),
const_param,
]),
));

self.register_predicate(traits::Obligation::new(
self.tcx,
self.misc(span),
self.param_env,
ty::ProjectionPredicate {
projection_term: ty::AliasTerm::new(
self.tcx,
fn_once_output_def_id,
[arg_ty.into(), fn_sig.inputs()[0].into(), const_param],
),
term: fn_sig.output().into(),
},
));

self.select_obligations_where_possible(|_| {});
} else if idx == 0 && !self.tcx.is_const_fn_raw(def_id) {
if idx == 0 && !self.tcx.is_const_fn_raw(def_id) {
self.dcx().emit_err(errors::ConstSelectMustBeConst { span });
}
} else {
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_middle/src/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ pub enum SelectionCandidate<'tcx> {

/// Implementation of a `Fn`-family trait by one of the anonymous
/// types generated for a fn pointer type (e.g., `fn(int) -> int`)
FnPointerCandidate {
fn_host_effect: ty::Const<'tcx>,
},
FnPointerCandidate,

TraitAliasCandidate,

Expand All @@ -180,9 +178,6 @@ pub enum SelectionCandidate<'tcx> {
BuiltinObjectCandidate,

BuiltinUnsizeCandidate,

/// Implementation of `const Destruct`, optionally from a custom `impl const Drop`.
ConstDestructCandidate(Option<DefId>),
}

/// The result of trait evaluation. The order is important
Expand Down
18 changes: 0 additions & 18 deletions compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,24 +907,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

/// Constructs generic args for an item, optionally appending a const effect param type
pub fn with_opt_host_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_host_effect_param_for_body(caller_def_id)));

self.mk_args_from_iter(args.into_iter().map(|arg| arg.into()).chain(opt_const_param))
}

/// Expand any [weak alias types][weak] contained within the given `value`.
///
/// This should be used over other normalization routines in situations where
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_mir_build/src/build/matches/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
};

let eq_def_id = self.tcx.require_lang_item(LangItem::PartialEq, Some(source_info.span));
let method = trait_method(
self.tcx,
eq_def_id,
sym::eq,
self.tcx.with_opt_host_effect_param(self.def_id, eq_def_id, [compare_ty, compare_ty]),
);
let method = trait_method(self.tcx, eq_def_id, sym::eq, [compare_ty, compare_ty]);

let bool_ty = self.tcx.types.bool;
let eq_result = self.temp(bool_ty, source_info.span);
Expand Down
12 changes: 1 addition & 11 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
}

struct ConstToPat<'tcx> {
id: hir::HirId,
span: Span,
param_env: ty::ParamEnv<'tcx>,

Expand All @@ -62,7 +61,6 @@ impl<'tcx> ConstToPat<'tcx> {
) -> Self {
trace!(?pat_ctxt.typeck_results.hir_owner);
ConstToPat {
id,
span,
infcx,
param_env: pat_ctxt.param_env,
Expand Down Expand Up @@ -149,15 +147,7 @@ impl<'tcx> ConstToPat<'tcx> {
tcx,
ObligationCause::dummy(),
self.param_env,
ty::TraitRef::new_from_args(
tcx,
partial_eq_trait_id,
tcx.with_opt_host_effect_param(
tcx.hir().enclosing_body_owner(self.id),
partial_eq_trait_id,
[ty, ty],
),
),
ty::TraitRef::new(tcx, partial_eq_trait_id, [ty, ty]),
);

// This *could* accept a type that isn't actually `PartialEq`, because region bounds get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3701,12 +3701,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
_ => None,
};
// Also add host param, if present
let host = self.tcx.generics_of(trait_pred.def_id()).host_effect_index.map(|idx| trait_pred.skip_binder().trait_ref.args[idx]);
let trait_pred = trait_pred.map_bound_ref(|tr| ty::TraitPredicate {
trait_ref: ty::TraitRef::new(self.tcx,
trait_pred.def_id(),
[field_ty].into_iter().chain(trait_args).chain(host),
[field_ty].into_iter().chain(trait_args),
),
..*tr
});
Expand Down
36 changes: 6 additions & 30 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,24 +1642,9 @@ fn confirm_fn_pointer_candidate<'cx, 'tcx>(
sig,
);

let host_effect_param = match *fn_type.kind() {
ty::FnDef(def_id, args) => tcx
.generics_of(def_id)
.host_effect_index
.map_or(tcx.consts.true_, |idx| args.const_at(idx)),
ty::FnPtr(..) => tcx.consts.true_,
_ => unreachable!("only expected FnPtr or FnDef in `confirm_fn_pointer_candidate`"),
};

confirm_callable_candidate(
selcx,
obligation,
sig,
util::TupleArgumentsFlag::Yes,
host_effect_param,
)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
}

fn confirm_closure_candidate<'cx, 'tcx>(
Expand Down Expand Up @@ -1739,24 +1724,16 @@ fn confirm_closure_candidate<'cx, 'tcx>(

debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");

confirm_callable_candidate(
selcx,
obligation,
closure_sig,
util::TupleArgumentsFlag::No,
// FIXME(effects): This doesn't handle const closures correctly!
selcx.tcx().consts.true_,
)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
.with_addl_obligations(nested)
.with_addl_obligations(obligations)
}

fn confirm_callable_candidate<'cx, 'tcx>(
selcx: &mut SelectionContext<'cx, 'tcx>,
obligation: &ProjectionTermObligation<'tcx>,
fn_sig: ty::PolyFnSig<'tcx>,
flag: util::TupleArgumentsFlag,
fn_host_effect: ty::Const<'tcx>,
) -> Progress<'tcx> {
let tcx = selcx.tcx();

Expand All @@ -1771,7 +1748,6 @@ fn confirm_callable_candidate<'cx, 'tcx>(
obligation.predicate.self_ty(),
fn_sig,
flag,
fn_host_effect,
)
.map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
projection_term: ty::AliasTerm::new_from_args(tcx, fn_once_output_def_id, trait_ref.args),
Expand Down
Loading
Loading