Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c58a6fa

Browse files
committed
Iterate DefId to encode spans.
1 parent 0b6c9e9 commit c58a6fa

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ impl Definitions {
419419
pub fn add_parent_module_of_macro_def(&mut self, expn_id: ExpnId, module: DefId) {
420420
self.parent_modules_of_macro_defs.insert(expn_id, module);
421421
}
422+
423+
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
424+
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
425+
}
422426
}
423427

424428
#[derive(Copy, Clone, PartialEq, Debug)]

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
580580

581581
// Encode the items.
582582
i = self.position();
583+
self.encode_def_ids();
583584
self.encode_info_for_items();
584585
let item_bytes = self.position() - i;
585586

@@ -716,6 +717,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
716717
}
717718

718719
impl EncodeContext<'a, 'tcx> {
720+
fn encode_def_ids(&mut self) {
721+
if self.is_proc_macro {
722+
return;
723+
}
724+
let tcx = self.tcx;
725+
let hir = tcx.hir();
726+
for local_id in hir.iter_local_def_id() {
727+
let def_id = local_id.to_def_id();
728+
record!(self.tables.span[def_id] <- tcx.def_span(def_id));
729+
}
730+
}
731+
719732
fn encode_variances_of(&mut self, def_id: DefId) {
720733
debug!("EncodeContext::encode_variances_of({:?})", def_id);
721734
record!(self.tables.variances[def_id] <- &self.tcx.variances_of(def_id)[..]);
@@ -742,7 +755,6 @@ impl EncodeContext<'a, 'tcx> {
742755
record!(self.tables.def_kind[def_id] <- DefKind::Variant);
743756
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
744757
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
745-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
746758
record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
747759
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
748760
record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
@@ -783,7 +795,6 @@ impl EncodeContext<'a, 'tcx> {
783795
record!(self.tables.def_kind[def_id] <- DefKind::Variant);
784796
record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
785797
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
786-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
787798
self.encode_stability(def_id);
788799
self.encode_deprecation(def_id);
789800
self.encode_item_type(def_id);
@@ -836,7 +847,6 @@ impl EncodeContext<'a, 'tcx> {
836847
record!(self.tables.def_kind[def_id] <- DefKind::Mod);
837848
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
838849
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
839-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
840850
record!(self.tables.attributes[def_id] <- attrs);
841851
if self.is_proc_macro {
842852
record!(self.tables.children[def_id] <- &[]);
@@ -868,7 +878,6 @@ impl EncodeContext<'a, 'tcx> {
868878
record!(self.tables.def_kind[def_id] <- DefKind::Field);
869879
record!(self.tables.kind[def_id] <- EntryKind::Field);
870880
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
871-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
872881
record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs);
873882
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
874883
self.encode_ident_span(def_id, field.ident);
@@ -895,7 +904,6 @@ impl EncodeContext<'a, 'tcx> {
895904
record!(self.tables.def_kind[def_id] <- DefKind::Struct);
896905
record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr));
897906
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
898-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
899907
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
900908
self.encode_stability(def_id);
901909
self.encode_deprecation(def_id);
@@ -1003,7 +1011,6 @@ impl EncodeContext<'a, 'tcx> {
10031011
}
10041012
}
10051013
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
1006-
record!(self.tables.span[def_id] <- ast_item.span);
10071014
record!(self.tables.attributes[def_id] <- ast_item.attrs);
10081015
self.encode_ident_span(def_id, ast_item.ident);
10091016
self.encode_stability(def_id);
@@ -1110,7 +1117,6 @@ impl EncodeContext<'a, 'tcx> {
11101117
}
11111118
}
11121119
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
1113-
record!(self.tables.span[def_id] <- ast_item.span);
11141120
record!(self.tables.attributes[def_id] <- ast_item.attrs);
11151121
self.encode_ident_span(def_id, impl_item.ident);
11161122
self.encode_stability(def_id);
@@ -1368,7 +1374,6 @@ impl EncodeContext<'a, 'tcx> {
13681374
record!(self.tables.def_kind[def_id] <- def_kind);
13691375
record!(self.tables.kind[def_id] <- entry_kind);
13701376
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
1371-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
13721377
record!(self.tables.attributes[def_id] <- item.attrs);
13731378
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
13741379
// FIXME(eddyb) there should be a nicer way to do this.
@@ -1489,7 +1494,6 @@ impl EncodeContext<'a, 'tcx> {
14891494
record!(self.tables.def_kind[def_id] <- DefKind::Macro(MacroKind::Bang));
14901495
record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
14911496
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
1492-
record!(self.tables.span[def_id] <- macro_def.span);
14931497
record!(self.tables.attributes[def_id] <- macro_def.attrs);
14941498
self.encode_ident_span(def_id, macro_def.ident);
14951499
self.encode_stability(def_id);
@@ -1505,7 +1509,6 @@ impl EncodeContext<'a, 'tcx> {
15051509
) {
15061510
record!(self.tables.def_kind[def_id] <- def_kind);
15071511
record!(self.tables.kind[def_id] <- kind);
1508-
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
15091512
if encode_type {
15101513
self.encode_item_type(def_id);
15111514
}
@@ -1533,7 +1536,6 @@ impl EncodeContext<'a, 'tcx> {
15331536

15341537
_ => bug!("closure that is neither generator nor closure"),
15351538
}
1536-
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
15371539
record!(self.tables.attributes[def_id.to_def_id()] <- &self.tcx.get_attrs(def_id.to_def_id())[..]);
15381540
self.encode_item_type(def_id.to_def_id());
15391541
if let ty::Closure(def_id, substs) = *ty.kind() {
@@ -1559,7 +1561,6 @@ impl EncodeContext<'a, 'tcx> {
15591561

15601562
record!(self.tables.def_kind[def_id.to_def_id()] <- DefKind::AnonConst);
15611563
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
1562-
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
15631564
self.encode_item_type(def_id.to_def_id());
15641565
self.encode_generics(def_id.to_def_id());
15651566
self.encode_explicit_predicates(def_id.to_def_id());
@@ -1605,6 +1606,8 @@ impl EncodeContext<'a, 'tcx> {
16051606
let tcx = self.tcx;
16061607
let hir = tcx.hir();
16071608

1609+
record!(self.tables.span[LOCAL_CRATE.as_def_id()] <- hir.span(hir::CRATE_HIR_ID));
1610+
16081611
let proc_macro_decls_static = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap().index;
16091612
let stability = tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)).copied();
16101613
let macros = self.lazy(hir.krate().proc_macros.iter().map(|p| p.owner.local_def_index));
@@ -1836,7 +1839,6 @@ impl EncodeContext<'a, 'tcx> {
18361839
}
18371840
}
18381841
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
1839-
record!(self.tables.span[def_id] <- nitem.span);
18401842
record!(self.tables.attributes[def_id] <- nitem.attrs);
18411843
self.encode_ident_span(def_id, nitem.ident);
18421844
self.encode_stability(def_id);

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ impl<'hir> Map<'hir> {
183183
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
184184
}
185185

186+
pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
187+
self.tcx.definitions.iter_local_def_id()
188+
}
189+
186190
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
187191
// FIXME(eddyb) support `find` on the crate root.
188192
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {

src/test/ui/lint/lint-const-item-mutation.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,8 @@ LL | VEC.push(0);
109109
note: mutable reference created due to call to this method
110110
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
111111
|
112-
LL | / pub fn push(&mut self, value: T) {
113-
LL | | // This will panic or abort if we would allocate > isize::MAX bytes
114-
LL | | // or if the length increment would overflow for zero-sized types.
115-
LL | | if self.len == self.buf.capacity() {
116-
... |
117-
LL | | }
118-
LL | | }
119-
| |_____^
112+
LL | pub fn push(&mut self, value: T) {
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120114
note: `const` item defined here
121115
--> $DIR/lint-const-item-mutation.rs:31:1
122116
|

0 commit comments

Comments
 (0)