]> git.lizzy.rs Git - rust.git/commitdiff
Allow features like const_try in d_m_b_i_c
authorDeadbeef <ent3rm4n@gmail.com>
Wed, 29 Sep 2021 12:15:35 +0000 (12:15 +0000)
committerDeadbeef <ent3rm4n@gmail.com>
Wed, 24 Nov 2021 07:57:44 +0000 (15:57 +0800)
compiler/rustc_passes/src/check_const.rs
src/test/ui/rfc-2632-const-trait-impl/trait-default-body-stability.rs [new file with mode: 0644]

index 82486a6a5f2e277a75b639edd84c5127e1af0750..9ccf76b5700c201a84cdcf36c2cf9ff62b8e53e4 100644 (file)
@@ -173,6 +173,12 @@ fn const_check_violated(&self, expr: NonConstExpr, span: Span) {
                 None => return true,
             };
 
+            // If the function belongs to a trait, then it must enable the const_trait_impl
+            // feature to use that trait function (with a const default body).
+            if tcx.trait_of_item(def_id).is_some() {
+                return true;
+            }
+
             // If this crate is not using stability attributes, or this function is not claiming to be a
             // stable `const fn`, that is all that is required.
             if !tcx.features().staged_api || tcx.has_attr(def_id, sym::rustc_const_unstable) {
diff --git a/src/test/ui/rfc-2632-const-trait-impl/trait-default-body-stability.rs b/src/test/ui/rfc-2632-const-trait-impl/trait-default-body-stability.rs
new file mode 100644 (file)
index 0000000..cbfdf89
--- /dev/null
@@ -0,0 +1,51 @@
+// check-pass
+
+#![feature(staged_api)]
+#![feature(const_trait_impl)]
+#![feature(const_fn_trait_bound)]
+#![feature(const_t_try)]
+#![feature(const_try)]
+#![feature(try_trait_v2)]
+
+#![stable(feature = "foo", since = "1.0")]
+
+use std::ops::{ControlFlow, FromResidual, Try};
+
+#[stable(feature = "foo", since = "1.0")]
+pub struct T;
+
+#[stable(feature = "foo", since = "1.0")]
+#[rustc_const_unstable(feature = "const_t_try", issue = "none")]
+impl const Try for T {
+    type Output = T;
+    type Residual = T;
+
+    fn from_output(t: T) -> T {
+        t
+    }
+
+    fn branch(self) -> ControlFlow<T, T> {
+        ControlFlow::Continue(self)
+    }
+}
+
+#[stable(feature = "foo", since = "1.0")]
+#[rustc_const_unstable(feature = "const_t_try", issue = "none")]
+impl const FromResidual for T {
+    fn from_residual(t: T) -> T {
+        t
+    }
+}
+
+#[stable(feature = "foo", since = "1.0")]
+pub trait Tr {
+    #[default_method_body_is_const]
+    #[stable(feature = "foo", since = "1.0")]
+    fn bar() -> T {
+        T?
+        // Should be allowed.
+        // Must enable unstable features to call this trait fn in const contexts.
+    }
+}
+
+fn main() {}