Skip to content

Commit 99dc249

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 d8ed1b0 commit 99dc249

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

src/librustc_metadata/rmeta/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1339,13 +1339,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13391339
}
13401340
}
13411341

1342-
fn get_fn_param_names(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [Symbol] {
1342+
fn get_fn_param_names(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [Ident] {
13431343
let param_names = match self.kind(id) {
13441344
EntryKind::Fn(data) | EntryKind::ForeignFn(data) => data.decode(self).param_names,
13451345
EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names,
13461346
_ => Lazy::empty(),
13471347
};
1348-
tcx.arena.alloc_from_iter(param_names.decode(self))
1348+
tcx.arena.alloc_from_iter(param_names.decode((self, tcx)))
13491349
}
13501350

13511351
fn exported_symbols(

src/librustc_metadata/rmeta/encoder.rs

+5-11
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, UseSpecializedEncodable};
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;
@@ -1004,18 +1004,12 @@ impl EncodeContext<'tcx> {
10041004
}
10051005
}
10061006

1007-
fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[Symbol]> {
1008-
self.tcx.dep_graph.with_ignore(|| {
1009-
let body = self.tcx.hir().body(body_id);
1010-
self.lazy(body.params.iter().map(|arg| match arg.pat.kind {
1011-
hir::PatKind::Binding(_, _, ident, _) => ident.name,
1012-
_ => kw::Invalid,
1013-
}))
1014-
})
1007+
fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[Ident]> {
1008+
self.tcx.dep_graph.with_ignore(|| self.lazy(self.tcx.hir().body_param_names(body_id)))
10151009
}
10161010

1017-
fn encode_fn_param_names(&mut self, param_names: &[Ident]) -> Lazy<[Symbol]> {
1018-
self.lazy(param_names.iter().map(|ident| ident.name))
1011+
fn encode_fn_param_names(&mut self, param_names: &[Ident]) -> Lazy<[Ident]> {
1012+
self.lazy(param_names.iter())
10191013
}
10201014

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

src/librustc_metadata/rmeta/mod.rs

+2-2
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

+8-1
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

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

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

src/librustc_middle/hir/mod.rs

+16-4
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ rustc_queries! {
729729
}
730730

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

0 commit comments

Comments
 (0)