Skip to content

Commit

Permalink
Don't parse intra doc links as syntax trees
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 26, 2024
1 parent 5a34341 commit ffb25c1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 55 deletions.
2 changes: 0 additions & 2 deletions crates/hir-def/src/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::iter;

use hir_expand::{span_map::SpanMapRef, InFile};
use la_arena::ArenaMap;
use stdx::assert_eq_size;
use syntax::ast;
use triomphe::Arc;

Expand All @@ -25,7 +24,6 @@ pub enum RawVisibility {
/// `pub`.
Public,
}
assert_eq_size!(RawVisibility, 48);

impl RawVisibility {
pub(crate) const fn private() -> RawVisibility {
Expand Down
3 changes: 0 additions & 3 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub mod span_map;
mod fixup;

use attrs::collect_attrs;
use stdx::assert_eq_size;
use triomphe::Arc;

use std::{fmt, hash::Hash};
Expand Down Expand Up @@ -176,7 +175,6 @@ pub struct MacroCallLoc {
pub kind: MacroCallKind,
pub call_site: Span,
}
assert_eq_size!(MacroCallLoc, 104);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
Expand All @@ -187,7 +185,6 @@ pub struct MacroDefId {
pub allow_internal_unsafe: bool,
pub span: Span,
}
assert_eq_size!(MacroDefId, 44);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroDefKind {
Expand Down
2 changes: 0 additions & 2 deletions crates/hir-expand/src/mod_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ use crate::{
use base_db::CrateId;
use smallvec::SmallVec;
use span::SyntaxContextId;
use stdx::assert_eq_size;
use syntax::{ast, AstNode};

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

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UnescapedModPath<'a>(&'a ModPath);
Expand Down
2 changes: 0 additions & 2 deletions crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::fmt;

use stdx::assert_eq_size;
use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};

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

/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
Expand Down
66 changes: 32 additions & 34 deletions crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::ops::ControlFlow;

use base_db::FileId;
use hir_def::{
attr::AttrsWithOwner,
item_scope::ItemInNs,
Expand All @@ -11,12 +10,8 @@ use hir_def::{
resolver::{HasResolver, Resolver, TypeNs},
AssocItemId, AttrDefId, ModuleDefId,
};
use hir_expand::{
name::Name,
span_map::{RealSpanMap, SpanMapRef},
};
use hir_expand::{mod_path::PathKind, name::Name};
use hir_ty::{db::HirDatabase, method_resolution};
use syntax::{ast, AstNode};

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

let mut modpath = modpath_from_str(db, link)?;
let mut modpath = modpath_from_str(link)?;

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

fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
fn 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 ast_path = ast::SourceFile::parse(&format!("type T = {link};"))
.syntax_node()
.descendants()
.find_map(ast::Path::cast)?;
if ast_path.syntax().text() != link {
return None;
}
ModPath::from_src(
db.upcast(),
ast_path,
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::BOGUS)),
)
let mut parts = link.split("::");
let mut first_segment = None;
let kind = match parts.next()? {
"" => PathKind::Abs,
"crate" => PathKind::Crate,
"self" => PathKind::Super(0),
"super" => {
let mut deg = 1;
while let Some(segment) = parts.next() {
if segment == "super" {
deg += 1;
} else {
first_segment = Some(segment);
break;
}
}
PathKind::Super(deg)
}
segment => {
first_segment = Some(segment);
PathKind::Plain
}
};
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()),
});
Some(ModPath::from_segments(kind, parts))
};

let full = try_get_modpath(link);
if full.is_some() {
return full;
}

// Tuple field names cannot be a part of `ModPath` usually, but rustdoc can
// resolve doc paths like `TupleStruct::0`.
// FIXME: Find a better way to handle these.
let (base, maybe_tuple_field) = link.rsplit_once("::")?;
let tuple_field = Name::new_tuple_field(maybe_tuple_field.parse().ok()?);
let mut modpath = try_get_modpath(base)?;
modpath.push_segment(tuple_field);
Some(modpath)
try_get_modpath(link)
}
2 changes: 1 addition & 1 deletion crates/proc-macro-srv/src/server/token_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl server::TokenStream for TokenIdServer {
stream: if subtree.token_trees.is_empty() {
None
} else {
Some(subtree.token_trees)
Some(TokenStream { token_trees })
},
span: bridge::DelimSpan::from_single(subtree.delimiter.open),
}),
Expand Down
2 changes: 0 additions & 2 deletions crates/span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::fmt::{self, Write};

use salsa::InternId;
use stdx::assert_eq_size;

mod map;

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

pub type Span = SpanData<SyntaxContextId>;
assert_eq_size!(Span, 20);

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct SpanData<Ctx> {
Expand Down
9 changes: 0 additions & 9 deletions crates/stdx/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,3 @@ macro_rules! impl_from {
)*
}
}

#[macro_export]
macro_rules! assert_eq_size {
($($ty:ty,)+ $val:expr $(,)?) => {
const _: () = {
$(core::mem::transmute::<[u8; $val], $ty>;)+
};
};
}

0 comments on commit ffb25c1

Please sign in to comment.