Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ICE checking for feature gated const fn #7128

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the return value change here depending on whether MSRV is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no stable rust version where the function is const as there's no stability attribute. If the MSRV is set, then the function is clearly not const. If it's not set, then the question becomes is the function const now, which is true.

}
} else {
true
}
})
}
14 changes: 14 additions & 0 deletions tests/ui/crashes/ice-7126.rs
Original file line number Diff line number Diff line change
@@ -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() {}