Skip to content

Commit

Permalink
Add temprary fix for complete tuple structs
Browse files Browse the repository at this point in the history
By rust-lang/rust#49718, ExprKind::TupField
was removed. This commit includes a temorary fix and a tests for this
removal.
  • Loading branch information
kngwyu committed May 18, 2018
1 parent 9f0902b commit 60be621
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/racer/typeinf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,27 @@ fn get_type_of_for_expr(m: &Match, msrc: Src, session: &Session) -> Option<core:
}

pub fn get_struct_field_type(fieldname: &str, structmatch: &Match, session: &Session) -> Option<core::Ty> {
assert!(structmatch.mtype == core::MatchType::Struct);
// temporary fix for https://github.com/rust-lang-nursery/rls/issues/783
if structmatch.mtype != core::MatchType::Struct {
warn!("get_struct_filed_type is called for {:?}", structmatch.mtype);
return None;
}

debug!("[get_struct_filed_type]{}, {:?}", fieldname, structmatch);

let src = session.load_file(&structmatch.filepath);

let opoint = scopes::expect_stmt_start(src.as_src(), structmatch.point);
let structsrc = scopes::end_of_next_scope(&src[opoint..]);

// HACK: if scopes::end_of_next_scope returns empty struct, it's maybe tuple struct
// TODO: remove this hack
let structsrc = if structsrc == "" {
(*get_first_stmt(src.as_src().from(opoint))).to_owned()
} else {
structsrc.to_owned()
};

let fields = ast::parse_struct_fields(structsrc.to_owned(), Scope::from_match(structmatch));
for (field, _, ty) in fields {
if fieldname == field {
Expand Down
17 changes: 17 additions & 0 deletions tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4256,3 +4256,20 @@ fn main {
assert_eq!(doc_str, got.docs);
})
}

#[test]
fn completes_methods_for_tuple_struct() {
let _lock = sync!();
let src = r"
fn main() {
struct A(i32, Vec<i32>);
let mut a = A(0, vec![3, 4]);
a.1.appen~
}
";
assert!(
get_all_completions(src, None)
.into_iter()
.any(|ma| ma.matchstr == "append")
);
}

0 comments on commit 60be621

Please sign in to comment.