]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/core.rs
Rollup merge of #82372 - RalfJung:unsafe-cell, r=KodrAus
[rust.git] / src / librustdoc / core.rs
index a20e9dec33b36f188fb0c83dec69bc11595468c8..1813367f789a12068713f245d0d5fa3565a6a623 100644 (file)
 
 use std::mem;
 use std::rc::Rc;
-use std::{
-    cell::{Cell, RefCell},
-    collections::hash_map::Entry,
-};
+use std::{cell::RefCell, collections::hash_map::Entry};
 
 use crate::clean;
+use crate::clean::inline::build_external_trait;
 use crate::clean::{AttributesExt, MAX_DEF_IDX};
 use crate::config::{Options as RustdocOptions, RenderOptions};
 use crate::config::{OutputFormat, RenderInfo};
@@ -48,7 +46,7 @@
     /// Used for normalization.
     ///
     /// Most of this logic is copied from rustc_lint::late.
-    crate param_env: Cell<ParamEnv<'tcx>>,
+    crate param_env: ParamEnv<'tcx>,
     /// Later on moved into `cache`
     crate renderinfo: RefCell<RenderInfo>,
     /// Later on moved through `clean::Crate` into `cache`
 }
 
 impl<'tcx> DocContext<'tcx> {
-    crate fn sess(&self) -> &Session {
+    crate fn sess(&self) -> &'tcx Session {
         &self.tcx.sess
     }
 
-    crate fn with_param_env<T, F: FnOnce() -> T>(&self, def_id: DefId, f: F) -> T {
-        let old_param_env = self.param_env.replace(self.tcx.param_env(def_id));
-        let ret = f();
-        self.param_env.set(old_param_env);
+    crate fn with_param_env<T, F: FnOnce(&mut Self) -> T>(&mut self, def_id: DefId, f: F) -> T {
+        let old_param_env = mem::replace(&mut self.param_env, self.tcx.param_env(def_id));
+        let ret = f(self);
+        self.param_env = old_param_env;
         ret
     }
 
@@ -104,24 +102,24 @@ impl<'tcx> DocContext<'tcx> {
     /// Call the closure with the given parameters set as
     /// the substitutions for a type alias' RHS.
     crate fn enter_alias<F, R>(
-        &self,
+        &mut self,
         ty_substs: FxHashMap<DefId, clean::Type>,
         lt_substs: FxHashMap<DefId, clean::Lifetime>,
         ct_substs: FxHashMap<DefId, clean::Constant>,
         f: F,
     ) -> R
     where
-        F: FnOnce() -> R,
+        F: FnOnce(&mut Self) -> R,
     {
         let (old_tys, old_lts, old_cts) = (
-            mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
-            mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs),
-            mem::replace(&mut *self.ct_substs.borrow_mut(), ct_substs),
+            mem::replace(&mut *self.ty_substs.get_mut(), ty_substs),
+            mem::replace(&mut *self.lt_substs.get_mut(), lt_substs),
+            mem::replace(&mut *self.ct_substs.get_mut(), ct_substs),
         );
-        let r = f();
-        *self.ty_substs.borrow_mut() = old_tys;
-        *self.lt_substs.borrow_mut() = old_lts;
-        *self.ct_substs.borrow_mut() = old_cts;
+        let r = f(self);
+        *self.ty_substs.get_mut() = old_tys;
+        *self.lt_substs.get_mut() = old_lts;
+        *self.ct_substs.get_mut() = old_cts;
         r
     }
 
@@ -161,7 +159,7 @@ impl<'tcx> DocContext<'tcx> {
             }
             Entry::Occupied(e) => e.into_mut(),
         };
-        *def_index = DefIndex::from(*def_index + 1);
+        *def_index = *def_index + 1;
 
         DefId { krate: crate_num, index: *def_index }
     }
@@ -479,7 +477,7 @@ pub(crate) fn init_lints<F>(
     // NOTE: This is copy/pasted from typeck/lib.rs and should be kept in sync with those changes.
     tcx.sess.time("item_types_checking", || {
         for &module in tcx.hir().krate().modules.keys() {
-            tcx.ensure().check_mod_item_types(tcx.hir().local_def_id(module));
+            tcx.ensure().check_mod_item_types(module);
         }
     });
     tcx.sess.abort_if_errors();
@@ -488,8 +486,7 @@ pub(crate) fn init_lints<F>(
     });
     tcx.sess.time("check_mod_attrs", || {
         for &module in tcx.hir().krate().modules.keys() {
-            let local_def_id = tcx.hir().local_def_id(module);
-            tcx.ensure().check_mod_attrs(local_def_id);
+            tcx.ensure().check_mod_attrs(module);
         }
     });
 
@@ -511,7 +508,7 @@ pub(crate) fn init_lints<F>(
     let mut ctxt = DocContext {
         tcx,
         resolver,
-        param_env: Cell::new(ParamEnv::empty()),
+        param_env: ParamEnv::empty(),
         external_traits: Default::default(),
         active_extern_traits: Default::default(),
         renderinfo: RefCell::new(renderinfo),
@@ -531,6 +528,16 @@ pub(crate) fn init_lints<F>(
         module_trait_cache: RefCell::new(FxHashMap::default()),
         cache: Cache::default(),
     };
+
+    // Small hack to force the Sized trait to be present.
+    //
+    // Note that in case of `#![no_core]`, the trait is not available.
+    if let Some(sized_trait_did) = ctxt.tcx.lang_items().sized_trait() {
+        let mut sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
+        sized_trait.is_auto = true;
+        ctxt.external_traits.borrow_mut().insert(sized_trait_did, sized_trait);
+    }
+
     debug!("crate: {:?}", tcx.hir().krate());
 
     let mut krate = tcx.sess.time("clean_crate", || clean::krate(&mut ctxt));
@@ -628,7 +635,7 @@ fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler) {
         };
         if run {
             debug!("running pass {}", p.pass.name);
-            krate = ctxt.tcx.sess.time(p.pass.name, || (p.pass.run)(krate, &ctxt));
+            krate = ctxt.tcx.sess.time(p.pass.name, || (p.pass.run)(krate, &mut ctxt));
         }
     }