]> git.lizzy.rs Git - rust.git/commitdiff
Fix ICE checking for feature gated const fn
authorJason Newcomb <jsnewcomb@pm.me>
Sun, 25 Apr 2021 14:07:01 +0000 (10:07 -0400)
committerJason Newcomb <jsnewcomb@pm.me>
Sun, 25 Apr 2021 14:18:15 +0000 (10:18 -0400)
clippy_utils/src/qualify_min_const_fn.rs
tests/ui/crashes/ice-7126.rs [new file with mode: 0644]

index b2ce58b597b3d901a160645b34d04342459f11f3..a08dcf19e5b51481398f933bd35f1bf4401f2ae4 100644 (file)
@@ -364,7 +364,7 @@ fn check_terminator(
 
 fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option<&RustcVersion>) -> bool {
     rustc_mir::const_eval::is_const_fn(tcx, def_id)
-        && if let Some(const_stab) = tcx.lookup_const_stability(def_id) {
+        && tcx.lookup_const_stability(def_id).map_or(true, |const_stab| {
             if let rustc_attr::StabilityLevel::Stable { since } = const_stab.level {
                 // Checking MSRV is manually necessary because `rustc` has no such concept. This entire
                 // function could be removed if `rustc` provided a MSRV-aware version of `is_const_fn`.
@@ -375,10 +375,8 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option<&RustcVersion>) -> b
                         .expect("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted"),
                 )
             } else {
-                // `rustc_mir::const_eval::is_const_fn` should return false for unstably const functions.
-                unreachable!();
+                // Unstable const fn with the feature enabled.
+                msrv.is_none()
             }
-        } else {
-            true
-        }
+        })
 }
diff --git a/tests/ui/crashes/ice-7126.rs b/tests/ui/crashes/ice-7126.rs
new file mode 100644 (file)
index 0000000..ca563ba
--- /dev/null
@@ -0,0 +1,14 @@
+// This test requires a feature gated const fn and will stop working in the future.
+
+#![feature(const_btree_new)]
+
+use std::collections::BTreeMap;
+
+struct Foo(BTreeMap<i32, i32>);
+impl Foo {
+    fn new() -> Self {
+        Self(BTreeMap::new())
+    }
+}
+
+fn main() {}