Make use of ThinArc in RawAttrs

This commit is contained in:
Lukas Wirth 2024-03-21 11:49:09 +01:00
parent 928d847cc2
commit 0036762b9d
2 changed files with 79 additions and 59 deletions

View file

@ -9,7 +9,7 @@ use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use span::{Span, SyntaxContextId}; use span::{Span, SyntaxContextId};
use syntax::{ast, format_smolstr, match_ast, AstNode, AstToken, SmolStr, SyntaxNode}; use syntax::{ast, format_smolstr, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
use triomphe::Arc; use triomphe::ThinArc;
use crate::{ use crate::{
db::ExpandDatabase, db::ExpandDatabase,
@ -22,8 +22,7 @@ use crate::{
/// Syntactical attributes, without filtering of `cfg_attr`s. /// Syntactical attributes, without filtering of `cfg_attr`s.
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct RawAttrs { pub struct RawAttrs {
// FIXME: Make this a ThinArc entries: Option<ThinArc<(), Attr>>,
entries: Option<Arc<[Attr]>>,
} }
impl ops::Deref for RawAttrs { impl ops::Deref for RawAttrs {
@ -31,7 +30,7 @@ impl ops::Deref for RawAttrs {
fn deref(&self) -> &[Attr] { fn deref(&self) -> &[Attr] {
match &self.entries { match &self.entries {
Some(it) => it, Some(it) => &it.slice,
None => &[], None => &[],
} }
} }
@ -45,27 +44,34 @@ impl RawAttrs {
owner: &dyn ast::HasAttrs, owner: &dyn ast::HasAttrs,
span_map: SpanMapRef<'_>, span_map: SpanMapRef<'_>,
) -> Self { ) -> Self {
let entries = collect_attrs(owner).filter_map(|(id, attr)| match attr { let entries: Vec<_> = collect_attrs(owner)
Either::Left(attr) => { .filter_map(|(id, attr)| match attr {
attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id)) Either::Left(attr) => {
} attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id))
Either::Right(comment) => comment.doc_comment().map(|doc| {
let span = span_map.span_for_range(comment.syntax().text_range());
Attr {
id,
input: Some(Interned::new(AttrInput::Literal(tt::Literal {
// FIXME: Escape quotes from comment content
text: SmolStr::new(format_smolstr!("\"{doc}\"",)),
span,
}))),
path: Interned::new(ModPath::from(crate::name!(doc))),
ctxt: span.ctx,
} }
}), Either::Right(comment) => comment.doc_comment().map(|doc| {
}); let span = span_map.span_for_range(comment.syntax().text_range());
let entries: Arc<[Attr]> = Arc::from_iter(entries); Attr {
id,
input: Some(Interned::new(AttrInput::Literal(tt::Literal {
// FIXME: Escape quotes from comment content
text: SmolStr::new(format_smolstr!("\"{doc}\"",)),
span,
}))),
path: Interned::new(ModPath::from(crate::name!(doc))),
ctxt: span.ctx,
}
}),
})
.collect();
Self { entries: if entries.is_empty() { None } else { Some(entries) } } let entries = if entries.is_empty() {
None
} else {
Some(ThinArc::from_header_and_iter((), entries.into_iter()))
};
RawAttrs { entries }
} }
pub fn from_attrs_owner( pub fn from_attrs_owner(
@ -82,16 +88,20 @@ impl RawAttrs {
(None, entries @ Some(_)) => Self { entries }, (None, entries @ Some(_)) => Self { entries },
(Some(entries), None) => Self { entries: Some(entries.clone()) }, (Some(entries), None) => Self { entries: Some(entries.clone()) },
(Some(a), Some(b)) => { (Some(a), Some(b)) => {
let last_ast_index = a.last().map_or(0, |it| it.id.ast_index() + 1) as u32; let last_ast_index = a.slice.last().map_or(0, |it| it.id.ast_index() + 1) as u32;
Self { let items = a
entries: Some(Arc::from_iter(a.iter().cloned().chain(b.iter().map(|it| { .slice
.iter()
.cloned()
.chain(b.slice.iter().map(|it| {
let mut it = it.clone(); let mut it = it.clone();
it.id.id = (it.id.ast_index() as u32 + last_ast_index) it.id.id = (it.id.ast_index() as u32 + last_ast_index)
| (it.id.cfg_attr_index().unwrap_or(0) as u32) | (it.id.cfg_attr_index().unwrap_or(0) as u32)
<< AttrId::AST_INDEX_BITS; << AttrId::AST_INDEX_BITS;
it it
})))), }))
} .collect::<Vec<_>>();
Self { entries: Some(ThinArc::from_header_and_iter((), items.into_iter())) }
} }
} }
} }
@ -107,41 +117,47 @@ impl RawAttrs {
} }
let crate_graph = db.crate_graph(); let crate_graph = db.crate_graph();
let new_attrs = Arc::from_iter(self.iter().flat_map(|attr| -> SmallVec<[_; 1]> { let new_attrs =
let is_cfg_attr = self.iter()
attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]); .flat_map(|attr| -> SmallVec<[_; 1]> {
if !is_cfg_attr { let is_cfg_attr =
return smallvec![attr.clone()]; attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]);
} if !is_cfg_attr {
return smallvec![attr.clone()];
}
let subtree = match attr.token_tree_value() { let subtree = match attr.token_tree_value() {
Some(it) => it, Some(it) => it,
_ => return smallvec![attr.clone()], _ => return smallvec![attr.clone()],
}; };
let (cfg, parts) = match parse_cfg_attr_input(subtree) { let (cfg, parts) = match parse_cfg_attr_input(subtree) {
Some(it) => it, Some(it) => it,
None => return smallvec![attr.clone()], None => return smallvec![attr.clone()],
}; };
let index = attr.id; let index = attr.id;
let attrs = parts let attrs = parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(
.enumerate() |(idx, attr)| Attr::from_tt(db, attr, index.with_cfg_attr(idx)),
.take(1 << AttrId::CFG_ATTR_BITS) );
.filter_map(|(idx, attr)| Attr::from_tt(db, attr, index.with_cfg_attr(idx)));
let cfg_options = &crate_graph[krate].cfg_options; let cfg_options = &crate_graph[krate].cfg_options;
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: Box::from(cfg) }; let cfg = Subtree { delimiter: subtree.delimiter, token_trees: Box::from(cfg) };
let cfg = CfgExpr::parse(&cfg); let cfg = CfgExpr::parse(&cfg);
if cfg_options.check(&cfg) == Some(false) { if cfg_options.check(&cfg) == Some(false) {
smallvec![] smallvec![]
} else { } else {
cov_mark::hit!(cfg_attr_active); cov_mark::hit!(cfg_attr_active);
attrs.collect() attrs.collect()
} }
})); })
.collect::<Vec<_>>();
RawAttrs { entries: Some(new_attrs) } let entries = if new_attrs.is_empty() {
None
} else {
Some(ThinArc::from_header_and_iter((), new_attrs.into_iter()))
};
RawAttrs { entries }
} }
} }

View file

@ -283,6 +283,10 @@ fn test_disabled_diagnostics() {
#[test] #[test]
fn minicore_smoke_test() { fn minicore_smoke_test() {
if test_utils::skip_slow_tests() {
return;
}
fn check(minicore: MiniCore) { fn check(minicore: MiniCore) {
let source = minicore.source_code(); let source = minicore.source_code();
let mut config = DiagnosticsConfig::test_sample(); let mut config = DiagnosticsConfig::test_sample();