Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
11 changes: 10 additions & 1 deletion src/racer/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,16 @@ impl<'c, 's, 'ast> visit::Visitor<'ast> for ExprTypeVisitor<'c, 's> {
.and_then(|ty| path_to_match(ty, self.session))
}
MatchType::Method(ref gen) => {
typeinf::get_return_type_of_function(&m, &m, self.session).and_then(
let mut return_ty = typeinf::get_return_type_of_function(&m, &m, self.session);
// Account for already resolved generics if the return type is Self
// (in which case we return bare type as found in the `impl` header)
if let (Some(Ty::Match(ref mut m)), Some(gen)) = (&mut return_ty, gen) {
let resolved = gen.args().filter_map(|tp| tp.resolved());
for (l, r) in m.generics_mut().zip(resolved) {
l.resolve(r.clone());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This looks like a hack but I encountered a similar resolve/substitution code in other places, so it seems it's not as out of place as it looks at a first glance.

There may be other designs how to tackle this but decided for it because it's least invasive for now.

}
}
return_ty.and_then(
|ty| {
path_to_match_including_generics(
ty,
Expand Down
41 changes: 41 additions & 0 deletions src/racer/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,27 @@ fn match_mod_inner(
}

fn find_generics_end(blob: &str) -> Option<BytePos> {
// Naive version that attempts to skip over attributes
let mut in_attr = false;
let mut attr_level = 0;

let mut level = 0;
for (i, b) in blob.as_bytes().into_iter().enumerate() {
// Naively skip attributes `#[...]`
if in_attr {
match b {
b'[' => attr_level += 1,
b']' => {
attr_level -=1;
if attr_level == 0 {
in_attr = false;
continue;
}
},
_ => continue,
}
}
// ...otherwise just try to find the last `>`
match b {
b'{' | b'(' | b';' => return None,
b'<' => level += 1,
Expand All @@ -436,6 +455,7 @@ fn find_generics_end(blob: &str) -> Option<BytePos> {
return Some(i.into());
}
}
b'#' if blob.bytes().nth(i + 1) == Some(b'[') => in_attr = true,
_ => {}
}
}
Expand Down Expand Up @@ -871,3 +891,24 @@ pub fn match_impl(decl: String, context: &MatchCxt<'_, '_>, offset: BytePos) ->
}
Some(out)
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn find_generics_end() {
use super::find_generics_end;
assert_eq!(
find_generics_end("Vec<T, #[unstable(feature = \"\", issue = \"\"] A: AllocRef = Global>"),
Some(BytePos(64))
);
assert_eq!(
find_generics_end("Vec<T, A: AllocRef = Global>"),
Some(BytePos(27))
);
assert_eq!(
find_generics_end("Result<Vec<String>, Option<&str>>"),
Some(BytePos(32))
);
}
}