Skip to content

Commit 754da88

Browse files
committed
Make fn_arg_names return Ident instead of symbol
Also, implement this query for the local crate, not just foreign crates.
1 parent a37c32e commit 754da88

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,13 +1317,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13171317
}
13181318
}
13191319

1320-
fn get_fn_param_names(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [Symbol] {
1320+
fn get_fn_param_names(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [Ident] {
13211321
let param_names = match self.kind(id) {
13221322
EntryKind::Fn(data) | EntryKind::ForeignFn(data) => data.decode(self).param_names,
13231323
EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names,
13241324
_ => Lazy::empty(),
13251325
};
1326-
tcx.arena.alloc_from_iter(param_names.decode(self))
1326+
tcx.arena.alloc_from_iter(param_names.decode((self, tcx)))
13271327
}
13281328

13291329
fn exported_symbols(

src/librustc_metadata/rmeta/encoder.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
3030
use rustc_serialize::{opaque, Encodable, Encoder, SpecializedEncoder};
3131
use rustc_session::config::CrateType;
3232
use rustc_span::source_map::Spanned;
33-
use rustc_span::symbol::{kw, sym, Ident, Symbol};
33+
use rustc_span::symbol::{sym, Ident, Symbol};
3434
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span};
3535
use rustc_target::abi::VariantIdx;
3636
use std::hash::Hash;
@@ -997,18 +997,12 @@ impl EncodeContext<'tcx> {
997997
}
998998
}
999999

1000-
fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[Symbol]> {
1001-
self.tcx.dep_graph.with_ignore(|| {
1002-
let body = self.tcx.hir().body(body_id);
1003-
self.lazy(body.params.iter().map(|arg| match arg.pat.kind {
1004-
hir::PatKind::Binding(_, _, ident, _) => ident.name,
1005-
_ => kw::Invalid,
1006-
}))
1007-
})
1000+
fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[Ident]> {
1001+
self.tcx.dep_graph.with_ignore(|| self.lazy(self.tcx.hir().body_param_names(body_id)))
10081002
}
10091003

1010-
fn encode_fn_param_names(&mut self, param_names: &[Ident]) -> Lazy<[Symbol]> {
1011-
self.lazy(param_names.iter().map(|ident| ident.name))
1004+
fn encode_fn_param_names(&mut self, param_names: &[Ident]) -> Lazy<[Ident]> {
1005+
self.lazy(param_names.iter())
10121006
}
10131007

10141008
fn encode_optimized_mir(&mut self, def_id: LocalDefId) {

src/librustc_metadata/rmeta/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_serialize::opaque::Encoder;
1919
use rustc_session::config::SymbolManglingVersion;
2020
use rustc_session::CrateDisambiguator;
2121
use rustc_span::edition::Edition;
22-
use rustc_span::symbol::Symbol;
22+
use rustc_span::symbol::{Ident, Symbol};
2323
use rustc_span::{self, Span};
2424
use rustc_target::spec::{PanicStrategy, TargetTriple};
2525

@@ -327,7 +327,7 @@ struct ModData {
327327
struct FnData {
328328
asyncness: hir::IsAsync,
329329
constness: hir::Constness,
330-
param_names: Lazy<[Symbol]>,
330+
param_names: Lazy<[Ident]>,
331331
}
332332

333333
#[derive(RustcEncodable, RustcDecodable)]

src/librustc_middle/hir/map/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir::*;
1414
use rustc_index::vec::IndexVec;
1515
use rustc_span::hygiene::MacroKind;
1616
use rustc_span::source_map::Spanned;
17-
use rustc_span::symbol::{kw, Symbol};
17+
use rustc_span::symbol::{kw, Ident, Symbol};
1818
use rustc_span::Span;
1919
use rustc_target::spec::abi::Abi;
2020

@@ -375,6 +375,13 @@ impl<'hir> Map<'hir> {
375375
})
376376
}
377377

378+
pub fn body_param_names(&self, id: BodyId) -> impl Iterator<Item = Ident> + 'hir {
379+
self.body(id).params.iter().map(|arg| match arg.pat.kind {
380+
PatKind::Binding(_, _, ident, _) => ident,
381+
_ => Ident::new(kw::Invalid, rustc_span::DUMMY_SP),
382+
})
383+
}
384+
378385
/// Returns the `BodyOwnerKind` of this `LocalDefId`.
379386
///
380387
/// Panics if `LocalDefId` does not have an associated body.

src/librustc_middle/hir/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ use rustc_data_structures::fingerprint::Fingerprint;
1212
use rustc_data_structures::fx::FxHashMap;
1313
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1414
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
15-
use rustc_hir::Body;
16-
use rustc_hir::HirId;
17-
use rustc_hir::ItemLocalId;
18-
use rustc_hir::Node;
15+
use rustc_hir::*;
1916
use rustc_index::vec::IndexVec;
2017

2118
pub struct Owner<'tcx> {
@@ -79,5 +76,20 @@ pub fn provide(providers: &mut Providers<'_>) {
7976
};
8077
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
8178
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();
79+
providers.fn_arg_names = |tcx, id| {
80+
let hir = tcx.hir();
81+
let hir_id = hir.as_local_hir_id(id.expect_local());
82+
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
83+
tcx.arena.alloc_from_iter(hir.body_param_names(body_id))
84+
} else if let Node::TraitItem(&TraitItem {
85+
kind: TraitItemKind::Fn(_, TraitFn::Required(idents)),
86+
..
87+
}) = hir.get(hir_id)
88+
{
89+
tcx.arena.alloc_slice(idents)
90+
} else {
91+
span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id);
92+
}
93+
};
8294
map::provide(providers);
8395
}

src/librustc_middle/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ rustc_queries! {
700700
}
701701

702702
Other {
703-
query fn_arg_names(def_id: DefId) -> &'tcx [Symbol] {
703+
query fn_arg_names(def_id: DefId) -> &'tcx [rustc_span::symbol::Ident] {
704704
desc { |tcx| "looking up function parameter names for `{}`", tcx.def_path_str(def_id) }
705705
}
706706
/// Gets the rendered value of the specified constant or associated constant.

0 commit comments

Comments
 (0)