Skip to content

Commit 2a716f3

Browse files
committed
resolve: Centralize retrieval of items span and item name
1 parent c7f424b commit 2a716f3

File tree

5 files changed

+37
-69
lines changed

5 files changed

+37
-69
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
130130
};
131131

132132
let expn_id = self.cstore().module_expansion_untracked(def_id, &self.tcx.sess);
133-
let span = self.cstore().get_span_untracked(def_id, &self.tcx.sess);
134133
Some(self.new_module(
135134
parent,
136135
ModuleKind::Def(def_kind, def_id, name),
137136
expn_id,
138-
span,
137+
self.def_span(def_id),
139138
// FIXME: Account for `#[no_implicit_prelude]` attributes.
140139
parent.map_or(false, |module| module.no_implicit_prelude),
141140
))

compiler/rustc_resolve/src/diagnostics.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_errors::{struct_span_err, SuggestionStyle};
1212
use rustc_feature::BUILTIN_ATTRIBUTES;
1313
use rustc_hir::def::Namespace::{self, *};
1414
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
15-
use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
15+
use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
1616
use rustc_hir::PrimTy;
1717
use rustc_middle::bug;
1818
use rustc_middle::ty::TyCtxt;
@@ -555,25 +555,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
555555
return err;
556556
}
557557
Res::SelfTyAlias { alias_to: def_id, .. } => {
558-
if let Some(impl_span) = self.opt_span(def_id) {
559-
err.span_label(
560-
reduce_impl_span_to_impl_keyword(sm, impl_span),
561-
"`Self` type implicitly declared here, by this `impl`",
562-
);
563-
}
558+
err.span_label(
559+
reduce_impl_span_to_impl_keyword(sm, self.def_span(def_id)),
560+
"`Self` type implicitly declared here, by this `impl`",
561+
);
564562
err.span_label(span, "use a type here instead");
565563
return err;
566564
}
567565
Res::Def(DefKind::TyParam, def_id) => {
568-
if let Some(span) = self.opt_span(def_id) {
569-
err.span_label(span, "type parameter from outer function");
570-
}
566+
err.span_label(self.def_span(def_id), "type parameter from outer function");
571567
def_id
572568
}
573569
Res::Def(DefKind::ConstParam, def_id) => {
574-
if let Some(span) = self.opt_span(def_id) {
575-
err.span_label(span, "const parameter from outer function");
576-
}
570+
err.span_label(
571+
self.def_span(def_id),
572+
"const parameter from outer function",
573+
);
577574
def_id
578575
}
579576
_ => {
@@ -589,7 +586,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
589586
// Try to retrieve the span of the function signature and generate a new
590587
// message with a local type or const parameter.
591588
let sugg_msg = "try using a local generic parameter instead";
592-
let name = self.opt_name(def_id).unwrap_or(sym::T);
589+
let name = self.tcx.item_name(def_id);
593590
let (span, snippet) = if span.is_empty() {
594591
let snippet = format!("<{}>", name);
595592
(span, snippet)
@@ -1369,8 +1366,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13691366
}
13701367
if ident.name == kw::Default
13711368
&& let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind
1372-
&& let Some(span) = self.opt_span(def_id)
13731369
{
1370+
let span = self.def_span(def_id);
13741371
let source_map = self.tcx.sess.source_map();
13751372
let head_span = source_map.guess_head_span(span);
13761373
if let Ok(head) = source_map.span_to_snippet(head_span) {
@@ -1446,11 +1443,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14461443
Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
14471444
Some(suggestion) => suggestion,
14481445
};
1449-
let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate {
1450-
LOCAL_CRATE => self.opt_span(def_id),
1451-
_ => Some(self.cstore().get_span_untracked(def_id, self.tcx.sess)),
1452-
});
1453-
if let Some(def_span) = def_span {
1446+
if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) {
14541447
if span.overlaps(def_span) {
14551448
// Don't suggest typo suggestion for itself like in the following:
14561449
// error[E0423]: expected function, tuple struct or tuple variant, found struct `X`

compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3376,7 +3376,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
33763376
participle: "defined",
33773377
article: res.article(),
33783378
shadowed_binding: res,
3379-
shadowed_binding_span: self.r.opt_span(def_id).expect("const parameter defined outside of local crate"),
3379+
shadowed_binding_span: self.r.def_span(def_id),
33803380
}
33813381
);
33823382
None

compiler/rustc_resolve/src/late/diagnostics.rs

+16-31
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_errors::{
1919
use rustc_hir as hir;
2020
use rustc_hir::def::Namespace::{self, *};
2121
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind};
22-
use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
22+
use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
2323
use rustc_hir::PrimTy;
2424
use rustc_session::lint;
2525
use rustc_session::parse::feature_err;
@@ -166,13 +166,6 @@ impl TypoCandidate {
166166
}
167167

168168
impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
169-
fn def_span(&self, def_id: DefId) -> Option<Span> {
170-
match def_id.krate {
171-
LOCAL_CRATE => self.r.opt_span(def_id),
172-
_ => Some(self.r.cstore().get_span_untracked(def_id, self.r.tcx.sess)),
173-
}
174-
}
175-
176169
fn make_base_error(
177170
&mut self,
178171
path: &[Segment],
@@ -191,7 +184,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
191184
span,
192185
span_label: match res {
193186
Res::Def(kind, def_id) if kind == DefKind::TyParam => {
194-
self.def_span(def_id).map(|span| (span, "found this type parameter"))
187+
Some((self.r.def_span(def_id), "found this type parameter"))
195188
}
196189
_ => None,
197190
},
@@ -1295,9 +1288,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
12951288
}
12961289
PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
12971290
let span = find_span(&source, err);
1298-
if let Some(span) = self.def_span(def_id) {
1299-
err.span_label(span, &format!("`{}` defined here", path_str));
1300-
}
1291+
err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here"));
13011292
let (tail, descr, applicability) = match source {
13021293
PathSource::Pat | PathSource::TupleStruct(..) => {
13031294
("", "pattern", Applicability::MachineApplicable)
@@ -1359,17 +1350,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
13591350
if self.r.tcx.sess.is_nightly_build() {
13601351
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
13611352
`type` alias";
1362-
if let Some(span) = self.def_span(def_id) {
1363-
if let Ok(snip) = self.r.tcx.sess.source_map().span_to_snippet(span) {
1364-
// The span contains a type alias so we should be able to
1365-
// replace `type` with `trait`.
1366-
let snip = snip.replacen("type", "trait", 1);
1367-
err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
1368-
} else {
1369-
err.span_help(span, msg);
1370-
}
1353+
let span = self.r.def_span(def_id);
1354+
if let Ok(snip) = self.r.tcx.sess.source_map().span_to_snippet(span) {
1355+
// The span contains a type alias so we should be able to
1356+
// replace `type` with `trait`.
1357+
let snip = snip.replacen("type", "trait", 1);
1358+
err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect);
13711359
} else {
1372-
err.help(msg);
1360+
err.span_help(span, msg);
13731361
}
13741362
}
13751363
}
@@ -1512,9 +1500,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
15121500
match source {
15131501
PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => {
15141502
let span = find_span(&source, err);
1515-
if let Some(span) = self.def_span(def_id) {
1516-
err.span_label(span, &format!("`{}` defined here", path_str));
1517-
}
1503+
err.span_label(
1504+
self.r.def_span(def_id),
1505+
&format!("`{path_str}` defined here"),
1506+
);
15181507
err.span_suggestion(
15191508
span,
15201509
"use this syntax instead",
@@ -1527,9 +1516,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
15271516
}
15281517
(Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => {
15291518
let def_id = self.r.tcx.parent(ctor_def_id);
1530-
if let Some(span) = self.def_span(def_id) {
1531-
err.span_label(span, &format!("`{}` defined here", path_str));
1532-
}
1519+
err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here"));
15331520
let fields = self.r.field_names.get(&def_id).map_or_else(
15341521
|| "/* fields */".to_string(),
15351522
|fields| vec!["_"; fields.len()].join(", "),
@@ -2093,9 +2080,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
20932080
};
20942081

20952082
if def_id.is_local() {
2096-
if let Some(span) = self.def_span(def_id) {
2097-
err.span_note(span, "the enum is defined here");
2098-
}
2083+
err.span_note(self.r.def_span(def_id), "the enum is defined here");
20992084
}
21002085
}
21012086

compiler/rustc_resolve/src/lib.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ use rustc_middle::span_bug;
4848
use rustc_middle::ty::{self, MainDefinition, RegisteredTools, TyCtxt};
4949
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
5050
use rustc_query_system::ich::StableHashingContext;
51-
use rustc_session::cstore::CrateStore;
5251
use rustc_session::lint::LintBuffer;
5352
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
5453
use rustc_span::source_map::Spanned;
@@ -1870,20 +1869,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18701869
}
18711870
}
18721871

1873-
/// Retrieves the span of the given `DefId` if `DefId` is in the local crate.
1874-
#[inline]
1875-
fn opt_span(&self, def_id: DefId) -> Option<Span> {
1876-
def_id.as_local().map(|def_id| self.tcx.source_span(def_id))
1877-
}
1878-
1879-
/// Retrieves the name of the given `DefId`.
1880-
#[inline]
1881-
fn opt_name(&self, def_id: DefId) -> Option<Symbol> {
1882-
let def_key = match def_id.as_local() {
1883-
Some(def_id) => self.tcx.definitions_untracked().def_key(def_id),
1884-
None => self.cstore().def_key(def_id),
1885-
};
1886-
def_key.get_opt_name()
1872+
/// Retrieves definition span of the given `DefId`.
1873+
fn def_span(&self, def_id: DefId) -> Span {
1874+
match def_id.as_local() {
1875+
Some(def_id) => self.tcx.source_span(def_id),
1876+
None => self.cstore().get_span_untracked(def_id, self.tcx.sess),
1877+
}
18871878
}
18881879

18891880
/// Checks if an expression refers to a function marked with

0 commit comments

Comments
 (0)