Skip to content

Commit 4ef6a49

Browse files
committed
Auto merge of rust-lang#16702 - Veykril:intra-doc-links-generic, r=Veykril
fix: Ignore generic arguments in intra doc link path resolution Fixes rust-lang/rust-analyzer#16699
2 parents 5c4c126 + ab533d8 commit 4ef6a49

File tree

7 files changed

+74
-50
lines changed

7 files changed

+74
-50
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exclude = ["crates/proc-macro-srv/proc-macro-test/imp"]
44
resolver = "2"
55

66
[workspace.package]
7-
rust-version = "1.74"
7+
rust-version = "1.76"
88
edition = "2021"
99
license = "MIT OR Apache-2.0"
1010
authors = ["rust-analyzer team"]

crates/hir/src/attrs.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn resolve_doc_path_on_(
124124
AttrDefId::GenericParamId(_) => return None,
125125
};
126126

127-
let mut modpath = modpath_from_str(link)?;
127+
let mut modpath = doc_modpath_from_str(link)?;
128128

129129
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
130130
if resolved.is_none() {
@@ -299,7 +299,7 @@ fn as_module_def_if_namespace_matches(
299299
(ns.unwrap_or(expected_ns) == expected_ns).then_some(DocLinkDef::ModuleDef(def))
300300
}
301301

302-
fn modpath_from_str(link: &str) -> Option<ModPath> {
302+
fn doc_modpath_from_str(link: &str) -> Option<ModPath> {
303303
// FIXME: this is not how we should get a mod path here.
304304
let try_get_modpath = |link: &str| {
305305
let mut parts = link.split("::");
@@ -327,7 +327,9 @@ fn modpath_from_str(link: &str) -> Option<ModPath> {
327327
};
328328
let parts = first_segment.into_iter().chain(parts).map(|segment| match segment.parse() {
329329
Ok(idx) => Name::new_tuple_field(idx),
330-
Err(_) => Name::new_text_dont_use(segment.into()),
330+
Err(_) => {
331+
Name::new_text_dont_use(segment.split_once('<').map_or(segment, |it| it.0).into())
332+
}
331333
});
332334
Some(ModPath::from_segments(kind, parts))
333335
};

crates/ide-db/src/defs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ impl NameRefClass {
721721

722722
impl_from!(
723723
Field, Module, Function, Adt, Variant, Const, Static, Trait, TraitAlias, TypeAlias, BuiltinType, Local,
724-
GenericParam, Label, Macro
724+
GenericParam, Label, Macro, ExternCrateDecl
725725
for Definition
726726
);
727727

crates/ide/src/doc_links.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,22 @@ pub(crate) fn doc_attributes(
233233
) -> Option<(hir::AttrsWithOwner, Definition)> {
234234
match_ast! {
235235
match node {
236-
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Module(def))),
237-
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Module(def))),
238-
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Function(def))),
239-
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Struct(def)))),
240-
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Union(def)))),
241-
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Enum(def)))),
242-
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Variant(def))),
243-
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Trait(def))),
244-
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Static(def))),
245-
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Const(def))),
246-
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::TypeAlias(def))),
247-
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
248-
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
249-
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
250-
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
236+
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
237+
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
238+
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
239+
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Struct(def)))),
240+
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Union(def)))),
241+
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Enum(def)))),
242+
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
243+
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
244+
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
245+
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
246+
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
247+
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
248+
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
249+
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
250+
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
251+
ast::ExternCrate(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
251252
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
252253
_ => None
253254
}

crates/ide/src/doc_links/intra_doc_links.rs

+24-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Helper tools for intra doc links.
22
3-
const TYPES: ([&str; 9], [&str; 0]) =
4-
(["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], []);
5-
const VALUES: ([&str; 8], [&str; 1]) =
6-
(["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
7-
const MACROS: ([&str; 2], [&str; 1]) = (["macro", "derive"], ["!"]);
3+
const TYPES: (&[&str], &[&str]) =
4+
(&["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], &[]);
5+
const VALUES: (&[&str], &[&str]) =
6+
(&["value", "function", "fn", "method", "const", "static", "mod", "module"], &["()"]);
7+
const MACROS: (&[&str], &[&str]) = (&["macro", "derive"], &["!"]);
88

99
/// Extract the specified namespace from an intra-doc-link if one exists.
1010
///
@@ -17,42 +17,38 @@ pub(super) fn parse_intra_doc_link(s: &str) -> (&str, Option<hir::Namespace>) {
1717
let s = s.trim_matches('`');
1818

1919
[
20-
(hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
21-
(hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
22-
(hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
20+
(hir::Namespace::Types, TYPES),
21+
(hir::Namespace::Values, VALUES),
22+
(hir::Namespace::Macros, MACROS),
2323
]
2424
.into_iter()
25-
.find_map(|(ns, (mut prefixes, mut suffixes))| {
26-
if let Some(prefix) = prefixes.find(|&&prefix| {
25+
.find_map(|(ns, (prefixes, suffixes))| {
26+
if let Some(prefix) = prefixes.iter().find(|&&prefix| {
2727
s.starts_with(prefix)
2828
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
2929
}) {
3030
Some((&s[prefix.len() + 1..], ns))
3131
} else {
32-
suffixes.find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
32+
suffixes.iter().find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
3333
}
3434
})
3535
.map_or((s, None), |(s, ns)| (s, Some(ns)))
3636
}
3737

3838
pub(super) fn strip_prefixes_suffixes(s: &str) -> &str {
39-
[
40-
(TYPES.0.iter(), TYPES.1.iter()),
41-
(VALUES.0.iter(), VALUES.1.iter()),
42-
(MACROS.0.iter(), MACROS.1.iter()),
43-
]
44-
.into_iter()
45-
.find_map(|(mut prefixes, mut suffixes)| {
46-
if let Some(prefix) = prefixes.find(|&&prefix| {
47-
s.starts_with(prefix)
48-
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
49-
}) {
50-
Some(&s[prefix.len() + 1..])
51-
} else {
52-
suffixes.find_map(|&suffix| s.strip_suffix(suffix))
53-
}
54-
})
55-
.unwrap_or(s)
39+
[TYPES, VALUES, MACROS]
40+
.into_iter()
41+
.find_map(|(prefixes, suffixes)| {
42+
if let Some(prefix) = prefixes.iter().find(|&&prefix| {
43+
s.starts_with(prefix)
44+
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
45+
}) {
46+
Some(&s[prefix.len() + 1..])
47+
} else {
48+
suffixes.iter().find_map(|&suffix| s.strip_suffix(suffix))
49+
}
50+
})
51+
.unwrap_or(s)
5652
}
5753

5854
#[cfg(test)]

crates/ide/src/hover/tests.rs

+25
Original file line numberDiff line numberDiff line change
@@ -6103,6 +6103,31 @@ pub struct Foo(i32);
61036103
);
61046104
}
61056105

6106+
#[test]
6107+
fn hover_intra_generics() {
6108+
check(
6109+
r#"
6110+
/// Doc comment for [`Foo$0<T>`]
6111+
pub struct Foo<T>(T);
6112+
"#,
6113+
expect![[r#"
6114+
*[`Foo<T>`]*
6115+
6116+
```rust
6117+
test
6118+
```
6119+
6120+
```rust
6121+
pub struct Foo<T>(T);
6122+
```
6123+
6124+
---
6125+
6126+
Doc comment for [`Foo<T>`](https://docs.rs/test/*/test/struct.Foo.html)
6127+
"#]],
6128+
);
6129+
}
6130+
61066131
#[test]
61076132
fn hover_inert_attr() {
61086133
check(

crates/syntax/fuzz/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "syntax-fuzz"
33
version = "0.0.1"
44
publish = false
55
edition = "2021"
6-
rust-version = "1.66.1"
6+
rust-version = "1.76"
77

88
[package.metadata]
99
cargo-fuzz = true
@@ -26,4 +26,4 @@ name = "reparse"
2626
path = "fuzz_targets/reparse.rs"
2727

2828
[lints]
29-
workspace = true
29+
workspace = true

0 commit comments

Comments
 (0)