Skip to content

Commit 6cf7b5f

Browse files
committed
Don't parse intra doc links as syntax trees
1 parent 5a34341 commit 6cf7b5f

File tree

8 files changed

+33
-55
lines changed

8 files changed

+33
-55
lines changed

crates/hir-def/src/visibility.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::iter;
44

55
use hir_expand::{span_map::SpanMapRef, InFile};
66
use la_arena::ArenaMap;
7-
use stdx::assert_eq_size;
87
use syntax::ast;
98
use triomphe::Arc;
109

@@ -25,7 +24,6 @@ pub enum RawVisibility {
2524
/// `pub`.
2625
Public,
2726
}
28-
assert_eq_size!(RawVisibility, 48);
2927

3028
impl RawVisibility {
3129
pub(crate) const fn private() -> RawVisibility {

crates/hir-expand/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod span_map;
2626
mod fixup;
2727

2828
use attrs::collect_attrs;
29-
use stdx::assert_eq_size;
3029
use triomphe::Arc;
3130

3231
use std::{fmt, hash::Hash};
@@ -176,7 +175,6 @@ pub struct MacroCallLoc {
176175
pub kind: MacroCallKind,
177176
pub call_site: Span,
178177
}
179-
assert_eq_size!(MacroCallLoc, 104);
180178

181179
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
182180
pub struct MacroDefId {
@@ -187,7 +185,6 @@ pub struct MacroDefId {
187185
pub allow_internal_unsafe: bool,
188186
pub span: Span,
189187
}
190-
assert_eq_size!(MacroDefId, 44);
191188

192189
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
193190
pub enum MacroDefKind {

crates/hir-expand/src/mod_path.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ use crate::{
1515
use base_db::CrateId;
1616
use smallvec::SmallVec;
1717
use span::SyntaxContextId;
18-
use stdx::assert_eq_size;
1918
use syntax::{ast, AstNode};
2019

2120
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2221
pub struct ModPath {
2322
pub kind: PathKind,
2423
segments: SmallVec<[Name; 1]>,
2524
}
26-
assert_eq_size!(ModPath, 40);
2725

2826
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2927
pub struct UnescapedModPath<'a>(&'a ModPath);

crates/hir-expand/src/name.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::fmt;
44

5-
use stdx::assert_eq_size;
65
use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
76

87
/// `Name` is a wrapper around string, which is used in hir for both references
@@ -14,7 +13,6 @@ use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
1413
/// name without "r#".
1514
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1615
pub struct Name(Repr);
17-
assert_eq_size!(Name, 24);
1816

1917
/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
2018
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]

crates/hir/src/attrs.rs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::ops::ControlFlow;
44

5-
use base_db::FileId;
65
use hir_def::{
76
attr::AttrsWithOwner,
87
item_scope::ItemInNs,
@@ -11,12 +10,8 @@ use hir_def::{
1110
resolver::{HasResolver, Resolver, TypeNs},
1211
AssocItemId, AttrDefId, ModuleDefId,
1312
};
14-
use hir_expand::{
15-
name::Name,
16-
span_map::{RealSpanMap, SpanMapRef},
17-
};
13+
use hir_expand::{mod_path::PathKind, name::Name};
1814
use hir_ty::{db::HirDatabase, method_resolution};
19-
use syntax::{ast, AstNode};
2015

2116
use crate::{
2217
Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, ExternCrateDecl,
@@ -129,7 +124,7 @@ fn resolve_doc_path_on_(
129124
AttrDefId::GenericParamId(_) => return None,
130125
};
131126

132-
let mut modpath = modpath_from_str(db, link)?;
127+
let mut modpath = modpath_from_str(link)?;
133128

134129
let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
135130
if resolved.is_none() {
@@ -305,34 +300,37 @@ fn as_module_def_if_namespace_matches(
305300
(ns.unwrap_or(expected_ns) == expected_ns).then(|| DocLinkDef::ModuleDef(def))
306301
}
307302

308-
fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
303+
fn modpath_from_str(link: &str) -> Option<ModPath> {
309304
// FIXME: this is not how we should get a mod path here.
310305
let try_get_modpath = |link: &str| {
311-
let ast_path = ast::SourceFile::parse(&format!("type T = {link};"))
312-
.syntax_node()
313-
.descendants()
314-
.find_map(ast::Path::cast)?;
315-
if ast_path.syntax().text() != link {
316-
return None;
317-
}
318-
ModPath::from_src(
319-
db.upcast(),
320-
ast_path,
321-
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::BOGUS)),
322-
)
306+
let mut parts = link.split("::");
307+
let mut first_segment = None;
308+
let kind = match parts.next()? {
309+
"" => PathKind::Abs,
310+
"crate" => PathKind::Crate,
311+
"self" => PathKind::Super(0),
312+
"super" => {
313+
let mut deg = 1;
314+
while let Some(segment) = parts.next() {
315+
if segment == "super" {
316+
deg += 1;
317+
} else {
318+
first_segment = Some(segment);
319+
break;
320+
}
321+
}
322+
PathKind::Super(deg)
323+
}
324+
segment => {
325+
first_segment = Some(segment);
326+
PathKind::Plain
327+
}
328+
};
329+
let parts = first_segment.into_iter().chain(parts).map(|segment| match segment.parse() {
330+
Ok(idx) => Name::new_tuple_field(idx),
331+
Err(_) => Name::new_text_dont_use(segment.into()),
332+
});
333+
Some(ModPath::from_segments(kind, parts))
323334
};
324-
325-
let full = try_get_modpath(link);
326-
if full.is_some() {
327-
return full;
328-
}
329-
330-
// Tuple field names cannot be a part of `ModPath` usually, but rustdoc can
331-
// resolve doc paths like `TupleStruct::0`.
332-
// FIXME: Find a better way to handle these.
333-
let (base, maybe_tuple_field) = link.rsplit_once("::")?;
334-
let tuple_field = Name::new_tuple_field(maybe_tuple_field.parse().ok()?);
335-
let mut modpath = try_get_modpath(base)?;
336-
modpath.push_segment(tuple_field);
337-
Some(modpath)
335+
try_get_modpath(link)
338336
}

crates/proc-macro-srv/src/server/token_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl server::TokenStream for TokenIdServer {
206206
stream: if subtree.token_trees.is_empty() {
207207
None
208208
} else {
209-
Some(subtree.token_trees)
209+
Some(TokenStream { token_trees: subtree.token_trees })
210210
},
211211
span: bridge::DelimSpan::from_single(subtree.delimiter.open),
212212
}),

crates/span/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use std::fmt::{self, Write};
33

44
use salsa::InternId;
5-
use stdx::assert_eq_size;
65

76
mod map;
87

@@ -36,7 +35,6 @@ pub const FIXUP_ERASED_FILE_AST_ID_MARKER: ErasedFileAstId =
3635
la_arena::Idx::from_raw(la_arena::RawIdx::from_u32(!0 - 1));
3736

3837
pub type Span = SpanData<SyntaxContextId>;
39-
assert_eq_size!(Span, 20);
4038

4139
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
4240
pub struct SpanData<Ctx> {

crates/stdx/src/macros.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,3 @@ macro_rules! impl_from {
5959
)*
6060
}
6161
}
62-
63-
#[macro_export]
64-
macro_rules! assert_eq_size {
65-
($($ty:ty,)+ $val:expr $(,)?) => {
66-
const _: () = {
67-
$(core::mem::transmute::<[u8; $val], $ty>;)+
68-
};
69-
};
70-
}

0 commit comments

Comments
 (0)