From 04eec37dc2cababaecffd423e330d3c19d3c6169 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 13 Aug 2023 09:02:31 +0000 Subject: Enable effects for libcore --- compiler/rustc_hir_typeck/src/expr.rs | 8 +++++++- compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs | 19 ++++++++++++++----- compiler/rustc_monomorphize/src/collector.rs | 6 ++++++ 3 files changed, 27 insertions(+), 6 deletions(-) (limited to 'compiler') diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 70d1dad8a6f..4ad14ce3059 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -525,8 +525,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0, }; - if let ty::FnDef(did, ..) = *ty.kind() { + if let ty::FnDef(did, callee_args) = *ty.kind() { let fn_sig = ty.fn_sig(tcx); + + // HACK: whenever we get a FnDef in a non-const context, enforce effects to get the + // default `host = true` to avoid inference errors later. + if tcx.hir().body_const_context(self.body_id).is_none() { + self.enforce_context_effects(expr.hir_id, qpath.span(), did, callee_args); + } if tcx.fn_sig(did).skip_binder().abi() == RustIntrinsic && tcx.item_name(did) == sym::transmute { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 4def7867384..37ea94d821e 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -273,11 +273,20 @@ 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 is_const_eval_select && (1..=2).contains(&idx) { - if let ty::FnDef(def_id, _) = checked_ty.kind() { - if idx == 1 && !self.tcx.is_const_fn_raw(*def_id) { - self.tcx - .sess - .emit_err(errors::ConstSelectMustBeConst { span: provided_arg.span }); + if let ty::FnDef(def_id, args) = *checked_ty.kind() { + if idx == 1 { + if !self.tcx.is_const_fn_raw(def_id) { + self.tcx.sess.emit_err(errors::ConstSelectMustBeConst { + span: provided_arg.span, + }); + } else { + self.enforce_context_effects( + provided_arg.hir_id, + provided_arg.span, + def_id, + args, + ) + } } } else { self.tcx.sess.emit_err(errors::ConstSelectMustBeFn { diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 67e821dcf5a..118b3862887 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -815,6 +815,12 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> { mir::AssertKind::BoundsCheck { .. } => LangItem::PanicBoundsCheck, _ => LangItem::Panic, }; + let def_id = tcx.require_lang_item(lang_item, Some(source)); + let instance = if has_host_effect { + Instance::new(def_id, tcx.mk_args(&[tcx.consts.true_.into()])) + } else { + Instance::mono(tcx, def_id) + }; push_mono_lang_item(self, lang_item); } mir::TerminatorKind::UnwindTerminate(reason) => { -- cgit 1.4.1-3-g733a5 From 7b1b17574bf1466ddd7e6657b6122244dc5a73ce Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Tue, 12 Sep 2023 01:07:04 +0000 Subject: fix bugs with effects fallback --- compiler/rustc_monomorphize/src/collector.rs | 6 ------ compiler/rustc_passes/src/lang_items.rs | 12 ++++++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) (limited to 'compiler') diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 118b3862887..67e821dcf5a 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -815,12 +815,6 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> { mir::AssertKind::BoundsCheck { .. } => LangItem::PanicBoundsCheck, _ => LangItem::Panic, }; - let def_id = tcx.require_lang_item(lang_item, Some(source)); - let instance = if has_host_effect { - Instance::new(def_id, tcx.mk_args(&[tcx.consts.true_.into()])) - } else { - Instance::mono(tcx, def_id) - }; push_mono_lang_item(self, lang_item); } mir::TerminatorKind::UnwindTerminate(reason) => { diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index 476394f30cc..7e837243918 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -20,7 +20,8 @@ use rustc_hir::lang_items::{extract, GenericRequirement}; use rustc_hir::{LangItem, LanguageItems, Target}; use rustc_middle::ty::TyCtxt; use rustc_session::cstore::ExternCrate; -use rustc_span::{symbol::kw::Empty, Span}; +use rustc_span::symbol::kw::Empty; +use rustc_span::{sym, Span}; use rustc_middle::query::Providers; @@ -157,7 +158,14 @@ impl<'tcx> LanguageItemCollector<'tcx> { self.tcx.hir().get_by_def_id(item_def_id) { let (actual_num, generics_span) = match kind.generics() { - Some(generics) => (generics.params.len(), generics.span), + Some(generics) => ( + generics + .params + .iter() + .filter(|p| !self.tcx.has_attr(p.def_id, sym::rustc_host)) + .count(), + generics.span, + ), None => (0, *item_span), }; -- cgit 1.4.1-3-g733a5