|
| 1 | +use ty::query::Providers; |
| 2 | +use hir::def_id::DefId; |
| 3 | +use hir; |
| 4 | +use ty::TyCtxt; |
| 5 | +use syntax_pos::symbol::Symbol; |
| 6 | +use hir::map::blocks::FnLikeNode; |
| 7 | +use syntax::attr; |
| 8 | +use rustc_target::spec::abi; |
| 9 | + |
| 10 | +impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> { |
| 11 | + /// Whether the `def_id` counts as const fn in your current crate, considering all active |
| 12 | + /// feature gates |
| 13 | + pub fn is_const_fn(self, def_id: DefId) -> bool { |
| 14 | + self.is_const_fn_raw(def_id) && match self.lookup_stability(def_id) { |
| 15 | + Some(stab) => match stab.const_stability { |
| 16 | + // has a `rustc_const_unstable` attribute, check whether the user enabled the |
| 17 | + // corresponding feature gate |
| 18 | + Some(feature_name) => self.features() |
| 19 | + .declared_lib_features |
| 20 | + .iter() |
| 21 | + .any(|&(sym, _)| sym == feature_name), |
| 22 | + // the function has no stability attribute, it is stable as const fn or the user |
| 23 | + // needs to use feature gates to use the function at all |
| 24 | + None => true, |
| 25 | + }, |
| 26 | + // functions without stability are either stable user written const fn or the user is |
| 27 | + // using feature gates and we thus don't care what they do |
| 28 | + None => true, |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it |
| 33 | + pub fn is_unstable_const_fn(self, def_id: DefId) -> Option<Symbol> { |
| 34 | + if self.is_const_fn_raw(def_id) { |
| 35 | + self.lookup_stability(def_id)?.const_stability |
| 36 | + } else { |
| 37 | + None |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Returns true if this function must conform to `min_const_fn` |
| 42 | + pub fn is_min_const_fn(self, def_id: DefId) -> bool { |
| 43 | + if self.features().staged_api { |
| 44 | + // some intrinsics are waved through if called inside the |
| 45 | + // standard library. Users never need to call them directly |
| 46 | + if let abi::Abi::RustIntrinsic = self.fn_sig(def_id).abi() { |
| 47 | + assert!(!self.is_const_fn(def_id)); |
| 48 | + match &self.item_name(def_id).as_str()[..] { |
| 49 | + | "size_of" |
| 50 | + | "min_align_of" |
| 51 | + | "needs_drop" |
| 52 | + => return true, |
| 53 | + _ => {}, |
| 54 | + } |
| 55 | + } |
| 56 | + // in order for a libstd function to be considered min_const_fn |
| 57 | + // it needs to be stable and have no `rustc_const_unstable` attribute |
| 58 | + match self.lookup_stability(def_id) { |
| 59 | + // stable functions with unstable const fn aren't `min_const_fn` |
| 60 | + Some(&attr::Stability { const_stability: Some(_), .. }) => false, |
| 61 | + // unstable functions don't need to conform |
| 62 | + Some(&attr::Stability { ref level, .. }) if level.is_unstable() => false, |
| 63 | + // everything else needs to conform, because it would be callable from |
| 64 | + // other `min_const_fn` functions |
| 65 | + _ => true, |
| 66 | + } |
| 67 | + } else { |
| 68 | + // users enabling the `const_fn` can do what they want |
| 69 | + !self.sess.features_untracked().const_fn |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +pub fn provide<'tcx>(providers: &mut Providers<'tcx>) { |
| 76 | + /// only checks whether the function has a `const` modifier |
| 77 | + fn is_const_fn_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool { |
| 78 | + let node_id = tcx.hir.as_local_node_id(def_id) |
| 79 | + .expect("Non-local call to local provider is_const_fn"); |
| 80 | + |
| 81 | + if let Some(fn_like) = FnLikeNode::from_node(tcx.hir.get(node_id)) { |
| 82 | + fn_like.constness() == hir::Constness::Const |
| 83 | + } else { |
| 84 | + false |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn is_promotable_const_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool { |
| 89 | + tcx.is_const_fn(def_id) && match tcx.lookup_stability(def_id) { |
| 90 | + Some(stab) => { |
| 91 | + if cfg!(debug_assertions) && stab.promotable { |
| 92 | + let sig = tcx.fn_sig(def_id); |
| 93 | + assert_eq!( |
| 94 | + sig.unsafety(), |
| 95 | + hir::Unsafety::Normal, |
| 96 | + "don't mark const unsafe fns as promotable", |
| 97 | + // https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682 |
| 98 | + ); |
| 99 | + } |
| 100 | + stab.promotable |
| 101 | + }, |
| 102 | + None => false, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + *providers = Providers { |
| 107 | + is_const_fn_raw, |
| 108 | + is_promotable_const_fn, |
| 109 | + ..*providers |
| 110 | + }; |
| 111 | +} |
0 commit comments