Skip to content

Commit 51e1724

Browse files
committed
save_analysis: better handle functions signature
1 parent 7b94fdb commit 51e1724

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed

src/librustc_hir_pretty/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ pub fn path_to_string(segment: &hir::Path<'_>) -> String {
227227
to_string(NO_ANN, |s| s.print_path(segment, false))
228228
}
229229

230+
pub fn fn_to_string(
231+
decl: &hir::FnDecl<'_>,
232+
header: hir::FnHeader,
233+
name: Option<Symbol>,
234+
generics: &hir::Generics<'_>,
235+
vis: &hir::Visibility<'_>,
236+
arg_names: &[Ident],
237+
body_id: Option<hir::BodyId>,
238+
) -> String {
239+
to_string(NO_ANN, |s| s.print_fn(decl, header, name, generics, vis, arg_names, body_id))
240+
}
241+
230242
impl<'a> State<'a> {
231243
pub fn cbox(&mut self, u: usize) {
232244
self.s.cbox(u);

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_hir as hir;
2020
use rustc_hir::def::{DefKind as HirDefKind, Res};
2121
use rustc_hir::def_id::{DefId, LocalDefId};
2222
use rustc_hir::intravisit::{self, Visitor};
23-
use rustc_hir_pretty::{bounds_to_string, generic_params_to_string, ty_to_string};
23+
use rustc_hir_pretty::{bounds_to_string, fn_to_string, generic_params_to_string, ty_to_string};
2424
use rustc_middle::hir::map::Map;
2525
use rustc_middle::span_bug;
2626
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
@@ -276,7 +276,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
276276
}
277277
v.process_generic_params(&generics, &method_data.qualname, hir_id);
278278

279-
method_data.value = crate::make_signature(&sig.decl, &generics);
279+
method_data.value =
280+
fn_to_string(sig.decl, sig.header, Some(ident.name), generics, vis, &[], None);
280281
method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt);
281282

282283
v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);

src/librustc_save_analysis/lib.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def::{CtorOf, DefKind as HirDefKind, Res};
1717
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1818
use rustc_hir::intravisit::{self, Visitor};
1919
use rustc_hir::Node;
20-
use rustc_hir_pretty::ty_to_string;
20+
use rustc_hir_pretty::{fn_to_string, ty_to_string};
2121
use rustc_middle::hir::map::Map;
2222
use rustc_middle::middle::cstore::ExternCrate;
2323
use rustc_middle::middle::privacy::AccessLevels;
@@ -135,7 +135,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
135135
let def_id = self.tcx.hir().local_def_id(item.hir_id).to_def_id();
136136
let qualname = format!("::{}", self.tcx.def_path_str(def_id));
137137
match item.kind {
138-
hir::ForeignItemKind::Fn(ref decl, _, ref generics) => {
138+
hir::ForeignItemKind::Fn(ref decl, arg_names, ref generics) => {
139139
filter!(self.span_utils, item.ident.span);
140140

141141
Some(Data::DefData(Def {
@@ -144,7 +144,23 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
144144
span: self.span_from_span(item.ident.span),
145145
name: item.ident.to_string(),
146146
qualname,
147-
value: make_signature(decl, generics),
147+
value: fn_to_string(
148+
decl,
149+
hir::FnHeader {
150+
// functions in extern block are implicitly unsafe
151+
unsafety: hir::Unsafety::Unsafe,
152+
// functions in extern block cannot be const
153+
constness: hir::Constness::NotConst,
154+
abi: self.tcx.hir().get_foreign_abi(item.hir_id),
155+
// functions in extern block cannot be async
156+
asyncness: hir::IsAsync::NotAsync,
157+
},
158+
Some(item.ident.name),
159+
generics,
160+
&item.vis,
161+
arg_names,
162+
None,
163+
),
148164
parent: None,
149165
children: vec![],
150166
decl_id: None,
@@ -191,7 +207,15 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
191207
span: self.span_from_span(item.ident.span),
192208
name: item.ident.to_string(),
193209
qualname,
194-
value: make_signature(&sig.decl, generics),
210+
value: fn_to_string(
211+
sig.decl,
212+
sig.header,
213+
Some(item.ident.name),
214+
generics,
215+
&item.vis,
216+
&[],
217+
None,
218+
),
195219
parent: None,
196220
children: vec![],
197221
decl_id: None,
@@ -848,31 +872,6 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
848872
}
849873
}
850874

851-
fn make_signature(decl: &hir::FnDecl<'_>, generics: &hir::Generics<'_>) -> String {
852-
let mut sig = "fn ".to_owned();
853-
if !generics.params.is_empty() {
854-
sig.push('<');
855-
sig.push_str(
856-
&generics
857-
.params
858-
.iter()
859-
.map(|param| param.name.ident().to_string())
860-
.collect::<Vec<_>>()
861-
.join(", "),
862-
);
863-
sig.push_str("> ");
864-
}
865-
sig.push('(');
866-
sig.push_str(&decl.inputs.iter().map(ty_to_string).collect::<Vec<_>>().join(", "));
867-
sig.push(')');
868-
match decl.output {
869-
hir::FnRetTy::DefaultReturn(_) => sig.push_str(" -> ()"),
870-
hir::FnRetTy::Return(ref t) => sig.push_str(&format!(" -> {}", ty_to_string(t))),
871-
}
872-
873-
sig
874-
}
875-
876875
// An AST visitor for collecting paths (e.g., the names of structs) and formal
877876
// variables (idents) from patterns.
878877
struct PathCollector<'l> {

src/librustc_save_analysis/sig.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,11 @@ impl<'hir> Sig for hir::Ty<'hir> {
324324
let text = format!("[{}; {}]", nested_ty.text, expr);
325325
Ok(replace_text(nested_ty, text))
326326
}
327-
hir::TyKind::Typeof(_)
328-
| hir::TyKind::Infer
329-
| hir::TyKind::Def(..)
330-
| hir::TyKind::Err => Err("Ty"),
327+
hir::TyKind::Def(item_id, _) => {
328+
let item = scx.tcx.hir().item(item_id.id);
329+
item.make(offset, Some(item_id.id), scx)
330+
}
331+
hir::TyKind::Typeof(_) | hir::TyKind::Infer | hir::TyKind::Err => Err("Ty"),
331332
}
332333
}
333334
}

0 commit comments

Comments
 (0)