-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Limit the promotion of const fns to the libstd and the rustc_promotable
attribute
#53851
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e46c0b
Only promote calls to `#[rustc_promotable]` const fns
oli-obk 1f94384
Update error id to an unused one
oli-obk a9156e9
Fix typo
oli-obk f5106a2
Poke in the dark and see if bors+windows stops screaming
oli-obk c793391
Move platform dependent output ui tests to compile-fail
oli-obk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use ty::query::Providers; | ||
use hir::def_id::DefId; | ||
use hir; | ||
use ty::TyCtxt; | ||
use syntax_pos::symbol::Symbol; | ||
use hir::map::blocks::FnLikeNode; | ||
use syntax::attr; | ||
use rustc_target::spec::abi; | ||
|
||
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> { | ||
/// Whether the `def_id` counts as const fn in your current crate, considering all active | ||
/// feature gates | ||
pub fn is_const_fn(self, def_id: DefId) -> bool { | ||
self.is_const_fn_raw(def_id) && match self.lookup_stability(def_id) { | ||
Some(stab) => match stab.const_stability { | ||
// has a `rustc_const_unstable` attribute, check whether the user enabled the | ||
// corresponding feature gate | ||
Some(feature_name) => self.features() | ||
.declared_lib_features | ||
.iter() | ||
.any(|&(sym, _)| sym == feature_name), | ||
// the function has no stability attribute, it is stable as const fn or the user | ||
// needs to use feature gates to use the function at all | ||
None => true, | ||
}, | ||
// functions without stability are either stable user written const fn or the user is | ||
// using feature gates and we thus don't care what they do | ||
None => true, | ||
} | ||
} | ||
|
||
/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it | ||
pub fn is_unstable_const_fn(self, def_id: DefId) -> Option<Symbol> { | ||
if self.is_const_fn_raw(def_id) { | ||
self.lookup_stability(def_id)?.const_stability | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Returns true if this function must conform to `min_const_fn` | ||
pub fn is_min_const_fn(self, def_id: DefId) -> bool { | ||
if self.features().staged_api { | ||
// some intrinsics are waved through if called inside the | ||
// standard library. Users never need to call them directly | ||
if let abi::Abi::RustIntrinsic = self.fn_sig(def_id).abi() { | ||
assert!(!self.is_const_fn(def_id)); | ||
match &self.item_name(def_id).as_str()[..] { | ||
| "size_of" | ||
| "min_align_of" | ||
| "needs_drop" | ||
=> return true, | ||
_ => {}, | ||
} | ||
} | ||
// in order for a libstd function to be considered min_const_fn | ||
// it needs to be stable and have no `rustc_const_unstable` attribute | ||
match self.lookup_stability(def_id) { | ||
// stable functions with unstable const fn aren't `min_const_fn` | ||
Some(&attr::Stability { const_stability: Some(_), .. }) => false, | ||
// unstable functions don't need to conform | ||
Some(&attr::Stability { ref level, .. }) if level.is_unstable() => false, | ||
// everything else needs to conform, because it would be callable from | ||
// other `min_const_fn` functions | ||
_ => true, | ||
} | ||
} else { | ||
// users enabling the `const_fn` can do what they want | ||
!self.sess.features_untracked().const_fn | ||
} | ||
} | ||
} | ||
|
||
|
||
pub fn provide<'tcx>(providers: &mut Providers<'tcx>) { | ||
/// only checks whether the function has a `const` modifier | ||
fn is_const_fn_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool { | ||
let node_id = tcx.hir.as_local_node_id(def_id) | ||
.expect("Non-local call to local provider is_const_fn"); | ||
|
||
if let Some(fn_like) = FnLikeNode::from_node(tcx.hir.get(node_id)) { | ||
fn_like.constness() == hir::Constness::Const | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fn is_promotable_const_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool { | ||
tcx.is_const_fn(def_id) && match tcx.lookup_stability(def_id) { | ||
Some(stab) => { | ||
if cfg!(debug_assertions) && stab.promotable { | ||
let sig = tcx.fn_sig(def_id); | ||
assert_eq!( | ||
sig.unsafety(), | ||
hir::Unsafety::Normal, | ||
"don't mark const unsafe fns as promotable", | ||
// https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682 | ||
); | ||
} | ||
stab.promotable | ||
}, | ||
None => false, | ||
} | ||
} | ||
|
||
*providers = Providers { | ||
is_const_fn_raw, | ||
is_promotable_const_fn, | ||
..*providers | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I collected all the logic in this one file in order to make our lives easier and have less code duplication which might cause odd inconsistencies