Skip to content

Commit 04eec37

Browse files
committed
Enable effects for libcore
1 parent 3b9e0fe commit 04eec37

26 files changed

+258
-182
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+7-1
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

+14-5
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_monomorphize/src/collector.rs

+6
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,12 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
815815
mir::AssertKind::BoundsCheck { .. } => LangItem::PanicBoundsCheck,
816816
_ => LangItem::Panic,
817817
};
818+
let def_id = tcx.require_lang_item(lang_item, Some(source));
819+
let instance = if has_host_effect {
820+
Instance::new(def_id, tcx.mk_args(&[tcx.consts.true_.into()]))
821+
} else {
822+
Instance::mono(tcx, def_id)
823+
};
818824
push_mono_lang_item(self, lang_item);
819825
}
820826
mir::TerminatorKind::UnwindTerminate(reason) => {

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
//
196196
// Language features:
197197
// tidy-alphabetical-start
198+
#![cfg_attr(not(bootstrap), feature(effects))]
198199
#![feature(abi_unadjusted)]
199200
#![feature(adt_const_params)]
200201
#![feature(allow_internal_unsafe)]

library/core/src/marker.rs

+1-1
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

+1-1
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

+3-3
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"]
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`.

tests/ui/consts/fn_trait_refs.stderr

+85-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,90 @@ error[E0635]: unknown feature `const_cmp`
1010
LL | #![feature(const_cmp)]
1111
| ^^^^^^^^^
1212

13-
error: aborting due to 2 previous errors
13+
error: ~const can only be applied to `#[const_trait]` traits
14+
--> $DIR/fn_trait_refs.rs:15:15
15+
|
16+
LL | T: ~const Fn<()> + ~const Destruct,
17+
| ^^^^^^
18+
19+
error: ~const can only be applied to `#[const_trait]` traits
20+
--> $DIR/fn_trait_refs.rs:15:31
21+
|
22+
LL | T: ~const Fn<()> + ~const Destruct,
23+
| ^^^^^^^^
24+
25+
error: ~const can only be applied to `#[const_trait]` traits
26+
--> $DIR/fn_trait_refs.rs:15:15
27+
|
28+
LL | T: ~const Fn<()> + ~const Destruct,
29+
| ^^^^^^
30+
31+
error: ~const can only be applied to `#[const_trait]` traits
32+
--> $DIR/fn_trait_refs.rs:22:15
33+
|
34+
LL | T: ~const FnMut<()> + ~const Destruct,
35+
| ^^^^^^^^^
36+
37+
error: ~const can only be applied to `#[const_trait]` traits
38+
--> $DIR/fn_trait_refs.rs:22:34
39+
|
40+
LL | T: ~const FnMut<()> + ~const Destruct,
41+
| ^^^^^^^^
42+
43+
error: ~const can only be applied to `#[const_trait]` traits
44+
--> $DIR/fn_trait_refs.rs:22:15
45+
|
46+
LL | T: ~const FnMut<()> + ~const Destruct,
47+
| ^^^^^^^^^
48+
49+
error: ~const can only be applied to `#[const_trait]` traits
50+
--> $DIR/fn_trait_refs.rs:29:15
51+
|
52+
LL | T: ~const FnOnce<()>,
53+
| ^^^^^^^^^^
54+
55+
error: ~const can only be applied to `#[const_trait]` traits
56+
--> $DIR/fn_trait_refs.rs:29:15
57+
|
58+
LL | T: ~const FnOnce<()>,
59+
| ^^^^^^^^^^
60+
61+
error: ~const can only be applied to `#[const_trait]` traits
62+
--> $DIR/fn_trait_refs.rs:36:15
63+
|
64+
LL | T: ~const Fn<()> + ~const Destruct,
65+
| ^^^^^^
66+
67+
error: ~const can only be applied to `#[const_trait]` traits
68+
--> $DIR/fn_trait_refs.rs:36:31
69+
|
70+
LL | T: ~const Fn<()> + ~const Destruct,
71+
| ^^^^^^^^
72+
73+
error: ~const can only be applied to `#[const_trait]` traits
74+
--> $DIR/fn_trait_refs.rs:36:15
75+
|
76+
LL | T: ~const Fn<()> + ~const Destruct,
77+
| ^^^^^^
78+
79+
error: ~const can only be applied to `#[const_trait]` traits
80+
--> $DIR/fn_trait_refs.rs:50:15
81+
|
82+
LL | T: ~const FnMut<()> + ~const Destruct,
83+
| ^^^^^^^^^
84+
85+
error: ~const can only be applied to `#[const_trait]` traits
86+
--> $DIR/fn_trait_refs.rs:50:34
87+
|
88+
LL | T: ~const FnMut<()> + ~const Destruct,
89+
| ^^^^^^^^
90+
91+
error: ~const can only be applied to `#[const_trait]` traits
92+
--> $DIR/fn_trait_refs.rs:50:15
93+
|
94+
LL | T: ~const FnMut<()> + ~const Destruct,
95+
| ^^^^^^^^^
96+
97+
error: aborting due to 16 previous errors
1498

1599
For more information about this error, try `rustc --explain E0635`.

tests/ui/consts/unstable-const-fn-in-libcore.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// known-bug: #110395
2+
// FIXME check-pass
13
// This is a non-regression test for const-qualification of unstable items in libcore
24
// as explained in issue #67053.
35
// const-qualification could miss some `const fn`s if they were unstable and the feature
@@ -15,12 +17,12 @@ impl<T> Opt<T> {
1517
#[rustc_const_unstable(feature = "foo", issue = "none")]
1618
#[stable(feature = "rust1", since = "1.0.0")]
1719
const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
18-
//~^ ERROR destructor of
19-
//~| ERROR destructor of
20+
//FIXME ~^ ERROR destructor of
21+
//FIXME ~| ERROR destructor of
2022
match self {
2123
Opt::Some(t) => t,
2224
Opt::None => f(),
23-
//~^ ERROR cannot call
25+
//FIXME ~^ ERROR cannot call
2426
}
2527
}
2628
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
1-
error[E0015]: cannot call non-const closure in constant functions
2-
--> $DIR/unstable-const-fn-in-libcore.rs:22:26
3-
|
4-
LL | Opt::None => f(),
5-
| ^^^
6-
|
7-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8-
help: consider further restricting this bound
9-
|
10-
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T + ~const std::ops::FnOnce<()>>(self, f: F) -> T {
11-
| +++++++++++++++++++++++++++++
12-
13-
error[E0493]: destructor of `F` cannot be evaluated at compile-time
14-
--> $DIR/unstable-const-fn-in-libcore.rs:17:60
15-
|
16-
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
17-
| ^ the destructor for this type cannot be evaluated in constant functions
18-
...
19-
LL | }
20-
| - value is dropped here
21-
22-
error[E0493]: destructor of `Opt<T>` cannot be evaluated at compile-time
23-
--> $DIR/unstable-const-fn-in-libcore.rs:17:54
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/unstable-const-fn-in-libcore.rs:19:39
243
|
254
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
26-
| ^^^^ the destructor for this type cannot be evaluated in constant functions
27-
...
28-
LL | }
29-
| - value is dropped here
5+
| ^^^^^^^^^^^^^
306

31-
error: aborting due to 3 previous errors
7+
error: aborting due to previous error
328

33-
Some errors have detailed explanations: E0015, E0493.
34-
For more information about an error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
1-
error[E0015]: cannot call non-const closure in constant functions
2-
--> $DIR/normalize-tait-in-const.rs:26:5
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/normalize-tait-in-const.rs:25:42
33
|
4-
LL | fun(filter_positive());
5-
| ^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8-
help: consider further restricting this bound
9-
|
10-
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct + ~const std::ops::Fn<(&Alias<'_>,)>>(fun: F) {
11-
| ++++++++++++++++++++++++++++++++++++
4+
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
5+
| ^^^^^^^^^^^^^^^^^
126

13-
error[E0493]: destructor of `F` cannot be evaluated at compile-time
14-
--> $DIR/normalize-tait-in-const.rs:25:79
7+
error: ~const can only be applied to `#[const_trait]` traits
8+
--> $DIR/normalize-tait-in-const.rs:25:69
159
|
1610
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
17-
| ^^^ the destructor for this type cannot be evaluated in constant functions
18-
LL | fun(filter_positive());
19-
LL | }
20-
| - value is dropped here
11+
| ^^^^^^^^
2112

2213
error: aborting due to 2 previous errors
2314

24-
Some errors have detailed explanations: E0015, E0493.
25-
For more information about an error, try `rustc --explain E0015`.

tests/ui/rfcs/rfc-2632-const-trait-impl/const-closure-parse-not-item.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// check-pass
1+
// known-bug: #110395
2+
// FIXME check-pass
23

34
#![feature(const_trait_impl, const_closures)]
45
#![allow(incomplete_features)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/const-closure-parse-not-item.rs:7:32
3+
|
4+
LL | const fn test() -> impl ~const Fn() {
5+
| ^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
error[E0015]: cannot call non-const closure in constant functions
2-
--> $DIR/const-closure-trait-method-fail.rs:15:5
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/const-closure-trait-method-fail.rs:14:39
33
|
4-
LL | x(())
5-
| ^^^^^
6-
|
7-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8-
help: consider further restricting this bound
9-
|
10-
LL | const fn need_const_closure<T: ~const FnOnce(()) -> i32 + ~const std::ops::FnOnce<((),)>>(x: T) -> i32 {
11-
| ++++++++++++++++++++++++++++++++
4+
LL | const fn need_const_closure<T: ~const FnOnce(()) -> i32>(x: T) -> i32 {
5+
| ^^^^^^^^^^^^^^^^^
126

137
error: aborting due to previous error
148

15-
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
error[E0015]: cannot call non-const closure in constant functions
2-
--> $DIR/const-closure-trait-method.rs:15:5
1+
error: ~const can only be applied to `#[const_trait]` traits
2+
--> $DIR/const-closure-trait-method.rs:14:39
33
|
4-
LL | x(())
5-
| ^^^^^
6-
|
7-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8-
help: consider further restricting this bound
9-
|
10-
LL | const fn need_const_closure<T: ~const FnOnce(()) -> i32 + ~const std::ops::FnOnce<((),)>>(x: T) -> i32 {
11-
| ++++++++++++++++++++++++++++++++
4+
LL | const fn need_const_closure<T: ~const FnOnce(()) -> i32>(x: T) -> i32 {
5+
| ^^^^^^^^^^^^^^^^^
126

137
error: aborting due to previous error
148

15-
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)