Enable effects for libcore

This commit is contained in:
Deadbeef 2023-08-13 09:02:31 +00:00
parent 3b9e0feff4
commit 04eec37dc2
26 changed files with 261 additions and 185 deletions

View file

@ -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
{

View file

@ -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 {

View file

@ -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) => {

View file

@ -195,6 +195,7 @@
//
// Language features:
// tidy-alphabetical-start
#![cfg_attr(not(bootstrap), feature(effects))]
#![feature(abi_unadjusted)]
#![feature(adt_const_params)]
#![feature(allow_internal_unsafe)]

View file

@ -960,7 +960,7 @@ marker_impls! {
#[lang = "destruct"]
#[rustc_on_unimplemented(message = "can't drop `{Self}`", append_const_msg)]
#[rustc_deny_explicit_impl(implement_via_object = false)]
#[const_trait]
// FIXME(effects) #[const_trait]
pub trait Destruct {}
/// A marker for tuple types.

View file

@ -202,7 +202,7 @@
/// [nomicon]: ../../nomicon/phantom-data.html#an-exception-the-special-case-of-the-standard-library-and-its-unstable-may_dangle
#[lang = "drop"]
#[stable(feature = "rust1", since = "1.0.0")]
#[const_trait]
// FIXME(effects) #[const_trait]
pub trait Drop {
/// Executes the destructor for this type.
///

View file

@ -72,7 +72,7 @@ use crate::marker::Tuple;
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
#[const_trait]
// FIXME(effects) #[const_trait]
pub trait Fn<Args: Tuple>: FnMut<Args> {
/// Performs the call operation.
#[unstable(feature = "fn_traits", issue = "29625")]
@ -159,7 +159,7 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
#[const_trait]
// FIXME(effects) #[const_trait]
pub trait FnMut<Args: Tuple>: FnOnce<Args> {
/// Performs the call operation.
#[unstable(feature = "fn_traits", issue = "29625")]
@ -238,7 +238,7 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
#[const_trait]
// FIXME(effects) #[const_trait]
pub trait FnOnce<Args: Tuple> {
/// The returned type after the call operator is used.
#[lang = "fn_once_output"]

View file

@ -1,11 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/const-block-const-bound.rs:8:32
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-block-const-bound.rs:8:22
|
LL | const fn f<T: ~const Destruct>(x: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0493`.

View file

@ -10,6 +10,90 @@ error[E0635]: unknown feature `const_cmp`
LL | #![feature(const_cmp)]
| ^^^^^^^^^
error: aborting due to 2 previous errors
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:15:15
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:15:31
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:15:15
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:22:15
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:22:34
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:22:15
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:29:15
|
LL | T: ~const FnOnce<()>,
| ^^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:29:15
|
LL | T: ~const FnOnce<()>,
| ^^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:36:15
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:36:31
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:36:15
|
LL | T: ~const Fn<()> + ~const Destruct,
| ^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:50:15
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:50:34
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/fn_trait_refs.rs:50:15
|
LL | T: ~const FnMut<()> + ~const Destruct,
| ^^^^^^^^^
error: aborting due to 16 previous errors
For more information about this error, try `rustc --explain E0635`.

View file

@ -1,3 +1,5 @@
// known-bug: #110395
// FIXME check-pass
// This is a non-regression test for const-qualification of unstable items in libcore
// as explained in issue #67053.
// const-qualification could miss some `const fn`s if they were unstable and the feature
@ -15,12 +17,12 @@ impl<T> Opt<T> {
#[rustc_const_unstable(feature = "foo", issue = "none")]
#[stable(feature = "rust1", since = "1.0.0")]
const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
//~^ ERROR destructor of
//~| ERROR destructor of
//FIXME ~^ ERROR destructor of
//FIXME ~| ERROR destructor of
match self {
Opt::Some(t) => t,
Opt::None => f(),
//~^ ERROR cannot call
//FIXME ~^ ERROR cannot call
}
}
}

View file

@ -1,34 +1,8 @@
error[E0015]: cannot call non-const closure in constant functions
--> $DIR/unstable-const-fn-in-libcore.rs:22:26
|
LL | Opt::None => f(),
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T + ~const std::ops::FnOnce<()>>(self, f: F) -> T {
| +++++++++++++++++++++++++++++
error[E0493]: destructor of `F` cannot be evaluated at compile-time
--> $DIR/unstable-const-fn-in-libcore.rs:17:60
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/unstable-const-fn-in-libcore.rs:19:39
|
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
| ^ the destructor for this type cannot be evaluated in constant functions
...
LL | }
| - value is dropped here
| ^^^^^^^^^^^^^
error[E0493]: destructor of `Opt<T>` cannot be evaluated at compile-time
--> $DIR/unstable-const-fn-in-libcore.rs:17:54
|
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T>(self, f: F) -> T {
| ^^^^ the destructor for this type cannot be evaluated in constant functions
...
LL | }
| - value is dropped here
error: aborting due to previous error
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0015, E0493.
For more information about an error, try `rustc --explain E0015`.

View file

@ -1,25 +1,14 @@
error[E0015]: cannot call non-const closure in constant functions
--> $DIR/normalize-tait-in-const.rs:26:5
|
LL | fun(filter_positive());
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct + ~const std::ops::Fn<(&Alias<'_>,)>>(fun: F) {
| ++++++++++++++++++++++++++++++++++++
error[E0493]: destructor of `F` cannot be evaluated at compile-time
--> $DIR/normalize-tait-in-const.rs:25:79
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:25:42
|
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
| ^^^ the destructor for this type cannot be evaluated in constant functions
LL | fun(filter_positive());
LL | }
| - value is dropped here
| ^^^^^^^^^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/normalize-tait-in-const.rs:25:69
|
LL | const fn with_positive<F: ~const for<'a> Fn(&'a Alias<'a>) + ~const Destruct>(fun: F) {
| ^^^^^^^^
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0015, E0493.
For more information about an error, try `rustc --explain E0015`.

View file

@ -1,4 +1,5 @@
// check-pass
// known-bug: #110395
// FIXME check-pass
#![feature(const_trait_impl, const_closures)]
#![allow(incomplete_features)]

View file

@ -0,0 +1,8 @@
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-closure-parse-not-item.rs:7:32
|
LL | const fn test() -> impl ~const Fn() {
| ^^^^
error: aborting due to previous error

View file

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

View file

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

View file

@ -1,39 +1,26 @@
error[E0015]: cannot call non-const closure in constant functions
--> $DIR/const-closures.rs:12:5
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-closures.rs:8:19
|
LL | f() * 7
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | F: ~const FnOnce() -> u8 + ~const std::ops::Fn<()>,
| +++++++++++++++++++++++++
LL | F: ~const FnOnce() -> u8,
| ^^^^^^^^^^^^^^
error[E0015]: cannot call non-const closure in constant functions
--> $DIR/const-closures.rs:24:5
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-closures.rs:9:19
|
LL | f() + f()
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn answer<F: ~const Fn() -> u8 + ~const std::ops::Fn<()>>(f: &F) -> u8 {
| +++++++++++++++++++++++++
LL | F: ~const FnMut() -> u8,
| ^^^^^^^^^^^^^
error[E0015]: cannot call non-const closure in constant functions
--> $DIR/const-closures.rs:24:11
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-closures.rs:10:19
|
LL | f() + f()
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
help: consider further restricting this bound
|
LL | const fn answer<F: ~const Fn() -> u8 + ~const std::ops::Fn<()>>(f: &F) -> u8 {
| +++++++++++++++++++++++++
LL | F: ~const Fn() -> u8,
| ^^^^^^^^^^
error: aborting due to 3 previous errors
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-closures.rs:23:27
|
LL | const fn answer<F: ~const Fn() -> u8>(f: &F) -> u8 {
| ^^^^^^^^^^
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0015`.

View file

@ -1,9 +1,20 @@
error[E0493]: destructor of `E` cannot be evaluated at compile-time
--> $DIR/const-drop-bound.rs:12:13
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop-bound.rs:9:68
|
LL | Err(_e) => None,
| ^^ the destructor for this type cannot be evaluated in constant functions
LL | const fn foo<T, E>(res: Result<T, E>) -> Option<T> where E: ~const Destruct {
| ^^^^^^^^
error: aborting due to previous error
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop-bound.rs:20:15
|
LL | T: ~const Destruct,
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop-bound.rs:21:15
|
LL | E: ~const Destruct,
| ^^^^^^^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0493`.

View file

@ -1,11 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/const-drop-fail-2.rs:21:36
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop-fail-2.rs:21:26
|
LL | const fn check<T: ~const Destruct>(_: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0493`.

View file

@ -1,9 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/const-drop-fail.rs:24:36
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop-fail.rs:24:26
|
LL | const fn check<T: ~const Destruct>(_: T) {}
| ^ the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0493`.

View file

@ -1,11 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/const-drop-fail.rs:24:36
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop-fail.rs:24:26
|
LL | const fn check<T: ~const Destruct>(_: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0493`.

View file

@ -1,19 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/const-drop.rs:19:32
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop.rs:19:22
|
LL | const fn a<T: ~const Destruct>(_: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error[E0493]: destructor of `S<'_>` cannot be evaluated at compile-time
--> $DIR/const-drop.rs:24:13
|
LL | let _ = S(&mut c);
| ^^^^^^^^^- value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0493`.

View file

@ -1,19 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/const-drop.rs:19:32
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-drop.rs:19:22
|
LL | const fn a<T: ~const Destruct>(_: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error[E0493]: destructor of `S<'_>` cannot be evaluated at compile-time
--> $DIR/const-drop.rs:24:13
|
LL | let _ = S(&mut c);
| ^^^^^^^^^- value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
error: aborting due to previous error
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0493`.

View file

@ -16,24 +16,60 @@ error: ~const can only be applied to `#[const_trait]` traits
LL | const fn wrap(x: impl ~const PartialEq + ~const Destruct)
| ^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:16:49
|
LL | const fn wrap(x: impl ~const PartialEq + ~const Destruct)
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:17:20
|
LL | -> impl ~const PartialEq + ~const Destruct
| ^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:17:39
|
LL | -> impl ~const PartialEq + ~const Destruct
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:24:29
|
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy;
| ^^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:24:48
|
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy;
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:28:29
|
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
| ^^^^^^^^^
error: aborting due to 6 previous errors
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:28:48
|
LL | fn huh() -> impl ~const PartialEq + ~const Destruct + Copy {
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:49:41
|
LL | const fn apit(_: impl ~const T + ~const Destruct) {}
| ^^^^^^^^
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/const-impl-trait.rs:53:73
|
LL | const fn apit_assoc_bound(_: impl IntoIterator<Item: ~const T> + ~const Destruct) {}
| ^^^^^^^^
error: aborting due to 12 previous errors
For more information about this error, try `rustc --explain E0635`.

View file

@ -0,0 +1,11 @@
// check-pass
#![feature(const_trait_impl, effects)]
const fn a() {}
fn foo<F: FnOnce()>(a: F) {}
fn main() {
let _ = a;
foo(a);
}

View file

@ -1,11 +1,8 @@
error[E0493]: destructor of `T` cannot be evaluated at compile-time
--> $DIR/issue-92111.rs:20:32
error: ~const can only be applied to `#[const_trait]` traits
--> $DIR/issue-92111.rs:20:22
|
LL | const fn a<T: ~const Destruct>(t: T) {}
| ^ - value is dropped here
| |
| the destructor for this type cannot be evaluated in constant functions
| ^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0493`.