internal: switch to Arc::from_iter

This commit is contained in:
David Barsky 2023-11-28 10:36:01 -05:00
parent 7e8a3391bb
commit c17dcc8d90
7 changed files with 114 additions and 160 deletions

View File

@ -44,21 +44,18 @@ impl RawAttrs {
owner: &dyn ast::HasAttrs, owner: &dyn ast::HasAttrs,
span_map: SpanMapRef<'_>, span_map: SpanMapRef<'_>,
) -> Self { ) -> Self {
let entries = collect_attrs(owner) let entries = collect_attrs(owner).filter_map(|(id, attr)| match attr {
.filter_map(|(id, attr)| match attr { Either::Left(attr) => {
Either::Left(attr) => { attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id))
attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id)) }
} Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
Either::Right(comment) => comment.doc_comment().map(|doc| Attr { id,
id, input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))), path: Interned::new(ModPath::from(crate::name!(doc))),
path: Interned::new(ModPath::from(crate::name!(doc))), ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx, }),
}), });
}) let entries: Arc<[Attr]> = Arc::from_iter(entries);
.collect::<Vec<_>>();
// FIXME: use `Arc::from_iter` when it becomes available
let entries: Arc<[Attr]> = Arc::from(entries);
Self { entries: if entries.is_empty() { None } else { Some(entries) } } Self { entries: if entries.is_empty() { None } else { Some(entries) } }
} }
@ -79,19 +76,13 @@ impl RawAttrs {
(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.last().map_or(0, |it| it.id.ast_index() + 1) as u32;
Self { Self {
entries: Some(Arc::from( entries: Some(Arc::from_iter(a.iter().cloned().chain(b.iter().map(|it| {
a.iter() let mut it = it.clone();
.cloned() it.id.id = it.id.ast_index() as u32 + last_ast_index
.chain(b.iter().map(|it| { | (it.id.cfg_attr_index().unwrap_or(0) as u32)
let mut it = it.clone(); << AttrId::AST_INDEX_BITS;
it.id.id = it.id.ast_index() as u32 + last_ast_index it
| (it.id.cfg_attr_index().unwrap_or(0) as u32) })))),
<< AttrId::AST_INDEX_BITS;
it
}))
// FIXME: use `Arc::from_iter` when it becomes available
.collect::<Vec<_>>(),
)),
} }
} }
} }
@ -108,49 +99,43 @@ impl RawAttrs {
} }
let crate_graph = db.crate_graph(); let crate_graph = db.crate_graph();
let new_attrs = Arc::from( let new_attrs = Arc::from_iter(self.iter().flat_map(|attr| -> SmallVec<[_; 1]> {
self.iter() let is_cfg_attr =
.flat_map(|attr| -> SmallVec<[_; 1]> { attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]);
let is_cfg_attr = if !is_cfg_attr {
attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]); return smallvec![attr.clone()];
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) {
Some(it) => it,
None => return smallvec![attr.clone()],
};
let index = attr.id;
let attrs =
parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(|(idx, attr)| {
let tree = Subtree {
delimiter: tt::Delimiter::dummy_invisible(),
token_trees: attr.to_vec(),
}; };
Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
});
let (cfg, parts) = match parse_cfg_attr_input(subtree) { let cfg_options = &crate_graph[krate].cfg_options;
Some(it) => it, let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
None => return smallvec![attr.clone()], let cfg = CfgExpr::parse(&cfg);
}; if cfg_options.check(&cfg) == Some(false) {
let index = attr.id; smallvec![]
let attrs = parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map( } else {
|(idx, attr)| { cov_mark::hit!(cfg_attr_active);
let tree = Subtree {
delimiter: tt::Delimiter::dummy_invisible(),
token_trees: attr.to_vec(),
};
Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
},
);
let cfg_options = &crate_graph[krate].cfg_options; attrs.collect()
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; }
let cfg = CfgExpr::parse(&cfg); }));
if cfg_options.check(&cfg) == Some(false) {
smallvec![]
} else {
cov_mark::hit!(cfg_attr_active);
attrs.collect()
}
})
// FIXME: use `Arc::from_iter` when it becomes available
.collect::<Vec<_>>(),
);
RawAttrs { entries: Some(new_attrs) } RawAttrs { entries: Some(new_attrs) }
} }

View File

@ -322,8 +322,7 @@ impl CallableSig {
pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig { pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
CallableSig { CallableSig {
// FIXME: what to do about lifetime params? -> return PolyFnSig // FIXME: what to do about lifetime params? -> return PolyFnSig
// FIXME: use `Arc::from_iter` when it becomes available params_and_return: Arc::from_iter(
params_and_return: Arc::from(
fn_ptr fn_ptr
.substitution .substitution
.clone() .clone()
@ -332,8 +331,7 @@ impl CallableSig {
.0 .0
.as_slice(Interner) .as_slice(Interner)
.iter() .iter()
.map(|arg| arg.assert_ty_ref(Interner).clone()) .map(|arg| arg.assert_ty_ref(Interner).clone()),
.collect::<Vec<_>>(),
), ),
is_varargs: fn_ptr.sig.variadic, is_varargs: fn_ptr.sig.variadic,
safety: fn_ptr.sig.safety, safety: fn_ptr.sig.safety,

View File

@ -1459,8 +1459,7 @@ pub(crate) fn generic_predicates_for_param_recover(
_param_id: &TypeOrConstParamId, _param_id: &TypeOrConstParamId,
_assoc_name: &Option<Name>, _assoc_name: &Option<Name>,
) -> Arc<[Binders<QuantifiedWhereClause>]> { ) -> Arc<[Binders<QuantifiedWhereClause>]> {
// FIXME: use `Arc::from_iter` when it becomes available Arc::from_iter(None)
Arc::from(vec![])
} }
pub(crate) fn trait_environment_for_body_query( pub(crate) fn trait_environment_for_body_query(
@ -1603,44 +1602,35 @@ pub(crate) fn generic_defaults_query(
let generic_params = generics(db.upcast(), def); let generic_params = generics(db.upcast(), def);
let parent_start_idx = generic_params.len_self(); let parent_start_idx = generic_params.len_self();
let defaults = Arc::from( let defaults = Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| {
generic_params match p {
.iter() TypeOrConstParamData::TypeParamData(p) => {
.enumerate() let mut ty =
.map(|(idx, (id, p))| { p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t));
match p { // Each default can only refer to previous parameters.
TypeOrConstParamData::TypeParamData(p) => { // Type variable default referring to parameter coming
let mut ty = p // after it is forbidden (FIXME: report diagnostic)
.default ty = fallback_bound_vars(ty, idx, parent_start_idx);
.as_ref() crate::make_binders(db, &generic_params, ty.cast(Interner))
.map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t)); }
// Each default can only refer to previous parameters. TypeOrConstParamData::ConstParamData(p) => {
// Type variable default referring to parameter coming let mut val = p.default.as_ref().map_or_else(
// after it is forbidden (FIXME: report diagnostic) || {
ty = fallback_bound_vars(ty, idx, parent_start_idx); unknown_const_as_generic(
crate::make_binders(db, &generic_params, ty.cast(Interner)) db.const_param_ty(ConstParamId::from_unchecked(id)),
} )
TypeOrConstParamData::ConstParamData(p) => { },
let mut val = p.default.as_ref().map_or_else( |c| {
|| { let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
unknown_const_as_generic( c.cast(Interner)
db.const_param_ty(ConstParamId::from_unchecked(id)), },
) );
}, // Each default can only refer to previous parameters, see above.
|c| { val = fallback_bound_vars(val, idx, parent_start_idx);
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty)); make_binders(db, &generic_params, val)
c.cast(Interner) }
}, }
); }));
// Each default can only refer to previous parameters, see above.
val = fallback_bound_vars(val, idx, parent_start_idx);
make_binders(db, &generic_params, val)
}
}
})
// FIXME: use `Arc::from_iter` when it becomes available
.collect::<Vec<_>>(),
);
defaults defaults
} }
@ -1653,19 +1643,13 @@ pub(crate) fn generic_defaults_recover(
let generic_params = generics(db.upcast(), *def); let generic_params = generics(db.upcast(), *def);
// FIXME: this code is not covered in tests. // FIXME: this code is not covered in tests.
// we still need one default per parameter // we still need one default per parameter
let defaults = Arc::from( let defaults = Arc::from_iter(generic_params.iter_id().map(|id| {
generic_params let val = match id {
.iter_id() Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner),
.map(|id| { Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)),
let val = match id { };
Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner), crate::make_binders(db, &generic_params, val)
Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)), }));
};
crate::make_binders(db, &generic_params, val)
})
// FIXME: use `Arc::from_iter` when it becomes available
.collect::<Vec<_>>(),
);
defaults defaults
} }

View File

@ -168,12 +168,9 @@ impl TraitImpls {
) -> Arc<[Arc<Self>]> { ) -> Arc<[Arc<Self>]> {
let _p = profile::span("trait_impls_in_deps_query").detail(|| format!("{krate:?}")); let _p = profile::span("trait_impls_in_deps_query").detail(|| format!("{krate:?}"));
let crate_graph = db.crate_graph(); let crate_graph = db.crate_graph();
// FIXME: use `Arc::from_iter` when it becomes available
Arc::from( Arc::from_iter(
crate_graph crate_graph.transitive_deps(krate).map(|krate| db.trait_impls_in_crate(krate)),
.transitive_deps(krate)
.map(|krate| db.trait_impls_in_crate(krate))
.collect::<Vec<_>>(),
) )
} }

View File

@ -187,11 +187,9 @@ impl GlobalState {
config_errors: Default::default(), config_errors: Default::default(),
proc_macro_changed: false, proc_macro_changed: false,
// FIXME: use `Arc::from_iter` when it becomes available proc_macro_clients: Arc::from_iter([]),
proc_macro_clients: Arc::from(Vec::new()),
// FIXME: use `Arc::from_iter` when it becomes available flycheck: Arc::from_iter([]),
flycheck: Arc::from(Vec::new()),
flycheck_sender, flycheck_sender,
flycheck_receiver, flycheck_receiver,
last_flycheck_error: None, last_flycheck_error: None,
@ -202,7 +200,7 @@ impl GlobalState {
vfs_progress_n_total: 0, vfs_progress_n_total: 0,
vfs_progress_n_done: 0, vfs_progress_n_done: 0,
workspaces: Arc::new(Vec::new()), workspaces: Arc::from(Vec::new()),
crate_graph_file_dependencies: FxHashSet::default(), crate_graph_file_dependencies: FxHashSet::default(),
fetch_workspaces_queue: OpQueue::default(), fetch_workspaces_queue: OpQueue::default(),
fetch_build_data_queue: OpQueue::default(), fetch_build_data_queue: OpQueue::default(),

View File

@ -51,8 +51,7 @@ use crate::{
}; };
pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow::Result<()> { pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
// FIXME: use `Arc::from_iter` when it becomes available state.proc_macro_clients = Arc::from_iter([]);
state.proc_macro_clients = Arc::from(Vec::new());
state.proc_macro_changed = false; state.proc_macro_changed = false;
state.fetch_workspaces_queue.request_op("reload workspace request".to_string(), false); state.fetch_workspaces_queue.request_op("reload workspace request".to_string(), false);
@ -60,8 +59,7 @@ pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow:
} }
pub(crate) fn handle_proc_macros_rebuild(state: &mut GlobalState, _: ()) -> anyhow::Result<()> { pub(crate) fn handle_proc_macros_rebuild(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
// FIXME: use `Arc::from_iter` when it becomes available state.proc_macro_clients = Arc::from_iter([]);
state.proc_macro_clients = Arc::from(Vec::new());
state.proc_macro_changed = false; state.proc_macro_changed = false;
state.fetch_build_data_queue.request_op("rebuild proc macros request".to_string(), ()); state.fetch_build_data_queue.request_op("rebuild proc macros request".to_string(), ());

View File

@ -437,28 +437,22 @@ impl GlobalState {
if self.config.expand_proc_macros() { if self.config.expand_proc_macros() {
tracing::info!("Spawning proc-macro servers"); tracing::info!("Spawning proc-macro servers");
// FIXME: use `Arc::from_iter` when it becomes available self.proc_macro_clients = Arc::from_iter(self.workspaces.iter().map(|ws| {
self.proc_macro_clients = Arc::from( let path = match self.config.proc_macro_srv() {
self.workspaces Some(path) => path,
.iter() None => ws.find_sysroot_proc_macro_srv()?,
.map(|ws| { };
let path = match self.config.proc_macro_srv() {
Some(path) => path,
None => ws.find_sysroot_proc_macro_srv()?,
};
tracing::info!("Using proc-macro server at {path}"); tracing::info!("Using proc-macro server at {path}");
ProcMacroServer::spawn(path.clone()).map_err(|err| { ProcMacroServer::spawn(path.clone()).map_err(|err| {
tracing::error!( tracing::error!(
"Failed to run proc-macro server from path {path}, error: {err:?}", "Failed to run proc-macro server from path {path}, error: {err:?}",
); );
anyhow::format_err!( anyhow::format_err!(
"Failed to run proc-macro server from path {path}, error: {err:?}", "Failed to run proc-macro server from path {path}, error: {err:?}",
) )
}) })
}) }))
.collect::<Vec<_>>(),
)
}; };
} }