Skip to content

fix: Ignore generic arguments in intra doc link path resolution #16702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exclude = ["crates/proc-macro-srv/proc-macro-test/imp"]
resolver = "2"

[workspace.package]
rust-version = "1.74"
rust-version = "1.76"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["rust-analyzer team"]
Expand Down
8 changes: 5 additions & 3 deletions crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn resolve_doc_path_on_(
AttrDefId::GenericParamId(_) => return None,
};

let mut modpath = modpath_from_str(link)?;
let mut modpath = doc_modpath_from_str(link)?;

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

fn modpath_from_str(link: &str) -> Option<ModPath> {
fn doc_modpath_from_str(link: &str) -> Option<ModPath> {
// FIXME: this is not how we should get a mod path here.
let try_get_modpath = |link: &str| {
let mut parts = link.split("::");
Expand Down Expand Up @@ -327,7 +327,9 @@ fn modpath_from_str(link: &str) -> Option<ModPath> {
};
let parts = first_segment.into_iter().chain(parts).map(|segment| match segment.parse() {
Ok(idx) => Name::new_tuple_field(idx),
Err(_) => Name::new_text_dont_use(segment.into()),
Err(_) => {
Name::new_text_dont_use(segment.split_once('<').map_or(segment, |it| it.0).into())
}
});
Some(ModPath::from_segments(kind, parts))
};
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ impl NameRefClass {

impl_from!(
Field, Module, Function, Adt, Variant, Const, Static, Trait, TraitAlias, TypeAlias, BuiltinType, Local,
GenericParam, Label, Macro
GenericParam, Label, Macro, ExternCrateDecl
for Definition
);

Expand Down
31 changes: 16 additions & 15 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,22 @@ pub(crate) fn doc_attributes(
) -> Option<(hir::AttrsWithOwner, Definition)> {
match_ast! {
match node {
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Module(def))),
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Module(def))),
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Function(def))),
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Struct(def)))),
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Union(def)))),
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Adt(hir::Adt::Enum(def)))),
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Variant(def))),
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Trait(def))),
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Static(def))),
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Const(def))),
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::TypeAlias(def))),
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
ast::SourceFile(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Module(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Fn(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Struct(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Struct(def)))),
ast::Union(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Union(def)))),
ast::Enum(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(hir::Adt::Enum(def)))),
ast::Variant(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Trait(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Static(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Const(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::TypeAlias(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
ast::ExternCrate(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::from(def))),
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
_ => None
}
Expand Down
52 changes: 24 additions & 28 deletions crates/ide/src/doc_links/intra_doc_links.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Helper tools for intra doc links.

const TYPES: ([&str; 9], [&str; 0]) =
(["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], []);
const VALUES: ([&str; 8], [&str; 1]) =
(["value", "function", "fn", "method", "const", "static", "mod", "module"], ["()"]);
const MACROS: ([&str; 2], [&str; 1]) = (["macro", "derive"], ["!"]);
const TYPES: (&[&str], &[&str]) =
(&["type", "struct", "enum", "mod", "trait", "union", "module", "prim", "primitive"], &[]);
const VALUES: (&[&str], &[&str]) =
(&["value", "function", "fn", "method", "const", "static", "mod", "module"], &["()"]);
const MACROS: (&[&str], &[&str]) = (&["macro", "derive"], &["!"]);

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

[
(hir::Namespace::Types, (TYPES.0.iter(), TYPES.1.iter())),
(hir::Namespace::Values, (VALUES.0.iter(), VALUES.1.iter())),
(hir::Namespace::Macros, (MACROS.0.iter(), MACROS.1.iter())),
(hir::Namespace::Types, TYPES),
(hir::Namespace::Values, VALUES),
(hir::Namespace::Macros, MACROS),
]
.into_iter()
.find_map(|(ns, (mut prefixes, mut suffixes))| {
if let Some(prefix) = prefixes.find(|&&prefix| {
.find_map(|(ns, (prefixes, suffixes))| {
if let Some(prefix) = prefixes.iter().find(|&&prefix| {
s.starts_with(prefix)
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
}) {
Some((&s[prefix.len() + 1..], ns))
} else {
suffixes.find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
suffixes.iter().find_map(|&suffix| s.strip_suffix(suffix).zip(Some(ns)))
}
})
.map_or((s, None), |(s, ns)| (s, Some(ns)))
}

pub(super) fn strip_prefixes_suffixes(s: &str) -> &str {
[
(TYPES.0.iter(), TYPES.1.iter()),
(VALUES.0.iter(), VALUES.1.iter()),
(MACROS.0.iter(), MACROS.1.iter()),
]
.into_iter()
.find_map(|(mut prefixes, mut suffixes)| {
if let Some(prefix) = prefixes.find(|&&prefix| {
s.starts_with(prefix)
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
}) {
Some(&s[prefix.len() + 1..])
} else {
suffixes.find_map(|&suffix| s.strip_suffix(suffix))
}
})
.unwrap_or(s)
[TYPES, VALUES, MACROS]
.into_iter()
.find_map(|(prefixes, suffixes)| {
if let Some(prefix) = prefixes.iter().find(|&&prefix| {
s.starts_with(prefix)
&& s.chars().nth(prefix.len()).map_or(false, |c| c == '@' || c == ' ')
}) {
Some(&s[prefix.len() + 1..])
} else {
suffixes.iter().find_map(|&suffix| s.strip_suffix(suffix))
}
})
.unwrap_or(s)
}

#[cfg(test)]
Expand Down
25 changes: 25 additions & 0 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6103,6 +6103,31 @@ pub struct Foo(i32);
);
}

#[test]
fn hover_intra_generics() {
check(
r#"
/// Doc comment for [`Foo$0<T>`]
pub struct Foo<T>(T);
"#,
expect![[r#"
*[`Foo<T>`]*

```rust
test
```

```rust
pub struct Foo<T>(T);
```

---

Doc comment for [`Foo<T>`](https://docs.rs/test/*/test/struct.Foo.html)
"#]],
);
}

#[test]
fn hover_inert_attr() {
check(
Expand Down
4 changes: 2 additions & 2 deletions crates/syntax/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "syntax-fuzz"
version = "0.0.1"
publish = false
edition = "2021"
rust-version = "1.66.1"
rust-version = "1.76"

[package.metadata]
cargo-fuzz = true
Expand All @@ -26,4 +26,4 @@ name = "reparse"
path = "fuzz_targets/reparse.rs"

[lints]
workspace = true
workspace = true