Skip to content

Commit 6e4437c

Browse files
authored
Rollup merge of rust-lang#119725 - compiler-errors:has_effect_param, r=fmease
Add helper for when we want to know if an item has a host param r? ```@fmease``` since you're a good reviewer and no good deed goes unpunished This helper will see far more usages as built-in traits get constified.
2 parents ef89314 + e651f6f commit 6e4437c

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ impl Qualif for NeedsNonConstDrop {
157157
// FIXME(effects): If `destruct` is not a `const_trait`,
158158
// or effects are disabled in this crate, then give up.
159159
let destruct_def_id = cx.tcx.require_lang_item(LangItem::Destruct, Some(cx.body.span));
160-
if cx.tcx.generics_of(destruct_def_id).host_effect_index.is_none()
161-
|| !cx.tcx.features().effects
162-
{
160+
if !cx.tcx.has_host_param(destruct_def_id) || !cx.tcx.features().effects {
163161
return NeedsDrop::in_any_value_of_ty(cx, ty);
164162
}
165163

compiler/rustc_hir_typeck/src/callee.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
490490
self.tcx.require_lang_item(hir::LangItem::FnOnce, Some(span));
491491
let fn_once_output_def_id =
492492
self.tcx.require_lang_item(hir::LangItem::FnOnceOutput, Some(span));
493-
if self.tcx.generics_of(fn_once_def_id).host_effect_index.is_none() {
494-
if idx == 0 && !self.tcx.is_const_fn_raw(def_id) {
495-
self.dcx().emit_err(errors::ConstSelectMustBeConst { span });
496-
}
497-
} else {
493+
if self.tcx.has_host_param(fn_once_def_id) {
498494
let const_param: ty::GenericArg<'tcx> =
499495
([self.tcx.consts.false_, self.tcx.consts.true_])[idx].into();
500496
self.register_predicate(traits::Obligation::new(
@@ -523,6 +519,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
523519
));
524520

525521
self.select_obligations_where_possible(|_| {});
522+
} else if idx == 0 && !self.tcx.is_const_fn_raw(def_id) {
523+
self.dcx().emit_err(errors::ConstSelectMustBeConst { span });
526524
}
527525
} else {
528526
self.dcx().emit_err(errors::ConstSelectMustBeFn { span, ty: arg_ty });

compiler/rustc_middle/src/ty/util.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
33
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
4-
use crate::query::Providers;
4+
use crate::query::{IntoQueryParam, Providers};
55
use crate::ty::layout::IntegerExt;
66
use crate::ty::{
77
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
@@ -786,6 +786,13 @@ impl<'tcx> TyCtxt<'tcx> {
786786
|| self.extern_crate(key.as_def_id()).is_some_and(|e| e.is_direct())
787787
}
788788

789+
/// Whether the item has a host effect param. This is different from `TyCtxt::is_const`,
790+
/// because the item must also be "maybe const", and the crate where the item is
791+
/// defined must also have the effects feature enabled.
792+
pub fn has_host_param(self, def_id: impl IntoQueryParam<DefId>) -> bool {
793+
self.generics_of(def_id).host_effect_index.is_some()
794+
}
795+
789796
pub fn expected_host_effect_param_for_body(self, def_id: impl Into<DefId>) -> ty::Const<'tcx> {
790797
let def_id = def_id.into();
791798
// FIXME(effects): This is suspicious and should probably not be done,

compiler/rustc_trait_selection/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ pub fn closure_trait_ref_and_return_type<'tcx>(
271271
TupleArgumentsFlag::No => sig.skip_binder().inputs()[0],
272272
TupleArgumentsFlag::Yes => Ty::new_tup(tcx, sig.skip_binder().inputs()),
273273
};
274-
let trait_ref = if tcx.generics_of(fn_trait_def_id).host_effect_index.is_some() {
274+
let trait_ref = if tcx.has_host_param(fn_trait_def_id) {
275275
ty::TraitRef::new(
276276
tcx,
277277
fn_trait_def_id,

0 commit comments

Comments
 (0)