Skip to content

Commit d5814b0

Browse files
authored
Auto merge of #37908 - nrc:save-def, r=eddyb
save-analysis: fix ICE on partially resolved path Occurs when we produce save-analysis before type checking is complete (due to errors).
2 parents d515586 + b1f86fb commit d5814b0

File tree

8 files changed

+571
-6
lines changed

8 files changed

+571
-6
lines changed

src/librustc_save_analysis/dump_visitor.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
276276
fn lookup_def_id(&self, ref_id: NodeId) -> Option<DefId> {
277277
self.tcx.expect_def_or_none(ref_id).and_then(|def| {
278278
match def {
279-
Def::PrimTy(..) | Def::SelfTy(..) => None,
279+
Def::Label(..) | Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => None,
280280
def => Some(def.def_id()),
281281
}
282282
})
@@ -358,7 +358,10 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
358358
collector.visit_pat(&arg.pat);
359359
let span_utils = self.span.clone();
360360
for &(id, ref p, ..) in &collector.collected_paths {
361-
let typ = self.tcx.tables().node_types.get(&id).unwrap().to_string();
361+
let typ = match self.tcx.tables().node_types.get(&id) {
362+
Some(s) => s.to_string(),
363+
None => continue,
364+
};
362365
// get the span only for the name of the variable (I hope the path is only ever a
363366
// variable name, but who knows?)
364367
let sub_span = span_utils.span_for_last_ident(p.span);
@@ -988,7 +991,13 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
988991
match p.node {
989992
PatKind::Struct(ref path, ref fields, _) => {
990993
visit::walk_path(self, path);
991-
let adt = self.tcx.tables().node_id_to_type(p.id).ty_adt_def().unwrap();
994+
let adt = match self.tcx.tables().node_id_to_type_opt(p.id) {
995+
Some(ty) => ty.ty_adt_def().unwrap(),
996+
None => {
997+
visit::walk_pat(self, p);
998+
return;
999+
}
1000+
};
9921001
let variant = adt.variant_of_def(self.tcx.expect_def(p.id));
9931002

9941003
for &Spanned { node: ref field, span } in fields {
@@ -1354,7 +1363,13 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
13541363
}
13551364
ast::ExprKind::Struct(ref path, ref fields, ref base) => {
13561365
let hir_expr = self.save_ctxt.tcx.map.expect_expr(ex.id);
1357-
let adt = self.tcx.tables().expr_ty(&hir_expr).ty_adt_def().unwrap();
1366+
let adt = match self.tcx.tables().expr_ty_opt(&hir_expr) {
1367+
Some(ty) => ty.ty_adt_def().unwrap(),
1368+
None => {
1369+
visit::walk_expr(self, ex);
1370+
return;
1371+
}
1372+
};
13581373
let def = self.tcx.expect_def(hir_expr.id);
13591374
self.process_struct_lit(ex, path, fields, adt.variant_of_def(def), base)
13601375
}
@@ -1380,7 +1395,13 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor for DumpVisitor<'l, 'tcx, 'll, D>
13801395
return;
13811396
}
13821397
};
1383-
let ty = &self.tcx.tables().expr_ty_adjusted(&hir_node).sty;
1398+
let ty = match self.tcx.tables().expr_ty_adjusted_opt(&hir_node) {
1399+
Some(ty) => &ty.sty,
1400+
None => {
1401+
visit::walk_expr(self, ex);
1402+
return;
1403+
}
1404+
};
13841405
match *ty {
13851406
ty::TyAdt(def, _) => {
13861407
let sub_span = self.span.sub_span_after_token(ex.span, token::Dot);

src/librustc_save_analysis/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
498498
}
499499

500500
pub fn get_path_data(&self, id: NodeId, path: &ast::Path) -> Option<Data> {
501-
let def = self.tcx.expect_def(id);
501+
let resolution = self.tcx.expect_resolution(id);
502+
if resolution.depth != 0 {
503+
return None;
504+
}
505+
let def = resolution.base_def;
506+
502507
let sub_span = self.span_utils.span_for_last_ident(path.span);
503508
filter!(self.span_utils, sub_span, path.span, None);
504509
match def {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-include ../tools.mk
2+
all: code
3+
krate2: krate2.rs
4+
$(RUSTC) $<
5+
code: foo.rs krate2
6+
$(RUSTC) foo.rs -Zsave-analysis || exit 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// sub-module in the same directory as the main crate file
12+
13+
pub struct SameStruct {
14+
pub name: String
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn hello(x: isize) {
12+
println!("macro {} :-(", x);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// sub-module in a sub-directory
12+
13+
use sub::sub2 as msalias;
14+
use sub::sub2;
15+
16+
static yy: usize = 25;
17+
18+
mod sub {
19+
pub mod sub2 {
20+
pub mod sub3 {
21+
pub fn hello() {
22+
println!("hello from module 3");
23+
}
24+
}
25+
pub fn hello() {
26+
println!("hello from a module");
27+
}
28+
29+
pub struct nested_struct {
30+
pub field2: u32,
31+
}
32+
}
33+
}
34+
35+
pub struct SubStruct {
36+
pub name: String
37+
}

0 commit comments

Comments
 (0)