Skip to content

save_analysis: various fixes #73071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4257,6 +4257,7 @@ dependencies = [
"rustc_parse",
"rustc_session",
"rustc_span",
"rustc_target",
"serde_json",
]

Expand Down
14 changes: 13 additions & 1 deletion src/librustc_hir_pretty/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ pub fn path_to_string(segment: &hir::Path<'_>) -> String {
to_string(NO_ANN, |s| s.print_path(segment, false))
}

pub fn fn_to_string(
decl: &hir::FnDecl<'_>,
header: hir::FnHeader,
name: Option<Symbol>,
generics: &hir::Generics<'_>,
vis: &hir::Visibility<'_>,
arg_names: &[Ident],
body_id: Option<hir::BodyId>,
) -> String {
to_string(NO_ANN, |s| s.print_fn(decl, header, name, generics, vis, arg_names, body_id))
}

impl<'a> State<'a> {
pub fn cbox(&mut self, u: usize) {
self.s.cbox(u);
Expand Down Expand Up @@ -385,7 +397,7 @@ impl<'a> State<'a> {
&f.param_names[..],
);
}
hir::TyKind::Def(..) => {}
hir::TyKind::Def(item, _) => self.ann.nested(self, Nested::Item(item)),
hir::TyKind::Path(ref qpath) => self.print_qpath(qpath, false),
hir::TyKind::TraitObject(bounds, ref lifetime) => {
let mut first = true;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_save_analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ rustc_parse = { path = "../librustc_parse" }
serde_json = "1"
rustc_session = { path = "../librustc_session" }
rustc_span = { path = "../librustc_span" }
rustc_target = { path = "../librustc_target" }
rls-data = "0.19"
rls-span = "0.5"
5 changes: 3 additions & 2 deletions src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind as HirDefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir_pretty::{bounds_to_string, generic_params_to_string, ty_to_string};
use rustc_hir_pretty::{bounds_to_string, fn_to_string, generic_params_to_string, ty_to_string};
use rustc_middle::hir::map::Map;
use rustc_middle::span_bug;
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
Expand Down Expand Up @@ -276,7 +276,8 @@ impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
}
v.process_generic_params(&generics, &method_data.qualname, hir_id);

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

v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);
Expand Down
55 changes: 26 additions & 29 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_hir::def::{CtorOf, DefKind as HirDefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::Node;
use rustc_hir_pretty::ty_to_string;
use rustc_hir_pretty::{fn_to_string, ty_to_string};
use rustc_middle::hir::map::Map;
use rustc_middle::middle::cstore::ExternCrate;
use rustc_middle::middle::privacy::AccessLevels;
Expand All @@ -28,6 +28,7 @@ use rustc_session::output::{filename_for_metadata, out_filename};
use rustc_span::source_map::Spanned;
use rustc_span::symbol::Ident;
use rustc_span::*;
use rustc_target::spec::abi::Abi;

use std::cell::Cell;
use std::default::Default;
Expand Down Expand Up @@ -135,7 +136,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
let def_id = self.tcx.hir().local_def_id(item.hir_id).to_def_id();
let qualname = format!("::{}", self.tcx.def_path_str(def_id));
match item.kind {
hir::ForeignItemKind::Fn(ref decl, _, ref generics) => {
hir::ForeignItemKind::Fn(ref decl, arg_names, ref generics) => {
filter!(self.span_utils, item.ident.span);

Some(Data::DefData(Def {
Expand All @@ -144,7 +145,20 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
span: self.span_from_span(item.ident.span),
name: item.ident.to_string(),
qualname,
value: make_signature(decl, generics),
value: fn_to_string(
decl,
hir::FnHeader {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's unfortunate, together with importing rustc_target as a dependency...

I think this would be alleviated if we stored FnSig that has both FnHeader and FnDecl in ForeignItemKind::Fn and accessed that directly rather than just synthesizing the header (we lose the extern ABI information, in particular). The header is small and Copy so hopefully this doesn't impact things much...

Would such a change to HIR make sense? @RalfJung since you've already invested a bit of time in this PR series 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I know nothing about HIR.^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try then... @petrochenkov

unsafety: hir::Unsafety::Normal,
constness: hir::Constness::NotConst,
abi: Abi::Rust,
asyncness: hir::IsAsync::NotAsync,
},
Some(item.ident.name),
generics,
&item.vis,
arg_names,
None,
),
parent: None,
children: vec![],
decl_id: None,
Expand Down Expand Up @@ -191,7 +205,15 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
span: self.span_from_span(item.ident.span),
name: item.ident.to_string(),
qualname,
value: make_signature(&sig.decl, generics),
value: fn_to_string(
sig.decl,
sig.header,
Some(item.ident.name),
generics,
&item.vis,
&[],
None,
),
parent: None,
children: vec![],
decl_id: None,
Expand Down Expand Up @@ -848,31 +870,6 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
}
}

fn make_signature(decl: &hir::FnDecl<'_>, generics: &hir::Generics<'_>) -> String {
let mut sig = "fn ".to_owned();
if !generics.params.is_empty() {
sig.push('<');
sig.push_str(
&generics
.params
.iter()
.map(|param| param.name.ident().to_string())
.collect::<Vec<_>>()
.join(", "),
);
sig.push_str("> ");
}
sig.push('(');
sig.push_str(&decl.inputs.iter().map(ty_to_string).collect::<Vec<_>>().join(", "));
sig.push(')');
match decl.output {
hir::FnRetTy::DefaultReturn(_) => sig.push_str(" -> ()"),
hir::FnRetTy::Return(ref t) => sig.push_str(&format!(" -> {}", ty_to_string(t))),
}

sig
}

// An AST visitor for collecting paths (e.g., the names of structs) and formal
// variables (idents) from patterns.
struct PathCollector<'l> {
Expand Down
9 changes: 5 additions & 4 deletions src/librustc_save_analysis/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,11 @@ impl<'hir> Sig for hir::Ty<'hir> {
let text = format!("[{}; {}]", nested_ty.text, expr);
Ok(replace_text(nested_ty, text))
}
hir::TyKind::Typeof(_)
| hir::TyKind::Infer
| hir::TyKind::Def(..)
| hir::TyKind::Err => Err("Ty"),
hir::TyKind::Def(item_id, _) => {
let item = scx.tcx.hir().item(item_id.id);
item.make(offset, Some(item_id.id), scx)
}
hir::TyKind::Typeof(_) | hir::TyKind::Infer | hir::TyKind::Err => Err("Ty"),
}
}
}
Expand Down