From c17dcc8d9058ef75c7c52fc474c9d15d2006340d Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 28 Nov 2023 10:36:01 -0500 Subject: [PATCH] internal: switch to `Arc::from_iter` --- crates/hir-expand/src/attrs.rs | 123 ++++++++----------- crates/hir-ty/src/lib.rs | 6 +- crates/hir-ty/src/lower.rs | 90 ++++++-------- crates/hir-ty/src/method_resolution.rs | 9 +- crates/rust-analyzer/src/global_state.rs | 8 +- crates/rust-analyzer/src/handlers/request.rs | 6 +- crates/rust-analyzer/src/reload.rs | 38 +++--- 7 files changed, 117 insertions(+), 163 deletions(-) diff --git a/crates/hir-expand/src/attrs.rs b/crates/hir-expand/src/attrs.rs index edaf2f06a4e8..b8fc30c91189 100644 --- a/crates/hir-expand/src/attrs.rs +++ b/crates/hir-expand/src/attrs.rs @@ -44,21 +44,18 @@ impl RawAttrs { owner: &dyn ast::HasAttrs, span_map: SpanMapRef<'_>, ) -> Self { - let entries = collect_attrs(owner) - .filter_map(|(id, attr)| match attr { - Either::Left(attr) => { - attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id)) - } - Either::Right(comment) => comment.doc_comment().map(|doc| Attr { - id, - input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))), - path: Interned::new(ModPath::from(crate::name!(doc))), - ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx, - }), - }) - .collect::>(); - // FIXME: use `Arc::from_iter` when it becomes available - let entries: Arc<[Attr]> = Arc::from(entries); + let entries = collect_attrs(owner).filter_map(|(id, attr)| match attr { + Either::Left(attr) => { + attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id)) + } + Either::Right(comment) => comment.doc_comment().map(|doc| Attr { + id, + input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))), + path: Interned::new(ModPath::from(crate::name!(doc))), + ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx, + }), + }); + let entries: Arc<[Attr]> = Arc::from_iter(entries); Self { entries: if entries.is_empty() { None } else { Some(entries) } } } @@ -79,19 +76,13 @@ impl RawAttrs { (Some(a), Some(b)) => { let last_ast_index = a.last().map_or(0, |it| it.id.ast_index() + 1) as u32; Self { - entries: Some(Arc::from( - a.iter() - .cloned() - .chain(b.iter().map(|it| { - let mut it = it.clone(); - it.id.id = it.id.ast_index() as u32 + last_ast_index - | (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::>(), - )), + entries: Some(Arc::from_iter(a.iter().cloned().chain(b.iter().map(|it| { + let mut it = it.clone(); + it.id.id = it.id.ast_index() as u32 + last_ast_index + | (it.id.cfg_attr_index().unwrap_or(0) as u32) + << AttrId::AST_INDEX_BITS; + it + })))), } } } @@ -108,49 +99,43 @@ impl RawAttrs { } let crate_graph = db.crate_graph(); - let new_attrs = Arc::from( - self.iter() - .flat_map(|attr| -> SmallVec<[_; 1]> { - let is_cfg_attr = - 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() { - Some(it) => it, - _ => return smallvec![attr.clone()], - }; + let new_attrs = Arc::from_iter(self.iter().flat_map(|attr| -> SmallVec<[_; 1]> { + let is_cfg_attr = + attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]); + if !is_cfg_attr { + return smallvec![attr.clone()]; + } - let (cfg, parts) = match parse_cfg_attr_input(subtree) { - Some(it) => it, - None => return smallvec![attr.clone()], + let subtree = match attr.token_tree_value() { + Some(it) => it, + _ => 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(), }; - 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_options = &crate_graph[krate].cfg_options; - 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::>(), - ); + Attr::from_tt(db, &tree, index.with_cfg_attr(idx)) + }); + + let cfg_options = &crate_graph[krate].cfg_options; + 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() + } + })); RawAttrs { entries: Some(new_attrs) } } diff --git a/crates/hir-ty/src/lib.rs b/crates/hir-ty/src/lib.rs index 33dffafa2600..5a3e423f152a 100644 --- a/crates/hir-ty/src/lib.rs +++ b/crates/hir-ty/src/lib.rs @@ -322,8 +322,7 @@ impl CallableSig { pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig { CallableSig { // FIXME: what to do about lifetime params? -> return PolyFnSig - // FIXME: use `Arc::from_iter` when it becomes available - params_and_return: Arc::from( + params_and_return: Arc::from_iter( fn_ptr .substitution .clone() @@ -332,8 +331,7 @@ impl CallableSig { .0 .as_slice(Interner) .iter() - .map(|arg| arg.assert_ty_ref(Interner).clone()) - .collect::>(), + .map(|arg| arg.assert_ty_ref(Interner).clone()), ), is_varargs: fn_ptr.sig.variadic, safety: fn_ptr.sig.safety, diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs index 5122021d6d2b..30ebd1f92e07 100644 --- a/crates/hir-ty/src/lower.rs +++ b/crates/hir-ty/src/lower.rs @@ -1459,8 +1459,7 @@ pub(crate) fn generic_predicates_for_param_recover( _param_id: &TypeOrConstParamId, _assoc_name: &Option, ) -> Arc<[Binders]> { - // FIXME: use `Arc::from_iter` when it becomes available - Arc::from(vec![]) + Arc::from_iter(None) } 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 parent_start_idx = generic_params.len_self(); - let defaults = Arc::from( - generic_params - .iter() - .enumerate() - .map(|(idx, (id, p))| { - match p { - TypeOrConstParamData::TypeParamData(p) => { - let mut ty = p - .default - .as_ref() - .map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t)); - // Each default can only refer to previous parameters. - // Type variable default referring to parameter coming - // after it is forbidden (FIXME: report diagnostic) - ty = fallback_bound_vars(ty, idx, parent_start_idx); - crate::make_binders(db, &generic_params, ty.cast(Interner)) - } - TypeOrConstParamData::ConstParamData(p) => { - let mut val = p.default.as_ref().map_or_else( - || { - unknown_const_as_generic( - db.const_param_ty(ConstParamId::from_unchecked(id)), - ) - }, - |c| { - let c = ctx.lower_const(c, ctx.lower_ty(&p.ty)); - 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::>(), - ); + let defaults = Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| { + match p { + TypeOrConstParamData::TypeParamData(p) => { + let mut ty = + p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t)); + // Each default can only refer to previous parameters. + // Type variable default referring to parameter coming + // after it is forbidden (FIXME: report diagnostic) + ty = fallback_bound_vars(ty, idx, parent_start_idx); + crate::make_binders(db, &generic_params, ty.cast(Interner)) + } + TypeOrConstParamData::ConstParamData(p) => { + let mut val = p.default.as_ref().map_or_else( + || { + unknown_const_as_generic( + db.const_param_ty(ConstParamId::from_unchecked(id)), + ) + }, + |c| { + let c = ctx.lower_const(c, ctx.lower_ty(&p.ty)); + 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) + } + } + })); defaults } @@ -1653,19 +1643,13 @@ pub(crate) fn generic_defaults_recover( let generic_params = generics(db.upcast(), *def); // FIXME: this code is not covered in tests. // we still need one default per parameter - let defaults = Arc::from( - generic_params - .iter_id() - .map(|id| { - let val = match id { - Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner), - 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::>(), - ); + let defaults = Arc::from_iter(generic_params.iter_id().map(|id| { + let val = match id { + Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner), + Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)), + }; + crate::make_binders(db, &generic_params, val) + })); defaults } diff --git a/crates/hir-ty/src/method_resolution.rs b/crates/hir-ty/src/method_resolution.rs index 732643566a2d..041d61c1b153 100644 --- a/crates/hir-ty/src/method_resolution.rs +++ b/crates/hir-ty/src/method_resolution.rs @@ -168,12 +168,9 @@ impl TraitImpls { ) -> Arc<[Arc]> { let _p = profile::span("trait_impls_in_deps_query").detail(|| format!("{krate:?}")); let crate_graph = db.crate_graph(); - // FIXME: use `Arc::from_iter` when it becomes available - Arc::from( - crate_graph - .transitive_deps(krate) - .map(|krate| db.trait_impls_in_crate(krate)) - .collect::>(), + + Arc::from_iter( + crate_graph.transitive_deps(krate).map(|krate| db.trait_impls_in_crate(krate)), ) } diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index c09f57252ce9..0f31fe16054a 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -187,11 +187,9 @@ impl GlobalState { config_errors: Default::default(), proc_macro_changed: false, - // FIXME: use `Arc::from_iter` when it becomes available - proc_macro_clients: Arc::from(Vec::new()), + proc_macro_clients: Arc::from_iter([]), - // FIXME: use `Arc::from_iter` when it becomes available - flycheck: Arc::from(Vec::new()), + flycheck: Arc::from_iter([]), flycheck_sender, flycheck_receiver, last_flycheck_error: None, @@ -202,7 +200,7 @@ impl GlobalState { vfs_progress_n_total: 0, vfs_progress_n_done: 0, - workspaces: Arc::new(Vec::new()), + workspaces: Arc::from(Vec::new()), crate_graph_file_dependencies: FxHashSet::default(), fetch_workspaces_queue: OpQueue::default(), fetch_build_data_queue: OpQueue::default(), diff --git a/crates/rust-analyzer/src/handlers/request.rs b/crates/rust-analyzer/src/handlers/request.rs index 49c88702faad..57955ebf897e 100644 --- a/crates/rust-analyzer/src/handlers/request.rs +++ b/crates/rust-analyzer/src/handlers/request.rs @@ -51,8 +51,7 @@ use crate::{ }; 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(Vec::new()); + state.proc_macro_clients = Arc::from_iter([]); state.proc_macro_changed = 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<()> { - // FIXME: use `Arc::from_iter` when it becomes available - state.proc_macro_clients = Arc::from(Vec::new()); + state.proc_macro_clients = Arc::from_iter([]); state.proc_macro_changed = false; state.fetch_build_data_queue.request_op("rebuild proc macros request".to_string(), ()); diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index abe2191f4002..7ab528f49751 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -437,28 +437,22 @@ impl GlobalState { if self.config.expand_proc_macros() { tracing::info!("Spawning proc-macro servers"); - // FIXME: use `Arc::from_iter` when it becomes available - self.proc_macro_clients = Arc::from( - self.workspaces - .iter() - .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}"); - ProcMacroServer::spawn(path.clone()).map_err(|err| { - tracing::error!( - "Failed to run proc-macro server from path {path}, error: {err:?}", - ); - anyhow::format_err!( - "Failed to run proc-macro server from path {path}, error: {err:?}", - ) - }) - }) - .collect::>(), - ) + self.proc_macro_clients = Arc::from_iter(self.workspaces.iter().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}"); + ProcMacroServer::spawn(path.clone()).map_err(|err| { + tracing::error!( + "Failed to run proc-macro server from path {path}, error: {err:?}", + ); + anyhow::format_err!( + "Failed to run proc-macro server from path {path}, error: {err:?}", + ) + }) + })) }; }