about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2020-11-28 22:07:00 +0100
committerCamille GILLOT <gillot.camille@gmail.com>2021-01-23 13:44:02 +0100
commitc58a6fa422dd7c5bcf4f9d32d096c3d42f313240 (patch)
tree1eba24390417c00b20073724ebd9243f3675cada
parent0b6c9e9f8853c30640e08088c2fae681afaa4b4f (diff)
downloadrust-c58a6fa422dd7c5bcf4f9d32d096c3d42f313240.tar.gz
rust-c58a6fa422dd7c5bcf4f9d32d096c3d42f313240.zip
Iterate DefId to encode spans.
-rw-r--r--compiler/rustc_hir/src/definitions.rs4
-rw-r--r--compiler/rustc_metadata/src/rmeta/encoder.rs28
-rw-r--r--compiler/rustc_middle/src/hir/map/mod.rs4
-rw-r--r--src/test/ui/lint/lint-const-item-mutation.stderr10
4 files changed, 25 insertions, 21 deletions
diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs
index d5ade86593e..6a1b9bdbb94 100644
--- a/compiler/rustc_hir/src/definitions.rs
+++ b/compiler/rustc_hir/src/definitions.rs
@@ -419,6 +419,10 @@ impl Definitions {
     pub fn add_parent_module_of_macro_def(&mut self, expn_id: ExpnId, module: DefId) {
         self.parent_modules_of_macro_defs.insert(expn_id, module);
     }
+
+    pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
+        self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
+    }
 }
 
 #[derive(Copy, Clone, PartialEq, Debug)]
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs
index 600f521318b..bc9fc00e0ec 100644
--- a/compiler/rustc_metadata/src/rmeta/encoder.rs
+++ b/compiler/rustc_metadata/src/rmeta/encoder.rs
@@ -580,6 +580,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
 
         // Encode the items.
         i = self.position();
+        self.encode_def_ids();
         self.encode_info_for_items();
         let item_bytes = self.position() - i;
 
@@ -716,6 +717,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
 }
 
 impl EncodeContext<'a, 'tcx> {
+    fn encode_def_ids(&mut self) {
+        if self.is_proc_macro {
+            return;
+        }
+        let tcx = self.tcx;
+        let hir = tcx.hir();
+        for local_id in hir.iter_local_def_id() {
+            let def_id = local_id.to_def_id();
+            record!(self.tables.span[def_id] <- tcx.def_span(def_id));
+        }
+    }
+
     fn encode_variances_of(&mut self, def_id: DefId) {
         debug!("EncodeContext::encode_variances_of({:?})", def_id);
         record!(self.tables.variances[def_id] <- &self.tcx.variances_of(def_id)[..]);
@@ -742,7 +755,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- DefKind::Variant);
         record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
         record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
         record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
@@ -783,7 +795,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- DefKind::Variant);
         record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         self.encode_stability(def_id);
         self.encode_deprecation(def_id);
         self.encode_item_type(def_id);
@@ -836,7 +847,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- DefKind::Mod);
         record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         record!(self.tables.attributes[def_id] <- attrs);
         if self.is_proc_macro {
             record!(self.tables.children[def_id] <- &[]);
@@ -868,7 +878,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- DefKind::Field);
         record!(self.tables.kind[def_id] <- EntryKind::Field);
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs);
         record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
         self.encode_ident_span(def_id, field.ident);
@@ -895,7 +904,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- DefKind::Struct);
         record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr));
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
         self.encode_stability(def_id);
         self.encode_deprecation(def_id);
@@ -1003,7 +1011,6 @@ impl EncodeContext<'a, 'tcx> {
             }
         }
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- ast_item.span);
         record!(self.tables.attributes[def_id] <- ast_item.attrs);
         self.encode_ident_span(def_id, ast_item.ident);
         self.encode_stability(def_id);
@@ -1110,7 +1117,6 @@ impl EncodeContext<'a, 'tcx> {
             }
         }
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- ast_item.span);
         record!(self.tables.attributes[def_id] <- ast_item.attrs);
         self.encode_ident_span(def_id, impl_item.ident);
         self.encode_stability(def_id);
@@ -1368,7 +1374,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- def_kind);
         record!(self.tables.kind[def_id] <- entry_kind);
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         record!(self.tables.attributes[def_id] <- item.attrs);
         record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
         // FIXME(eddyb) there should be a nicer way to do this.
@@ -1489,7 +1494,6 @@ impl EncodeContext<'a, 'tcx> {
         record!(self.tables.def_kind[def_id] <- DefKind::Macro(MacroKind::Bang));
         record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- macro_def.span);
         record!(self.tables.attributes[def_id] <- macro_def.attrs);
         self.encode_ident_span(def_id, macro_def.ident);
         self.encode_stability(def_id);
@@ -1505,7 +1509,6 @@ impl EncodeContext<'a, 'tcx> {
     ) {
         record!(self.tables.def_kind[def_id] <- def_kind);
         record!(self.tables.kind[def_id] <- kind);
-        record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
         if encode_type {
             self.encode_item_type(def_id);
         }
@@ -1533,7 +1536,6 @@ impl EncodeContext<'a, 'tcx> {
 
             _ => bug!("closure that is neither generator nor closure"),
         }
-        record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
         record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]);
         self.encode_item_type(def_id.to_def_id());
         if let ty::Closure(def_id, substs) = *ty.kind() {
@@ -1559,7 +1561,6 @@ impl EncodeContext<'a, 'tcx> {
 
         record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::AnonConst);
         record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
-        record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
         self.encode_item_type(def_id.to_def_id());
         self.encode_generics(def_id.to_def_id());
         self.encode_explicit_predicates(def_id.to_def_id());
@@ -1605,6 +1606,8 @@ impl EncodeContext<'a, 'tcx> {
             let tcx = self.tcx;
             let hir = tcx.hir();
 
+            record!(self.tables.span[LOCAL_CRATE.as_def_id()] <- hir.span(hir::CRATE_HIR_ID));
+
             let proc_macro_decls_static = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap().index;
             let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)).copied();
             let macros = self.lazy(hir.krate().proc_macros.iter().map(|p| p.owner.local_def_index));
@@ -1836,7 +1839,6 @@ impl EncodeContext<'a, 'tcx> {
             }
         }
         record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
-        record!(self.tables.span[def_id] <- nitem.span);
         record!(self.tables.attributes[def_id] <- nitem.attrs);
         self.encode_ident_span(def_id, nitem.ident);
         self.encode_stability(def_id);
diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs
index e1fcc9be664..3cc8683abb1 100644
--- a/compiler/rustc_middle/src/hir/map/mod.rs
+++ b/compiler/rustc_middle/src/hir/map/mod.rs
@@ -183,6 +183,10 @@ impl<'hir> Map<'hir> {
         self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
     }
 
+    pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
+        self.tcx.definitions.iter_local_def_id()
+    }
+
     pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
         // FIXME(eddyb) support `find` on the crate root.
         if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
diff --git a/src/test/ui/lint/lint-const-item-mutation.stderr b/src/test/ui/lint/lint-const-item-mutation.stderr
index 3973af540c8..540e076c7e4 100644
--- a/src/test/ui/lint/lint-const-item-mutation.stderr
+++ b/src/test/ui/lint/lint-const-item-mutation.stderr
@@ -109,14 +109,8 @@ LL |     VEC.push(0);
 note: mutable reference created due to call to this method
   --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
    |
-LL | /     pub fn push(&mut self, value: T) {
-LL | |         // This will panic or abort if we would allocate > isize::MAX bytes
-LL | |         // or if the length increment would overflow for zero-sized types.
-LL | |         if self.len == self.buf.capacity() {
-...  |
-LL | |         }
-LL | |     }
-   | |_____^
+LL |     pub fn push(&mut self, value: T) {
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 note: `const` item defined here
   --> $DIR/lint-const-item-mutation.rs:31:1
    |