Skip to content

Commit 99930ac

Browse files
committed
Auto merge of #95880 - cjgillot:def-ident-span, r=petrochenkov
Handle `def_ident_span` like `def_span`. `def_ident_span` had an ad-hoc status in the compiler. This PR refactors it to be a first-class citizen like `def_span`: - it gets encoded in the main metadata loop, instead of the visitor; - its implementation is updated to mirror the one of `def_span`. We do not remove the `Option` in the return type, since some items do not have an ident, AnonConsts for instance.
2 parents c08b235 + 1a881a4 commit 99930ac

File tree

14 files changed

+229
-151
lines changed

14 files changed

+229
-151
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -774,17 +774,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
774774

775775
fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option<Ident> {
776776
let name = self.opt_item_name(item_index)?;
777-
let span = match self.root.tables.def_ident_span.get(self, item_index) {
778-
Some(lazy_span) => lazy_span.decode((self, sess)),
779-
None => {
780-
// FIXME: this weird case of a name with no span is specific to `extern crate`
781-
// items, which are supposed to be treated like `use` items and only be encoded
782-
// to metadata as `Export`s, return `None` because that's what all the callers
783-
// expect in this case.
784-
assert_eq!(self.def_kind(item_index), DefKind::ExternCrate);
785-
return None;
786-
}
787-
};
777+
let span =
778+
self.root.tables.def_ident_span.get(self, item_index).unwrap().decode((self, sess));
788779
Some(Ident::new(name, span))
789780
}
790781

compiler/rustc_metadata/src/rmeta/encoder.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_serialize::{opaque, Encodable, Encoder};
3131
use rustc_session::config::CrateType;
3232
use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib};
3333
use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind};
34-
use rustc_span::symbol::{sym, Ident, Symbol};
34+
use rustc_span::symbol::{sym, Symbol};
3535
use rustc_span::{
3636
self, DebuggerVisualizerFile, ExternalSource, FileName, SourceFile, Span, SyntaxContext,
3737
};
@@ -1007,6 +1007,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10071007
record!(self.tables.def_span[def_id] <- tcx.def_span(def_id));
10081008
self.encode_attrs(local_id);
10091009
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id));
1010+
if let Some(ident_span) = tcx.def_ident_span(def_id) {
1011+
record!(self.tables.def_ident_span[def_id] <- ident_span);
1012+
}
10101013
if def_kind.has_codegen_attrs() {
10111014
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
10121015
}
@@ -1071,7 +1074,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10711074
assert!(f.did.is_local());
10721075
f.did.index
10731076
}));
1074-
self.encode_ident_span(def_id, variant.ident(tcx));
10751077
self.encode_item_type(def_id);
10761078
if variant.ctor_kind == CtorKind::Fn {
10771079
// FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
@@ -1163,7 +1165,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11631165
debug!("EncodeContext::encode_field({:?})", def_id);
11641166

11651167
record!(self.tables.kind[def_id] <- EntryKind::Field);
1166-
self.encode_ident_span(def_id, field.ident(self.tcx));
11671168
self.encode_item_type(def_id);
11681169
}
11691170

@@ -1242,7 +1243,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12421243
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
12431244
}
12441245
}
1245-
self.encode_ident_span(def_id, ast_item.ident);
12461246
match trait_item.kind {
12471247
ty::AssocKind::Const | ty::AssocKind::Fn => {
12481248
self.encode_item_type(def_id);
@@ -1306,7 +1306,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13061306
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
13071307
}
13081308
}
1309-
self.encode_ident_span(def_id, impl_item.ident(self.tcx));
13101309
self.encode_item_type(def_id);
13111310
if let Some(trait_item_def_id) = impl_item.trait_item_def_id {
13121311
self.tables.trait_item_def_id.set(def_id.index, trait_item_def_id.into());
@@ -1408,8 +1407,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14081407

14091408
debug!("EncodeContext::encode_info_for_item({:?})", def_id);
14101409

1411-
self.encode_ident_span(def_id, item.ident);
1412-
14131410
let entry_kind = match item.kind {
14141411
hir::ItemKind::Static(..) => EntryKind::Static,
14151412
hir::ItemKind::Const(_, body_id) => {
@@ -1953,7 +1950,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19531950
record!(self.tables.kind[def_id] <- EntryKind::ForeignType);
19541951
}
19551952
}
1956-
self.encode_ident_span(def_id, nitem.ident);
19571953
self.encode_item_type(def_id);
19581954
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
19591955
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
@@ -2035,10 +2031,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20352031
}
20362032
}
20372033

2038-
fn encode_ident_span(&mut self, def_id: DefId, ident: Ident) {
2039-
record!(self.tables.def_ident_span[def_id] <- ident.span);
2040-
}
2041-
20422034
/// In some cases, along with the item itself, we also
20432035
/// encode some sub-items. Usually we want some info from the item
20442036
/// so it's easier to do that here then to wait until we would encounter

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

+24-18
Original file line numberDiff line numberDiff line change
@@ -912,27 +912,33 @@ impl<'hir> Map<'hir> {
912912
}
913913
}
914914

915+
#[inline]
916+
fn opt_ident(self, id: HirId) -> Option<Ident> {
917+
match self.get(id) {
918+
Node::Binding(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident),
919+
// A `Ctor` doesn't have an identifier itself, but its parent
920+
// struct/variant does. Compare with `hir::Map::opt_span`.
921+
Node::Ctor(..) => match self.find(self.get_parent_node(id))? {
922+
Node::Item(item) => Some(item.ident),
923+
Node::Variant(variant) => Some(variant.ident),
924+
_ => unreachable!(),
925+
},
926+
node => node.ident(),
927+
}
928+
}
929+
930+
#[inline]
931+
pub(super) fn opt_ident_span(self, id: HirId) -> Option<Span> {
932+
self.opt_ident(id).map(|ident| ident.span)
933+
}
934+
935+
#[inline]
915936
pub fn opt_name(self, id: HirId) -> Option<Symbol> {
916-
Some(match self.get(id) {
917-
Node::Item(i) => i.ident.name,
918-
Node::ForeignItem(fi) => fi.ident.name,
919-
Node::ImplItem(ii) => ii.ident.name,
920-
Node::TraitItem(ti) => ti.ident.name,
921-
Node::Variant(v) => v.ident.name,
922-
Node::Field(f) => f.ident.name,
923-
Node::Lifetime(lt) => lt.name.ident().name,
924-
Node::GenericParam(param) => param.name.ident().name,
925-
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
926-
Node::Ctor(..) => self.name(HirId::make_owner(self.get_parent_item(id))),
927-
_ => return None,
928-
})
937+
self.opt_ident(id).map(|ident| ident.name)
929938
}
930939

931940
pub fn name(self, id: HirId) -> Symbol {
932-
match self.opt_name(id) {
933-
Some(name) => name,
934-
None => bug!("no name for {}", self.node_to_string(id)),
935-
}
941+
self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.node_to_string(id)))
936942
}
937943

938944
/// Given a node ID, gets a list of attributes associated with the AST
@@ -1008,7 +1014,7 @@ impl<'hir> Map<'hir> {
10081014
}
10091015

10101016
pub fn span_if_local(self, id: DefId) -> Option<Span> {
1011-
id.as_local().and_then(|id| self.opt_span(self.local_def_id_to_hir_id(id)))
1017+
if id.is_local() { Some(self.tcx.def_span(id)) } else { None }
10121018
}
10131019

10141020
pub fn res_span(self, res: Res) -> Option<Span> {

compiler/rustc_middle/src/hir/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,16 @@ pub fn provide(providers: &mut Providers) {
121121
providers.hir_attrs =
122122
|tcx, id| tcx.hir_crate(()).owners[id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs);
123123
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
124-
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
124+
providers.def_span = |tcx, def_id| {
125+
let def_id = def_id.expect_local();
126+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
127+
tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP)
128+
};
129+
providers.def_ident_span = |tcx, def_id| {
130+
let def_id = def_id.expect_local();
131+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
132+
tcx.hir().opt_ident_span(hir_id)
133+
};
125134
providers.fn_arg_names = |tcx, id| {
126135
let hir = tcx.hir();
127136
let hir_id = hir.local_def_id_to_hir_id(id.expect_local());

compiler/rustc_ty_utils/src/ty.rs

-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_middle::ty::subst::Subst;
55
use rustc_middle::ty::{
66
self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt,
77
};
8-
use rustc_span::Span;
98
use rustc_trait_selection::traits;
109

1110
fn sized_constraint_for_ty<'tcx>(
@@ -103,21 +102,6 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain
103102
ty::AdtSizedConstraint(result)
104103
}
105104

106-
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
107-
tcx.hir()
108-
.get_if_local(def_id)
109-
.and_then(|node| match node {
110-
// A `Ctor` doesn't have an identifier itself, but its parent
111-
// struct/variant does. Compare with `hir::Map::opt_span`.
112-
hir::Node::Ctor(ctor) => ctor
113-
.ctor_hir_id()
114-
.and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id)))
115-
.and_then(|parent| parent.ident()),
116-
_ => node.ident(),
117-
})
118-
.map(|ident| ident.span)
119-
}
120-
121105
/// See `ParamEnv` struct definition for details.
122106
#[instrument(level = "debug", skip(tcx))]
123107
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
@@ -480,7 +464,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
480464
*providers = ty::query::Providers {
481465
asyncness,
482466
adt_sized_constraint,
483-
def_ident_span,
484467
param_env,
485468
param_env_reveal_all_normalized,
486469
instance_def_size_estimate,

src/test/ui/closures/issue-87461.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | Ok(())
55
| -- ^^ expected `u16`, found `()`
66
| |
77
| arguments to this enum variant are incorrect
8+
|
9+
note: tuple variant defined here
10+
--> $SRC_DIR/core/src/result.rs:LL:COL
11+
|
12+
LL | Ok(#[stable(feature = "rust1", since = "1.0.0")] T),
13+
| ^^
814

915
error[E0308]: mismatched types
1016
--> $DIR/issue-87461.rs:17:8
@@ -13,6 +19,12 @@ LL | Ok(())
1319
| -- ^^ expected `u16`, found `()`
1420
| |
1521
| arguments to this enum variant are incorrect
22+
|
23+
note: tuple variant defined here
24+
--> $SRC_DIR/core/src/result.rs:LL:COL
25+
|
26+
LL | Ok(#[stable(feature = "rust1", since = "1.0.0")] T),
27+
| ^^
1628

1729
error[E0308]: mismatched types
1830
--> $DIR/issue-87461.rs:26:12
@@ -21,6 +33,12 @@ LL | Ok(())
2133
| -- ^^ expected `u16`, found `()`
2234
| |
2335
| arguments to this enum variant are incorrect
36+
|
37+
note: tuple variant defined here
38+
--> $SRC_DIR/core/src/result.rs:LL:COL
39+
|
40+
LL | Ok(#[stable(feature = "rust1", since = "1.0.0")] T),
41+
| ^^
2442

2543
error: aborting due to 3 previous errors
2644

src/test/ui/const-generics/const-argument-cross-crate-mismatch.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ LL | let _ = const_generic_lib::function(const_generic_lib::Struct([0u8, 1u8
55
| ------------------------- ^^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
66
| |
77
| arguments to this struct are incorrect
8+
|
9+
note: tuple struct defined here
10+
--> $DIR/auxiliary/const_generic_lib.rs:1:12
11+
|
12+
LL | pub struct Struct<const N: usize>(pub [u8; N]);
13+
| ^^^^^^
814

915
error[E0308]: mismatched types
1016
--> $DIR/const-argument-cross-crate-mismatch.rs:8:65
@@ -13,6 +19,12 @@ LL | let _: const_generic_lib::Alias = const_generic_lib::Struct([0u8, 1u8,
1319
| ------------------------- ^^^^^^^^^^^^^^^ expected an array with a fixed size of 2 elements, found one with 3 elements
1420
| |
1521
| arguments to this struct are incorrect
22+
|
23+
note: tuple struct defined here
24+
--> $DIR/auxiliary/const_generic_lib.rs:1:12
25+
|
26+
LL | pub struct Struct<const N: usize>(pub [u8; N]);
27+
| ^^^^^^
1628

1729
error: aborting due to 2 previous errors
1830

src/test/ui/mismatched_types/issue-35030.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ LL | Some(true)
1111
|
1212
= note: expected type parameter `bool` (type parameter `bool`)
1313
found type `bool` (`bool`)
14+
note: tuple variant defined here
15+
--> $SRC_DIR/core/src/option.rs:LL:COL
16+
|
17+
LL | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
18+
| ^^^^
1419

1520
error: aborting due to previous error
1621

0 commit comments

Comments
 (0)