Skip to content

Commit 0e6df53

Browse files
committed
Rollup merge of rust-lang#48778 - sinkuu:rls_crash_tuple_struct, r=nrc
Fix save-analysis generation crash with invalid tuple access Reproduction: ```rust fn invalid_tuple_struct_accessing() { bar.0; } ``` ``` error[E0425]: cannot find value `bar` in this scope --> test.rs:2:5 | 2 | bar.0; | ^^^ not found in this scope error[E0601]: main function not found error: internal compiler error: librustc_save_analysis/dump_visitor.rs:1678: Expected struct or tuple type, found TyError --> test.rs:2:5 | 2 | bar.0; | ^^^^^ thread 'rustc' panicked at 'Box<Any>', librustc_errors/lib.rs:482:9 note: Run with `RUST_BACKTRACE=1` for a backtrace. ``` This should fix a crash in RLS when editing such code. cc @nrc
2 parents 584a28f + f5a3efe commit 0e6df53

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,17 +1665,23 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
16651665
if !self.span.filter_generated(sub_span, ex.span) {
16661666
let span =
16671667
self.span_from_span(sub_span.expect("No span found for var ref"));
1668-
let ref_id =
1669-
::id_from_def_id(def.non_enum_variant().fields[idx.node].did);
1670-
self.dumper.dump_ref(Ref {
1671-
kind: RefKind::Variable,
1672-
span,
1673-
ref_id,
1674-
});
1668+
if let Some(field) = def.non_enum_variant().fields.get(idx.node) {
1669+
let ref_id = ::id_from_def_id(field.did);
1670+
self.dumper.dump_ref(Ref {
1671+
kind: RefKind::Variable,
1672+
span,
1673+
ref_id,
1674+
});
1675+
} else {
1676+
return;
1677+
}
16751678
}
16761679
}
16771680
ty::TyTuple(..) => {}
1678-
_ => span_bug!(ex.span, "Expected struct or tuple type, found {:?}", ty),
1681+
_ => {
1682+
debug!("Expected struct or tuple type, found {:?}", ty);
1683+
return;
1684+
}
16791685
}
16801686
}
16811687
ast::ExprKind::Closure(_, _, ref decl, ref body, _fn_decl_span) => {

src/test/run-make/save-analysis-fail/foo.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,10 @@ struct Rls699 {
459459
fn new(f: u32) -> Rls699 {
460460
Rls699 { fs }
461461
}
462+
463+
fn invalid_tuple_struct_access() {
464+
bar.0;
465+
466+
struct S;
467+
S.0;
468+
}

0 commit comments

Comments
 (0)