Skip to content

Commit 22e53f4

Browse files
committed
Enable effects for libcore
1 parent b4e54c6 commit 22e53f4

28 files changed

+270
-189
lines changed

compiler/rustc_codegen_ssa/src/common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
123123
) -> (Bx::FnAbiOfResult, Bx::Value) {
124124
let tcx = bx.tcx();
125125
let def_id = tcx.require_lang_item(li, span);
126-
let instance = ty::Instance::mono(tcx, def_id);
126+
let instance = if li == LangItem::Panic {
127+
ty::Instance::new(def_id, tcx.mk_args(&[tcx.consts.true_.into()]))
128+
} else {
129+
ty::Instance::mono(tcx, def_id)
130+
};
127131
(bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance))
128132
}
129133

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
525525
_ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
526526
};
527527

528-
if let ty::FnDef(did, ..) = *ty.kind() {
528+
if let ty::FnDef(did, callee_args) = *ty.kind() {
529529
let fn_sig = ty.fn_sig(tcx);
530+
531+
// HACK: whenever we get a FnDef in a non-const context, enforce effects to get the
532+
// default `host = true` to avoid inference errors later.
533+
if tcx.hir().body_const_context(self.body_id).is_none() {
534+
self.enforce_context_effects(expr.hir_id, qpath.span(), did, callee_args);
535+
}
530536
if tcx.fn_sig(did).skip_binder().abi() == RustIntrinsic
531537
&& tcx.item_name(did) == sym::transmute
532538
{

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
273273
//
274274
// This check is here because there is currently no way to express a trait bound for `FnDef` types only.
275275
if is_const_eval_select && (1..=2).contains(&idx) {
276-
if let ty::FnDef(def_id, _) = checked_ty.kind() {
277-
if idx == 1 && !self.tcx.is_const_fn_raw(*def_id) {
278-
self.tcx
279-
.sess
280-
.emit_err(errors::ConstSelectMustBeConst { span: provided_arg.span });
276+
if let ty::FnDef(def_id, args) = *checked_ty.kind() {
277+
if idx == 1 {
278+
if !self.tcx.is_const_fn_raw(def_id) {
279+
self.tcx.sess.emit_err(errors::ConstSelectMustBeConst {
280+
span: provided_arg.span,
281+
});
282+
} else {
283+
self.enforce_context_effects(
284+
provided_arg.hir_id,
285+
provided_arg.span,
286+
def_id,
287+
args,
288+
)
289+
}
281290
}
282291
} else {
283292
self.tcx.sess.emit_err(errors::ConstSelectMustBeFn {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ fn should_encode_span(def_kind: DefKind) -> bool {
854854
}
855855
}
856856

857-
fn should_encode_attrs(def_kind: DefKind) -> bool {
857+
fn should_encode_attrs(def_kind: DefKind, def_id: DefId, tcx: TyCtxt<'_>) -> bool {
858858
match def_kind {
859859
DefKind::Mod
860860
| DefKind::Struct
@@ -879,8 +879,9 @@ fn should_encode_attrs(def_kind: DefKind) -> bool {
879879
// https://github.com/model-checking/kani and is not a performance
880880
// or maintenance issue for us.
881881
DefKind::Closure => true,
882+
// encode the host param attr
883+
DefKind::ConstParam => tcx.has_attr(def_id, sym::rustc_host),
882884
DefKind::TyParam
883-
| DefKind::ConstParam
884885
| DefKind::Ctor(..)
885886
| DefKind::ExternCrate
886887
| DefKind::Use
@@ -1354,7 +1355,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13541355
let def_span = tcx.def_span(local_id);
13551356
record!(self.tables.def_span[def_id] <- def_span);
13561357
}
1357-
if should_encode_attrs(def_kind) {
1358+
if should_encode_attrs(def_kind, def_id, tcx) {
13581359
self.encode_attrs(local_id);
13591360
}
13601361
if should_encode_expn_that_defined(def_kind) {

compiler/rustc_monomorphize/src/collector.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,9 +836,15 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
836836
}
837837
}
838838
mir::TerminatorKind::Assert { ref msg, .. } => {
839-
let lang_item = match &**msg {
840-
mir::AssertKind::BoundsCheck { .. } => LangItem::PanicBoundsCheck,
841-
_ => LangItem::Panic,
839+
let (lang_item, has_host_effect) = match &**msg {
840+
mir::AssertKind::BoundsCheck { .. } => (LangItem::PanicBoundsCheck, false),
841+
_ => (LangItem::Panic, true),
842+
};
843+
let def_id = tcx.require_lang_item(lang_item, Some(source));
844+
let instance = if has_host_effect {
845+
Instance::new(def_id, tcx.mk_args(&[tcx.consts.true_.into()]))
846+
} else {
847+
Instance::mono(tcx, def_id)
842848
};
843849
push_mono_lang_item(self, lang_item);
844850
}

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
//
195195
// Language features:
196196
// tidy-alphabetical-start
197+
#![cfg_attr(not(bootstrap), feature(effects))]
197198
#![feature(abi_unadjusted)]
198199
#![feature(adt_const_params)]
199200
#![feature(allow_internal_unsafe)]

library/core/src/marker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ marker_impls! {
960960
#[lang = "destruct"]
961961
#[rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg)]
962962
#[rustc_deny_explicit_impl(implement_via_object = false)]
963-
#[const_trait]
963+
// FIXME(effects) #[const_trait]
964964
pub trait Destruct {}
965965

966966
/// A marker for tuple types.

library/core/src/ops/drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
/// [nomicon]: ../../nomicon/phantom-data.html#an-exception-the-special-case-of-the-standard-library-and-its-unstable-may_dangle
203203
#[lang = "drop"]
204204
#[stable(feature = "rust1", since = "1.0.0")]
205-
#[const_trait]
205+
// FIXME(effects) #[const_trait]
206206
pub trait Drop {
207207
/// Executes the destructor for this type.
208208
///

library/core/src/ops/function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use crate::marker::Tuple;
7272
)]
7373
#[fundamental] // so that regex can rely that `&str: !FnMut`
7474
#[must_use = "closures are lazy and do nothing unless called"]
75-
#[const_trait]
75+
// FIXME(effects) #[const_trait]
7676
pub trait Fn<Args: Tuple>: FnMut<Args> {
7777
/// Performs the call operation.
7878
#[unstable(feature = "fn_traits", issue = "29625")]
@@ -159,7 +159,7 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
159159
)]
160160
#[fundamental] // so that regex can rely that `&str: !FnMut`
161161
#[must_use = "closures are lazy and do nothing unless called"]
162-
#[const_trait]
162+
// FIXME(effects) #[const_trait]
163163
pub trait FnMut<Args: Tuple>: FnOnce<Args> {
164164
/// Performs the call operation.
165165
#[unstable(feature = "fn_traits", issue = "29625")]
@@ -238,7 +238,7 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
238238
)]
239239
#[fundamental] // so that regex can rely that `&str: !FnMut`
240240
#[must_use = "closures are lazy and do nothing unless called"]
241-
#[const_trait]
241+
// FIXME(effects) #[const_trait]
242242
pub trait FnOnce<Args: Tuple> {
243243
/// The returned type after the call operator is used.
244244
#[lang = "fn_once_output"]
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
error[E0493]: destructor of `T` cannot be evaluated at compile-time
2-
--> $DIR/const-block-const-bound.rs:8:32
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/const-block-const-bound.rs:8:22
33
|
44
LL | const fn f<T: ~const Destruct>(x: T) {}
5-
| ^ - value is dropped here
6-
| |
7-
| the destructor for this type cannot be evaluated in constant functions
5+
| ^^^^^^^^
86

97
error: aborting due to previous error
108

11-
For more information about this error, try `rustc --explain E0493`.

0 commit comments

Comments
 (0)