2020-12-16 14:35:15 -06:00
|
|
|
use either::Either;
|
2020-12-19 07:12:51 -06:00
|
|
|
use hir::{HasAttrs, ModuleDef, Semantics};
|
2020-08-13 09:39:16 -05:00
|
|
|
use ide_db::{
|
2020-11-28 15:50:59 -06:00
|
|
|
base_db::FileId,
|
2020-12-19 07:12:51 -06:00
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2020-03-03 11:54:39 -06:00
|
|
|
symbol_index, RootDatabase,
|
|
|
|
};
|
2020-12-19 07:12:51 -06:00
|
|
|
use syntax::{
|
2021-01-11 07:24:50 -06:00
|
|
|
ast, match_ast, AstNode, AstToken, SyntaxKind::*, SyntaxToken, TextSize, TokenAtOffset, T,
|
2020-12-19 07:12:51 -06:00
|
|
|
};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2019-05-22 11:49:22 -05:00
|
|
|
use crate::{
|
2020-02-22 09:57:29 -06:00
|
|
|
display::{ToNav, TryToNav},
|
2020-12-19 07:12:51 -06:00
|
|
|
doc_links::extract_definitions_from_markdown,
|
2021-01-11 07:24:50 -06:00
|
|
|
runnables::doc_owner_to_def,
|
2020-12-17 05:29:05 -06:00
|
|
|
FilePosition, NavigationTarget, RangeInfo, SymbolKind,
|
2019-05-22 11:49:22 -05:00
|
|
|
};
|
2019-01-08 13:33:36 -06:00
|
|
|
|
2020-05-31 02:45:41 -05:00
|
|
|
// Feature: Go to Definition
|
2020-05-30 18:54:54 -05:00
|
|
|
//
|
|
|
|
// Navigates to the definition of an identifier.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Shortcut
|
|
|
|
//
|
|
|
|
// | VS Code | kbd:[F12]
|
|
|
|
// |===
|
2019-01-08 16:38:51 -06:00
|
|
|
pub(crate) fn goto_definition(
|
2019-01-08 13:33:36 -06:00
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
2019-01-15 12:02:42 -06:00
|
|
|
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
2020-02-18 11:35:10 -06:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let file = sema.parse(position.file_id).syntax().clone();
|
2019-12-13 15:00:05 -06:00
|
|
|
let original_token = pick_best(file.token_at_offset(position.offset))?;
|
2020-02-18 11:35:10 -06:00
|
|
|
let token = sema.descend_into_macros(original_token.clone());
|
2020-07-10 07:08:35 -05:00
|
|
|
let parent = token.parent();
|
2020-12-19 07:12:51 -06:00
|
|
|
if let Some(comment) = ast::Comment::cast(token.clone()) {
|
|
|
|
let nav = def_for_doc_comment(&sema, position, &comment)?.try_to_nav(db)?;
|
|
|
|
return Some(RangeInfo::new(original_token.text_range(), vec![nav]));
|
|
|
|
}
|
2019-11-16 07:49:26 -06:00
|
|
|
|
2019-12-09 11:42:45 -06:00
|
|
|
let nav_targets = match_ast! {
|
2020-07-10 07:08:35 -05:00
|
|
|
match parent {
|
2019-11-17 11:15:55 -06:00
|
|
|
ast::NameRef(name_ref) => {
|
2020-12-16 14:35:15 -06:00
|
|
|
reference_definition(&sema, Either::Right(&name_ref)).to_vec()
|
2019-11-17 11:15:55 -06:00
|
|
|
},
|
|
|
|
ast::Name(name) => {
|
2020-10-15 10:33:32 -05:00
|
|
|
let def = NameClass::classify(&sema, &name)?.referenced_or_defined(sema.db);
|
2020-02-28 08:27:52 -06:00
|
|
|
let nav = def.try_to_nav(sema.db)?;
|
|
|
|
vec![nav]
|
2019-11-17 11:15:55 -06:00
|
|
|
},
|
2020-11-28 09:07:07 -06:00
|
|
|
ast::SelfParam(self_param) => {
|
2020-11-28 15:50:59 -06:00
|
|
|
vec![self_to_nav_target(self_param, position.file_id)?]
|
|
|
|
},
|
|
|
|
ast::PathSegment(segment) => {
|
|
|
|
segment.self_token()?;
|
|
|
|
let path = segment.parent_path();
|
|
|
|
if path.qualifier().is_some() && !ast::PathExpr::can_cast(path.syntax().parent()?.kind()) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let func = segment.syntax().ancestors().find_map(ast::Fn::cast)?;
|
|
|
|
let self_param = func.param_list()?.self_param()?;
|
|
|
|
vec![self_to_nav_target(self_param, position.file_id)?]
|
2020-11-28 09:07:07 -06:00
|
|
|
},
|
2020-12-16 14:35:15 -06:00
|
|
|
ast::Lifetime(lt) => if let Some(name_class) = NameClass::classify_lifetime(&sema, <) {
|
|
|
|
let def = name_class.referenced_or_defined(sema.db);
|
|
|
|
let nav = def.try_to_nav(sema.db)?;
|
|
|
|
vec![nav]
|
|
|
|
} else {
|
|
|
|
reference_definition(&sema, Either::Left(<)).to_vec()
|
|
|
|
},
|
2019-11-17 11:15:55 -06:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-09 11:42:45 -06:00
|
|
|
Some(RangeInfo::new(original_token.text_range(), nav_targets))
|
2019-11-16 13:18:07 -06:00
|
|
|
}
|
|
|
|
|
2020-12-19 07:12:51 -06:00
|
|
|
fn def_for_doc_comment(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
position: FilePosition,
|
|
|
|
doc_comment: &ast::Comment,
|
|
|
|
) -> Option<hir::ModuleDef> {
|
|
|
|
let parent = doc_comment.syntax().parent();
|
|
|
|
let (link, ns) = extract_positioned_link_from_comment(position, doc_comment)?;
|
2021-01-11 07:24:50 -06:00
|
|
|
|
|
|
|
let def = doc_owner_to_def(sema, parent)?;
|
|
|
|
match def {
|
2020-12-19 07:12:51 -06:00
|
|
|
Definition::ModuleDef(def) => match def {
|
2021-01-11 07:24:50 -06:00
|
|
|
ModuleDef::Module(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::Function(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::Adt(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::Variant(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::Const(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::Static(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::Trait(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
ModuleDef::TypeAlias(it) => it.resolve_doc_path(sema.db, &link, ns),
|
2020-12-19 07:12:51 -06:00
|
|
|
ModuleDef::BuiltinType(_) => return None,
|
|
|
|
},
|
2021-01-11 07:24:50 -06:00
|
|
|
Definition::Macro(it) => it.resolve_doc_path(sema.db, &link, ns),
|
|
|
|
Definition::Field(it) => it.resolve_doc_path(sema.db, &link, ns),
|
2020-12-19 07:12:51 -06:00
|
|
|
Definition::SelfType(_)
|
|
|
|
| Definition::Local(_)
|
2021-01-08 05:28:02 -06:00
|
|
|
| Definition::GenericParam(_)
|
2020-12-19 07:12:51 -06:00
|
|
|
| Definition::Label(_) => return None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_positioned_link_from_comment(
|
|
|
|
position: FilePosition,
|
|
|
|
comment: &ast::Comment,
|
|
|
|
) -> Option<(String, Option<hir::Namespace>)> {
|
|
|
|
let comment_range = comment.syntax().text_range();
|
|
|
|
let doc_comment = comment.doc_comment()?;
|
|
|
|
let def_links = extract_definitions_from_markdown(doc_comment);
|
|
|
|
let (def_link, ns, _) = def_links.iter().min_by_key(|(_, _, def_link_range)| {
|
|
|
|
let matched_position = comment_range.start() + TextSize::from(def_link_range.start as u32);
|
|
|
|
match position.offset.checked_sub(matched_position) {
|
|
|
|
Some(distance) => distance,
|
|
|
|
None => comment_range.end(),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
Some((def_link.to_string(), ns.clone()))
|
|
|
|
}
|
|
|
|
|
2019-12-13 15:00:05 -06:00
|
|
|
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
|
|
|
|
return tokens.max_by_key(priority);
|
|
|
|
fn priority(n: &SyntaxToken) -> usize {
|
|
|
|
match n.kind() {
|
2020-12-19 07:12:51 -06:00
|
|
|
IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | COMMENT => 2,
|
2019-12-13 15:00:05 -06:00
|
|
|
kind if kind.is_trivia() => 0,
|
|
|
|
_ => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-28 15:50:59 -06:00
|
|
|
fn self_to_nav_target(self_param: ast::SelfParam, file_id: FileId) -> Option<NavigationTarget> {
|
|
|
|
let self_token = self_param.self_token()?;
|
|
|
|
Some(NavigationTarget {
|
|
|
|
file_id,
|
|
|
|
full_range: self_param.syntax().text_range(),
|
|
|
|
focus_range: Some(self_token.text_range()),
|
|
|
|
name: self_token.text().clone(),
|
2020-12-18 12:10:13 -06:00
|
|
|
kind: Some(SymbolKind::SelfParam),
|
2020-11-28 15:50:59 -06:00
|
|
|
container_name: None,
|
|
|
|
description: None,
|
|
|
|
docs: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-28 08:43:10 -05:00
|
|
|
#[derive(Debug)]
|
2019-01-13 04:31:37 -06:00
|
|
|
pub(crate) enum ReferenceResult {
|
|
|
|
Exact(NavigationTarget),
|
|
|
|
Approximate(Vec<NavigationTarget>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReferenceResult {
|
|
|
|
fn to_vec(self) -> Vec<NavigationTarget> {
|
|
|
|
match self {
|
2020-04-18 15:05:06 -05:00
|
|
|
ReferenceResult::Exact(target) => vec![target],
|
|
|
|
ReferenceResult::Approximate(vec) => vec,
|
2019-01-13 04:31:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 16:38:51 -06:00
|
|
|
pub(crate) fn reference_definition(
|
2020-02-18 11:35:10 -06:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-12-16 14:35:15 -06:00
|
|
|
name_ref: Either<&ast::Lifetime, &ast::NameRef>,
|
2019-01-15 12:02:42 -06:00
|
|
|
) -> ReferenceResult {
|
2020-12-16 14:35:15 -06:00
|
|
|
let name_kind = name_ref.either(
|
|
|
|
|lifetime| NameRefClass::classify_lifetime(sema, lifetime),
|
|
|
|
|name_ref| NameRefClass::classify(sema, name_ref),
|
|
|
|
);
|
2020-08-09 17:52:19 -05:00
|
|
|
if let Some(def) = name_kind {
|
2020-10-15 10:33:32 -05:00
|
|
|
let def = def.referenced(sema.db);
|
2020-02-18 11:35:10 -06:00
|
|
|
return match def.try_to_nav(sema.db) {
|
2020-02-22 09:57:29 -06:00
|
|
|
Some(nav) => ReferenceResult::Exact(nav),
|
|
|
|
None => ReferenceResult::Approximate(Vec::new()),
|
|
|
|
};
|
|
|
|
}
|
2019-03-01 17:26:49 -06:00
|
|
|
|
2019-04-10 03:15:55 -05:00
|
|
|
// Fallback index based approach:
|
2020-12-16 14:35:15 -06:00
|
|
|
let name = name_ref.either(ast::Lifetime::text, ast::NameRef::text);
|
|
|
|
let navs =
|
|
|
|
symbol_index::index_resolve(sema.db, name).into_iter().map(|s| s.to_nav(sema.db)).collect();
|
2020-04-18 15:05:06 -05:00
|
|
|
ReferenceResult::Approximate(navs)
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-10-24 03:39:57 -05:00
|
|
|
use ide_db::base_db::FileRange;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{TextRange, TextSize};
|
2020-06-30 06:03:08 -05:00
|
|
|
|
2020-10-02 10:34:31 -05:00
|
|
|
use crate::fixture;
|
2020-06-30 06:03:08 -05:00
|
|
|
|
|
|
|
fn check(ra_fixture: &str) {
|
2020-10-02 10:34:31 -05:00
|
|
|
let (analysis, position, mut annotations) = fixture::annotations(ra_fixture);
|
2020-10-02 09:13:48 -05:00
|
|
|
let (mut expected, data) = annotations.pop().unwrap();
|
2020-06-30 06:03:08 -05:00
|
|
|
match data.as_str() {
|
|
|
|
"" => (),
|
|
|
|
"file" => {
|
|
|
|
expected.range =
|
|
|
|
TextRange::up_to(TextSize::of(&*analysis.file_text(expected.file_id).unwrap()))
|
|
|
|
}
|
|
|
|
data => panic!("bad data: {}", data),
|
2019-12-18 08:33:36 -06:00
|
|
|
}
|
2019-12-14 11:20:07 -06:00
|
|
|
|
2020-07-10 07:08:35 -05:00
|
|
|
let mut navs =
|
|
|
|
analysis.goto_definition(position).unwrap().expect("no definition found").info;
|
2020-06-27 11:21:26 -05:00
|
|
|
if navs.len() == 0 {
|
|
|
|
panic!("unresolved reference")
|
|
|
|
}
|
|
|
|
assert_eq!(navs.len(), 1);
|
|
|
|
|
|
|
|
let nav = navs.pop().unwrap();
|
2020-07-17 05:42:48 -05:00
|
|
|
assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
|
2020-06-27 11:21:26 -05:00
|
|
|
}
|
|
|
|
|
2020-08-08 13:14:18 -05:00
|
|
|
#[test]
|
|
|
|
fn goto_def_for_extern_crate() {
|
|
|
|
check(
|
|
|
|
r#"
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /main.rs crate:main deps:std
|
2021-01-06 14:15:48 -06:00
|
|
|
extern crate std$0;
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /std/lib.rs crate:std
|
2020-08-08 13:14:18 -05:00
|
|
|
// empty
|
|
|
|
//^ file
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_renamed_extern_crate() {
|
|
|
|
check(
|
|
|
|
r#"
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /main.rs crate:main deps:std
|
2021-01-06 14:15:48 -06:00
|
|
|
extern crate std as abc$0;
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /std/lib.rs crate:std
|
2020-08-08 13:14:18 -05:00
|
|
|
// empty
|
|
|
|
//^ file
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-08 13:33:36 -06:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_in_items() {
|
2020-06-27 11:21:26 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
enum E { X(Foo$0) }
|
2020-06-27 11:21:26 -05:00
|
|
|
"#,
|
2019-12-13 12:20:02 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_at_start_of_item() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
enum E { X($0Foo) }
|
2020-06-30 06:03:08 -05:00
|
|
|
"#,
|
2019-01-07 17:30:49 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_definition_resolves_correct_name() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
use a::Foo;
|
|
|
|
mod a;
|
|
|
|
mod b;
|
2021-01-06 14:15:48 -06:00
|
|
|
enum E { X(Foo$0) }
|
2019-12-18 07:52:58 -06:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
//- /a.rs
|
|
|
|
struct Foo;
|
|
|
|
//^^^
|
|
|
|
//- /b.rs
|
|
|
|
struct Foo;
|
|
|
|
"#,
|
2019-01-08 13:33:36 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_module_declaration() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
mod $0foo;
|
2019-12-18 07:52:58 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /foo.rs
|
|
|
|
// empty
|
2020-06-30 06:03:08 -05:00
|
|
|
//^ file
|
2020-06-23 15:27:24 -05:00
|
|
|
"#,
|
2019-01-08 13:33:36 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
mod $0foo;
|
2019-12-18 07:52:58 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /foo/mod.rs
|
|
|
|
// empty
|
2020-06-30 06:03:08 -05:00
|
|
|
//^ file
|
2020-06-23 15:27:24 -05:00
|
|
|
"#,
|
2019-01-08 13:33:36 -06:00
|
|
|
);
|
|
|
|
}
|
2019-01-13 09:56:57 -06:00
|
|
|
|
2019-04-24 15:16:50 -05:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_macros() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! foo { () => { () } }
|
|
|
|
//^^^
|
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0foo!();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-04-24 15:16:50 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-01 06:34:19 -05:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_macros_from_other_crates() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2020-06-30 06:03:08 -05:00
|
|
|
use foo::foo;
|
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0foo!();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
2020-05-01 09:26:22 -05:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
//- /foo/lib.rs
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! foo { () => { () } }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
2020-06-23 15:27:24 -05:00
|
|
|
"#,
|
2019-06-01 06:34:19 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-31 12:29:56 -05:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_macros_in_use_tree() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::foo$0;
|
2019-10-31 12:29:56 -05:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
//- /foo/lib.rs
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! foo { () => { () } }
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-10-31 12:29:56 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-03 11:49:49 -06:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_macro_defined_fn_with_arg() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! define_fn {
|
|
|
|
($name:ident) => (fn $name() {})
|
|
|
|
}
|
2019-11-03 11:49:49 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
define_fn!(foo);
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
2019-11-03 11:49:49 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0foo();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-11-03 11:49:49 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_macro_defined_fn_no_arg() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! define_fn {
|
|
|
|
() => (fn foo() {})
|
|
|
|
}
|
2019-11-03 11:49:49 -06:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
define_fn!();
|
|
|
|
//^^^^^^^^^^^^^
|
2019-11-03 11:49:49 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0foo();
|
2020-06-23 15:27:24 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-11-03 11:49:49 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-20 09:11:07 -06:00
|
|
|
#[test]
|
|
|
|
fn goto_definition_works_for_macro_inside_pattern() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! foo {() => {0}}
|
|
|
|
//^^^
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
match (0,1) {
|
2021-01-06 14:15:48 -06:00
|
|
|
($0foo!(), _) => {}
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-12-20 09:11:07 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_definition_works_for_macro_inside_match_arm_lhs() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
macro_rules! foo {() => {0}}
|
|
|
|
//^^^
|
|
|
|
fn bar() {
|
|
|
|
match 0 {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0foo!() => {}
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_use_alias() {
|
|
|
|
check(
|
|
|
|
r#"
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /lib.rs crate:main deps:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo as bar$0;
|
2020-06-30 06:03:08 -05:00
|
|
|
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /foo/lib.rs crate:foo
|
2020-06-30 06:03:08 -05:00
|
|
|
// empty
|
|
|
|
//^ file
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_use_alias_foo_macro() {
|
|
|
|
check(
|
|
|
|
r#"
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /lib.rs crate:main deps:foo
|
2021-01-06 14:15:48 -06:00
|
|
|
use foo::foo as bar$0;
|
2020-06-30 06:03:08 -05:00
|
|
|
|
2020-10-02 09:13:48 -05:00
|
|
|
//- /foo/lib.rs crate:foo
|
2020-06-30 06:03:08 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! foo { () => { () } }
|
|
|
|
//^^^
|
|
|
|
"#,
|
2019-12-20 09:11:07 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-13 09:56:57 -06:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_methods() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn frobnicate(&self) { }
|
|
|
|
//^^^^^^^^^^
|
|
|
|
}
|
2019-01-13 09:56:57 -06:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar(foo: &Foo) {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo.frobnicate$0();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-01-13 09:56:57 -06:00
|
|
|
);
|
2019-01-25 11:32:34 -06:00
|
|
|
}
|
2019-01-13 09:56:57 -06:00
|
|
|
|
2019-01-25 11:32:34 -06:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_fields() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
spam: u32,
|
|
|
|
} //^^^^
|
2019-01-25 11:32:34 -06:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar(foo: &Foo) {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo.spam$0;
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-01-13 09:56:57 -06:00
|
|
|
);
|
|
|
|
}
|
2019-02-23 03:02:42 -06:00
|
|
|
|
2019-02-27 09:22:53 -06:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_record_fields() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo {
|
|
|
|
spam: u32,
|
|
|
|
} //^^^^
|
2019-02-27 09:22:53 -06:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar() -> Foo {
|
|
|
|
Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
spam$0: 0,
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-02-27 09:22:53 -06:00
|
|
|
);
|
|
|
|
}
|
2020-04-18 15:05:06 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_record_pat_fields() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
struct Foo {
|
|
|
|
spam: u32,
|
|
|
|
} //^^^^
|
2020-04-18 15:05:06 -05:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar(foo: Foo) -> Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
let Foo { spam$0: _, } = foo
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-04-18 15:05:06 -05:00
|
|
|
);
|
|
|
|
}
|
2019-10-31 09:13:52 -05:00
|
|
|
|
2020-03-25 07:53:15 -05:00
|
|
|
#[test]
|
|
|
|
fn goto_def_for_record_fields_macros() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-03-25 07:53:15 -05:00
|
|
|
r"
|
2020-06-30 06:03:08 -05:00
|
|
|
macro_rules! m { () => { 92 };}
|
|
|
|
struct Foo { spam: u32 }
|
|
|
|
//^^^^
|
2020-03-25 07:53:15 -05:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar() -> Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
Foo { spam$0: m!() }
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
",
|
2020-03-25 07:53:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-13 14:59:25 -06:00
|
|
|
#[test]
|
|
|
|
fn goto_for_tuple_fields() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo(u32);
|
|
|
|
//^^^
|
|
|
|
|
|
|
|
fn bar() {
|
|
|
|
let foo = Foo(0);
|
2021-01-06 14:15:48 -06:00
|
|
|
foo.$00;
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-12-13 14:59:25 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-31 09:13:52 -05:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_ufcs_inherent_methods() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn frobnicate() { }
|
|
|
|
} //^^^^^^^^^^
|
2019-10-31 09:13:52 -05:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar(foo: &Foo) {
|
2021-01-06 14:15:48 -06:00
|
|
|
Foo::frobnicate$0();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-10-31 09:13:52 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_ufcs_trait_methods_through_traits() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Foo {
|
|
|
|
fn frobnicate();
|
|
|
|
} //^^^^^^^^^^
|
2019-10-31 09:13:52 -05:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
Foo::frobnicate$0();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-10-31 09:13:52 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_for_ufcs_trait_methods_through_self() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
trait Trait {
|
|
|
|
fn frobnicate();
|
|
|
|
} //^^^^^^^^^^
|
|
|
|
impl Trait for Foo {}
|
2019-10-31 09:13:52 -05:00
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
Foo::frobnicate$0();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-10-31 09:13:52 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 02:32:39 -06:00
|
|
|
#[test]
|
|
|
|
fn goto_definition_on_self() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
//^^^
|
|
|
|
pub fn new() -> Self {
|
2021-01-06 14:15:48 -06:00
|
|
|
Self$0 {}
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
pub fn new() -> Self$0 {
|
2020-06-30 06:03:08 -05:00
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { A }
|
|
|
|
impl Foo {
|
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
pub fn new() -> Self$0 {
|
2020-06-30 06:03:08 -05:00
|
|
|
Foo::A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { A }
|
|
|
|
impl Foo {
|
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
pub fn thing(a: &Self$0) {
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-03-07 02:32:39 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_definition_on_self_in_trait_impl() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
trait Make {
|
|
|
|
fn new() -> Self;
|
|
|
|
}
|
|
|
|
impl Make for Foo {
|
|
|
|
//^^^
|
|
|
|
fn new() -> Self {
|
2021-01-06 14:15:48 -06:00
|
|
|
Self$0 {}
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-03-07 02:32:39 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
trait Make {
|
|
|
|
fn new() -> Self;
|
|
|
|
}
|
|
|
|
impl Make for Foo {
|
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
fn new() -> Self$0 {
|
2020-06-30 06:03:08 -05:00
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-03-07 02:32:39 -06:00
|
|
|
);
|
|
|
|
}
|
2019-02-27 09:22:53 -06:00
|
|
|
|
2019-02-23 03:02:42 -06:00
|
|
|
#[test]
|
2019-12-20 07:47:01 -06:00
|
|
|
fn goto_def_when_used_on_definition_name_itself() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo$0 { value: u32 }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2019-02-23 03:02:42 -06:00
|
|
|
r#"
|
2020-06-30 06:03:08 -05:00
|
|
|
struct Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
field$0: string,
|
2020-06-30 06:03:08 -05:00
|
|
|
} //^^^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo_test$0() { }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^^^^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
enum Foo$0 { Variant }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2019-02-23 03:02:42 -06:00
|
|
|
r#"
|
2020-06-30 06:03:08 -05:00
|
|
|
enum Foo {
|
|
|
|
Variant1,
|
2021-01-06 14:15:48 -06:00
|
|
|
Variant2$0,
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^^^^^^
|
|
|
|
Variant3,
|
|
|
|
}
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2019-02-23 03:02:42 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
static INNER$0: &str = "";
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2019-02-23 03:02:42 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
const INNER$0: &str = "";
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2019-02-23 03:02:42 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
type Thing$0 = Option<()>;
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2019-02-23 03:02:42 -06:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
trait Foo$0 { }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
mod bar$0 { }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^^^
|
|
|
|
"#,
|
2019-02-23 03:02:42 -06:00
|
|
|
);
|
|
|
|
}
|
2019-11-16 07:49:26 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_from_macro() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
macro_rules! id {
|
|
|
|
($($tt:tt)*) => { $($tt)* }
|
|
|
|
}
|
|
|
|
fn foo() {}
|
|
|
|
//^^^
|
|
|
|
id! {
|
|
|
|
fn bar() {
|
2021-01-06 14:15:48 -06:00
|
|
|
fo$0o();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mod confuse_index { fn foo(); }
|
|
|
|
"#,
|
2019-11-16 07:49:26 -06:00
|
|
|
);
|
|
|
|
}
|
2019-12-06 12:30:15 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_through_format() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! format {
|
|
|
|
($($arg:tt)*) => ($crate::fmt::format($crate::__export::format_args!($($arg)*)))
|
|
|
|
}
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! format_args {
|
|
|
|
($fmt:expr) => ({ /* compiler built-in */ });
|
|
|
|
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
|
|
|
|
}
|
|
|
|
pub mod __export {
|
|
|
|
pub use crate::format_args;
|
|
|
|
fn foo() {} // for index confusion
|
|
|
|
}
|
|
|
|
fn foo() -> i8 {}
|
|
|
|
//^^^
|
|
|
|
fn test() {
|
2021-01-06 14:15:48 -06:00
|
|
|
format!("{}", fo$0o())
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-12-06 12:30:15 -06:00
|
|
|
);
|
|
|
|
}
|
2019-12-07 11:54:18 -06:00
|
|
|
|
2020-12-22 07:42:28 -06:00
|
|
|
#[test]
|
|
|
|
fn goto_through_included_file() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
macro_rules! include {}
|
|
|
|
|
|
|
|
include!("foo.rs");
|
|
|
|
//^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
fn f() {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo$0();
|
2020-12-22 07:42:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mod confuse_index {
|
|
|
|
pub fn foo() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
fn foo() {}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:54:18 -06:00
|
|
|
#[test]
|
|
|
|
fn goto_for_type_param() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-05-04 05:20:38 -05:00
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
struct Foo<T: Clone> { t: $0T }
|
2020-06-30 06:03:08 -05:00
|
|
|
//^
|
|
|
|
"#,
|
2019-12-07 11:54:18 -06:00
|
|
|
);
|
|
|
|
}
|
2019-12-18 09:25:15 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_within_macro() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
macro_rules! id {
|
|
|
|
($($tt:tt)*) => ($($tt)*)
|
|
|
|
}
|
2019-12-18 09:25:15 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn foo() {
|
|
|
|
let x = 1;
|
2020-06-30 06:03:08 -05:00
|
|
|
//^
|
2020-06-23 15:27:24 -05:00
|
|
|
id!({
|
2021-01-06 14:15:48 -06:00
|
|
|
let y = $0x;
|
2020-06-23 15:27:24 -05:00
|
|
|
let z = y;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"#,
|
2019-12-18 09:25:15 -06:00
|
|
|
);
|
|
|
|
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
2020-06-23 15:27:24 -05:00
|
|
|
r#"
|
|
|
|
macro_rules! id {
|
|
|
|
($($tt:tt)*) => ($($tt)*)
|
|
|
|
}
|
2019-12-18 09:25:15 -06:00
|
|
|
|
2020-06-23 15:27:24 -05:00
|
|
|
fn foo() {
|
|
|
|
let x = 1;
|
|
|
|
id!({
|
|
|
|
let y = x;
|
2020-06-30 06:03:08 -05:00
|
|
|
//^
|
2021-01-06 14:15:48 -06:00
|
|
|
let z = $0y;
|
2020-06-23 15:27:24 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
"#,
|
2019-12-18 09:25:15 -06:00
|
|
|
);
|
|
|
|
}
|
2019-12-20 04:19:09 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_in_local_fn() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
fn foo() {
|
|
|
|
let x = 92;
|
|
|
|
//^
|
2021-01-06 14:15:48 -06:00
|
|
|
$0x;
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-12-20 04:19:09 -06:00
|
|
|
);
|
|
|
|
}
|
2019-12-20 07:47:01 -06:00
|
|
|
|
2020-03-14 01:25:30 -05:00
|
|
|
#[test]
|
|
|
|
fn goto_def_in_local_macro() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn bar() {
|
|
|
|
macro_rules! foo { () => { () } }
|
|
|
|
//^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
$0foo!();
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-14 01:25:30 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-20 07:47:01 -06:00
|
|
|
#[test]
|
|
|
|
fn goto_def_for_field_init_shorthand() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo { x: i32 }
|
|
|
|
fn main() {
|
|
|
|
let x = 92;
|
|
|
|
//^
|
2021-01-06 14:15:48 -06:00
|
|
|
Foo { x$0 };
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-12-20 07:47:01 -06:00
|
|
|
)
|
|
|
|
}
|
2020-06-06 14:16:59 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_enum_variant_field() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo {
|
|
|
|
Bar { x: i32 }
|
|
|
|
} //^
|
|
|
|
fn baz(foo: Foo) {
|
|
|
|
match foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
Foo::Bar { x$0 } => x
|
2020-06-30 06:03:08 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
2020-06-06 14:16:59 -05:00
|
|
|
);
|
|
|
|
}
|
2020-06-10 12:11:47 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_enum_variant_self_pattern_const() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar }
|
|
|
|
//^^^
|
|
|
|
impl Foo {
|
|
|
|
fn baz(self) {
|
2021-01-06 14:15:48 -06:00
|
|
|
match self { Self::Bar$0 => {} }
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-06-10 12:11:47 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_enum_variant_self_pattern_record() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar { val: i32 } }
|
|
|
|
//^^^
|
|
|
|
impl Foo {
|
|
|
|
fn baz(self) -> i32 {
|
2021-01-06 14:15:48 -06:00
|
|
|
match self { Self::Bar$0 { val } => {} }
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-06-10 12:11:47 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_enum_variant_self_expr_const() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar }
|
|
|
|
//^^^
|
|
|
|
impl Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
fn baz(self) { Self::Bar$0; }
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-06-10 12:11:47 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_enum_variant_self_expr_record() {
|
2020-06-30 06:03:08 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Foo { Bar { val: i32 } }
|
|
|
|
//^^^
|
|
|
|
impl Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
fn baz(self) { Self::Bar$0 {val: 4}; }
|
2020-06-30 06:03:08 -05:00
|
|
|
}
|
|
|
|
"#,
|
2020-06-10 12:11:47 -05:00
|
|
|
);
|
|
|
|
}
|
2020-07-11 05:45:30 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_type_alias_generic_parameter() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
type Alias<T> = T$0;
|
2020-07-11 05:45:30 -05:00
|
|
|
//^
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2020-07-14 10:23:33 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_macro_container() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 14:15:48 -06:00
|
|
|
foo::module$0::mac!();
|
2020-07-14 10:23:33 -05:00
|
|
|
|
|
|
|
//- /foo/lib.rs
|
|
|
|
pub mod module {
|
|
|
|
//^^^^^^
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! _mac { () => { () } }
|
|
|
|
pub use crate::_mac as mac;
|
|
|
|
}
|
2020-07-17 07:58:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_assoc_ty_in_path() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Iterator {
|
|
|
|
type Item;
|
|
|
|
//^^^^
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn f() -> impl Iterator<Item$0 = u8> {}
|
2020-07-17 07:58:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_assoc_ty_in_path_multiple() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Iterator {
|
|
|
|
type A;
|
|
|
|
//^
|
|
|
|
type B;
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn f() -> impl Iterator<A$0 = u8, B = ()> {}
|
2020-07-17 07:58:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Iterator {
|
|
|
|
type A;
|
|
|
|
type B;
|
|
|
|
//^
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn f() -> impl Iterator<A = u8, B$0 = ()> {}
|
2020-07-17 07:58:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_assoc_ty_ufcs() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Iterator {
|
|
|
|
type Item;
|
|
|
|
//^^^^
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn g() -> <() as Iterator<Item$0 = ()>>::Item {}
|
2020-07-17 07:58:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_assoc_ty_ufcs_multiple() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Iterator {
|
|
|
|
type A;
|
|
|
|
//^
|
|
|
|
type B;
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn g() -> <() as Iterator<A$0 = (), B = u8>>::B {}
|
2020-07-17 07:58:49 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait Iterator {
|
|
|
|
type A;
|
|
|
|
type B;
|
|
|
|
//^
|
|
|
|
}
|
|
|
|
|
2021-01-06 14:15:48 -06:00
|
|
|
fn g() -> <() as Iterator<A = (), B$0 = u8>>::A {}
|
2020-11-28 09:07:07 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-28 15:50:59 -06:00
|
|
|
fn goto_self_param_ty_specified() {
|
2020-11-28 09:07:07 -06:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
2020-11-28 15:50:59 -06:00
|
|
|
fn bar(self: &Foo) {
|
|
|
|
//^^^^
|
2021-01-06 14:15:48 -06:00
|
|
|
let foo = sel$0f;
|
2020-11-28 15:50:59 -06:00
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
2020-11-28 09:07:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-28 15:50:59 -06:00
|
|
|
fn goto_self_param_on_decl() {
|
2020-11-28 09:07:07 -06:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Foo {}
|
|
|
|
|
|
|
|
impl Foo {
|
2021-01-06 14:15:48 -06:00
|
|
|
fn bar(&self$0) {
|
2020-11-28 15:50:59 -06:00
|
|
|
//^^^^
|
|
|
|
}
|
2020-12-16 14:35:15 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_lifetime_param_on_decl() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo<'foobar$0>(_: &'foobar ()) {
|
2020-12-16 14:35:15 -06:00
|
|
|
//^^^^^^^
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_lifetime_param_decl() {
|
|
|
|
check(
|
|
|
|
r#"
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo<'foobar>(_: &'foobar$0 ()) {
|
2020-12-16 14:35:15 -06:00
|
|
|
//^^^^^^^
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_lifetime_param_decl_nested() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo<'foobar>(_: &'foobar ()) {
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo<'foobar>(_: &'foobar$0 ()) {}
|
2020-12-16 14:35:15 -06:00
|
|
|
//^^^^^^^
|
2020-11-28 15:50:59 -06:00
|
|
|
}"#,
|
|
|
|
)
|
2020-07-14 10:23:33 -05:00
|
|
|
}
|
2020-12-17 15:01:42 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore] // requires the HIR to somehow track these hrtb lifetimes
|
|
|
|
fn goto_lifetime_hrtb() {
|
|
|
|
check(
|
|
|
|
r#"trait Foo<T> {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo<T>() where for<'a> T: Foo<&'a$0 (u8, u16)>, {}
|
2020-12-17 15:01:42 -06:00
|
|
|
//^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"trait Foo<T> {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo<T>() where for<'a$0> T: Foo<&'a (u8, u16)>, {}
|
2020-12-17 15:01:42 -06:00
|
|
|
//^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore] // requires ForTypes to be implemented
|
|
|
|
fn goto_lifetime_hrtb_for_type() {
|
|
|
|
check(
|
|
|
|
r#"trait Foo<T> {}
|
2021-01-06 14:15:48 -06:00
|
|
|
fn foo<T>() where T: for<'a> Foo<&'a$0 (u8, u16)>, {}
|
2020-12-17 15:01:42 -06:00
|
|
|
//^^
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
2020-12-23 10:15:01 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_label() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn foo<'foo>(_: &'foo ()) {
|
|
|
|
'foo: {
|
|
|
|
//^^^^
|
|
|
|
'bar: loop {
|
2021-01-06 14:15:48 -06:00
|
|
|
break 'foo$0;
|
2020-12-23 10:15:01 -06:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 07:12:51 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-11 07:24:50 -06:00
|
|
|
fn goto_def_for_intra_doc_link_same_file() {
|
2020-12-19 07:12:51 -06:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
/// Blah, [`bar`](bar) .. [`foo`](foo)$0 has [`bar`](bar)
|
|
|
|
pub fn bar() { }
|
|
|
|
|
|
|
|
/// You might want to see [`std::fs::read()`] too.
|
|
|
|
pub fn foo() { }
|
|
|
|
//^^^
|
|
|
|
|
2020-12-23 10:15:01 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2021-01-11 07:24:50 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn goto_def_for_intra_doc_link_inner() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod m;
|
|
|
|
struct S;
|
|
|
|
//^
|
|
|
|
|
|
|
|
//- /m.rs
|
|
|
|
//! [`super::S$0`]
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|