Skip to content

Commit 311a5e9

Browse files
committed
Auto merge of #15986 - davidbarsky:david/move-to-arc-iter, r=lnicola
internal: move to `Arc::from_iter` Builds atop of #15985, will rebase.
2 parents 7e8a339 + c17dcc8 commit 311a5e9

File tree

7 files changed

+117
-163
lines changed

7 files changed

+117
-163
lines changed

crates/hir-expand/src/attrs.rs

+54-69
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,18 @@ impl RawAttrs {
4444
owner: &dyn ast::HasAttrs,
4545
span_map: SpanMapRef<'_>,
4646
) -> Self {
47-
let entries = collect_attrs(owner)
48-
.filter_map(|(id, attr)| match attr {
49-
Either::Left(attr) => {
50-
attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id))
51-
}
52-
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
53-
id,
54-
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
55-
path: Interned::new(ModPath::from(crate::name!(doc))),
56-
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
57-
}),
58-
})
59-
.collect::<Vec<_>>();
60-
// FIXME: use `Arc::from_iter` when it becomes available
61-
let entries: Arc<[Attr]> = Arc::from(entries);
47+
let entries = collect_attrs(owner).filter_map(|(id, attr)| match attr {
48+
Either::Left(attr) => {
49+
attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id))
50+
}
51+
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
52+
id,
53+
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
54+
path: Interned::new(ModPath::from(crate::name!(doc))),
55+
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
56+
}),
57+
});
58+
let entries: Arc<[Attr]> = Arc::from_iter(entries);
6259

6360
Self { entries: if entries.is_empty() { None } else { Some(entries) } }
6461
}
@@ -79,19 +76,13 @@ impl RawAttrs {
7976
(Some(a), Some(b)) => {
8077
let last_ast_index = a.last().map_or(0, |it| it.id.ast_index() + 1) as u32;
8178
Self {
82-
entries: Some(Arc::from(
83-
a.iter()
84-
.cloned()
85-
.chain(b.iter().map(|it| {
86-
let mut it = it.clone();
87-
it.id.id = it.id.ast_index() as u32 + last_ast_index
88-
| (it.id.cfg_attr_index().unwrap_or(0) as u32)
89-
<< AttrId::AST_INDEX_BITS;
90-
it
91-
}))
92-
// FIXME: use `Arc::from_iter` when it becomes available
93-
.collect::<Vec<_>>(),
94-
)),
79+
entries: Some(Arc::from_iter(a.iter().cloned().chain(b.iter().map(|it| {
80+
let mut it = it.clone();
81+
it.id.id = it.id.ast_index() as u32 + last_ast_index
82+
| (it.id.cfg_attr_index().unwrap_or(0) as u32)
83+
<< AttrId::AST_INDEX_BITS;
84+
it
85+
})))),
9586
}
9687
}
9788
}
@@ -108,49 +99,43 @@ impl RawAttrs {
10899
}
109100

110101
let crate_graph = db.crate_graph();
111-
let new_attrs = Arc::from(
112-
self.iter()
113-
.flat_map(|attr| -> SmallVec<[_; 1]> {
114-
let is_cfg_attr =
115-
attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]);
116-
if !is_cfg_attr {
117-
return smallvec![attr.clone()];
118-
}
119-
120-
let subtree = match attr.token_tree_value() {
121-
Some(it) => it,
122-
_ => return smallvec![attr.clone()],
123-
};
102+
let new_attrs = Arc::from_iter(self.iter().flat_map(|attr| -> SmallVec<[_; 1]> {
103+
let is_cfg_attr =
104+
attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr]);
105+
if !is_cfg_attr {
106+
return smallvec![attr.clone()];
107+
}
124108

125-
let (cfg, parts) = match parse_cfg_attr_input(subtree) {
126-
Some(it) => it,
127-
None => return smallvec![attr.clone()],
109+
let subtree = match attr.token_tree_value() {
110+
Some(it) => it,
111+
_ => return smallvec![attr.clone()],
112+
};
113+
114+
let (cfg, parts) = match parse_cfg_attr_input(subtree) {
115+
Some(it) => it,
116+
None => return smallvec![attr.clone()],
117+
};
118+
let index = attr.id;
119+
let attrs =
120+
parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(|(idx, attr)| {
121+
let tree = Subtree {
122+
delimiter: tt::Delimiter::dummy_invisible(),
123+
token_trees: attr.to_vec(),
128124
};
129-
let index = attr.id;
130-
let attrs = parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(
131-
|(idx, attr)| {
132-
let tree = Subtree {
133-
delimiter: tt::Delimiter::dummy_invisible(),
134-
token_trees: attr.to_vec(),
135-
};
136-
Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
137-
},
138-
);
139-
140-
let cfg_options = &crate_graph[krate].cfg_options;
141-
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
142-
let cfg = CfgExpr::parse(&cfg);
143-
if cfg_options.check(&cfg) == Some(false) {
144-
smallvec![]
145-
} else {
146-
cov_mark::hit!(cfg_attr_active);
147-
148-
attrs.collect()
149-
}
150-
})
151-
// FIXME: use `Arc::from_iter` when it becomes available
152-
.collect::<Vec<_>>(),
153-
);
125+
Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
126+
});
127+
128+
let cfg_options = &crate_graph[krate].cfg_options;
129+
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
130+
let cfg = CfgExpr::parse(&cfg);
131+
if cfg_options.check(&cfg) == Some(false) {
132+
smallvec![]
133+
} else {
134+
cov_mark::hit!(cfg_attr_active);
135+
136+
attrs.collect()
137+
}
138+
}));
154139

155140
RawAttrs { entries: Some(new_attrs) }
156141
}

crates/hir-ty/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ impl CallableSig {
322322
pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
323323
CallableSig {
324324
// FIXME: what to do about lifetime params? -> return PolyFnSig
325-
// FIXME: use `Arc::from_iter` when it becomes available
326-
params_and_return: Arc::from(
325+
params_and_return: Arc::from_iter(
327326
fn_ptr
328327
.substitution
329328
.clone()
@@ -332,8 +331,7 @@ impl CallableSig {
332331
.0
333332
.as_slice(Interner)
334333
.iter()
335-
.map(|arg| arg.assert_ty_ref(Interner).clone())
336-
.collect::<Vec<_>>(),
334+
.map(|arg| arg.assert_ty_ref(Interner).clone()),
337335
),
338336
is_varargs: fn_ptr.sig.variadic,
339337
safety: fn_ptr.sig.safety,

crates/hir-ty/src/lower.rs

+37-53
Original file line numberDiff line numberDiff line change
@@ -1459,8 +1459,7 @@ pub(crate) fn generic_predicates_for_param_recover(
14591459
_param_id: &TypeOrConstParamId,
14601460
_assoc_name: &Option<Name>,
14611461
) -> Arc<[Binders<QuantifiedWhereClause>]> {
1462-
// FIXME: use `Arc::from_iter` when it becomes available
1463-
Arc::from(vec![])
1462+
Arc::from_iter(None)
14641463
}
14651464

14661465
pub(crate) fn trait_environment_for_body_query(
@@ -1603,44 +1602,35 @@ pub(crate) fn generic_defaults_query(
16031602
let generic_params = generics(db.upcast(), def);
16041603
let parent_start_idx = generic_params.len_self();
16051604

1606-
let defaults = Arc::from(
1607-
generic_params
1608-
.iter()
1609-
.enumerate()
1610-
.map(|(idx, (id, p))| {
1611-
match p {
1612-
TypeOrConstParamData::TypeParamData(p) => {
1613-
let mut ty = p
1614-
.default
1615-
.as_ref()
1616-
.map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t));
1617-
// Each default can only refer to previous parameters.
1618-
// Type variable default referring to parameter coming
1619-
// after it is forbidden (FIXME: report diagnostic)
1620-
ty = fallback_bound_vars(ty, idx, parent_start_idx);
1621-
crate::make_binders(db, &generic_params, ty.cast(Interner))
1622-
}
1623-
TypeOrConstParamData::ConstParamData(p) => {
1624-
let mut val = p.default.as_ref().map_or_else(
1625-
|| {
1626-
unknown_const_as_generic(
1627-
db.const_param_ty(ConstParamId::from_unchecked(id)),
1628-
)
1629-
},
1630-
|c| {
1631-
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
1632-
c.cast(Interner)
1633-
},
1634-
);
1635-
// Each default can only refer to previous parameters, see above.
1636-
val = fallback_bound_vars(val, idx, parent_start_idx);
1637-
make_binders(db, &generic_params, val)
1638-
}
1639-
}
1640-
})
1641-
// FIXME: use `Arc::from_iter` when it becomes available
1642-
.collect::<Vec<_>>(),
1643-
);
1605+
let defaults = Arc::from_iter(generic_params.iter().enumerate().map(|(idx, (id, p))| {
1606+
match p {
1607+
TypeOrConstParamData::TypeParamData(p) => {
1608+
let mut ty =
1609+
p.default.as_ref().map_or(TyKind::Error.intern(Interner), |t| ctx.lower_ty(t));
1610+
// Each default can only refer to previous parameters.
1611+
// Type variable default referring to parameter coming
1612+
// after it is forbidden (FIXME: report diagnostic)
1613+
ty = fallback_bound_vars(ty, idx, parent_start_idx);
1614+
crate::make_binders(db, &generic_params, ty.cast(Interner))
1615+
}
1616+
TypeOrConstParamData::ConstParamData(p) => {
1617+
let mut val = p.default.as_ref().map_or_else(
1618+
|| {
1619+
unknown_const_as_generic(
1620+
db.const_param_ty(ConstParamId::from_unchecked(id)),
1621+
)
1622+
},
1623+
|c| {
1624+
let c = ctx.lower_const(c, ctx.lower_ty(&p.ty));
1625+
c.cast(Interner)
1626+
},
1627+
);
1628+
// Each default can only refer to previous parameters, see above.
1629+
val = fallback_bound_vars(val, idx, parent_start_idx);
1630+
make_binders(db, &generic_params, val)
1631+
}
1632+
}
1633+
}));
16441634

16451635
defaults
16461636
}
@@ -1653,19 +1643,13 @@ pub(crate) fn generic_defaults_recover(
16531643
let generic_params = generics(db.upcast(), *def);
16541644
// FIXME: this code is not covered in tests.
16551645
// we still need one default per parameter
1656-
let defaults = Arc::from(
1657-
generic_params
1658-
.iter_id()
1659-
.map(|id| {
1660-
let val = match id {
1661-
Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner),
1662-
Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)),
1663-
};
1664-
crate::make_binders(db, &generic_params, val)
1665-
})
1666-
// FIXME: use `Arc::from_iter` when it becomes available
1667-
.collect::<Vec<_>>(),
1668-
);
1646+
let defaults = Arc::from_iter(generic_params.iter_id().map(|id| {
1647+
let val = match id {
1648+
Either::Left(_) => TyKind::Error.intern(Interner).cast(Interner),
1649+
Either::Right(id) => unknown_const_as_generic(db.const_param_ty(id)),
1650+
};
1651+
crate::make_binders(db, &generic_params, val)
1652+
}));
16691653

16701654
defaults
16711655
}

crates/hir-ty/src/method_resolution.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,9 @@ impl TraitImpls {
168168
) -> Arc<[Arc<Self>]> {
169169
let _p = profile::span("trait_impls_in_deps_query").detail(|| format!("{krate:?}"));
170170
let crate_graph = db.crate_graph();
171-
// FIXME: use `Arc::from_iter` when it becomes available
172-
Arc::from(
173-
crate_graph
174-
.transitive_deps(krate)
175-
.map(|krate| db.trait_impls_in_crate(krate))
176-
.collect::<Vec<_>>(),
171+
172+
Arc::from_iter(
173+
crate_graph.transitive_deps(krate).map(|krate| db.trait_impls_in_crate(krate)),
177174
)
178175
}
179176

crates/rust-analyzer/src/global_state.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ impl GlobalState {
187187
config_errors: Default::default(),
188188

189189
proc_macro_changed: false,
190-
// FIXME: use `Arc::from_iter` when it becomes available
191-
proc_macro_clients: Arc::from(Vec::new()),
190+
proc_macro_clients: Arc::from_iter([]),
192191

193-
// FIXME: use `Arc::from_iter` when it becomes available
194-
flycheck: Arc::from(Vec::new()),
192+
flycheck: Arc::from_iter([]),
195193
flycheck_sender,
196194
flycheck_receiver,
197195
last_flycheck_error: None,
@@ -202,7 +200,7 @@ impl GlobalState {
202200
vfs_progress_n_total: 0,
203201
vfs_progress_n_done: 0,
204202

205-
workspaces: Arc::new(Vec::new()),
203+
workspaces: Arc::from(Vec::new()),
206204
crate_graph_file_dependencies: FxHashSet::default(),
207205
fetch_workspaces_queue: OpQueue::default(),
208206
fetch_build_data_queue: OpQueue::default(),

crates/rust-analyzer/src/handlers/request.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,15 @@ use crate::{
5151
};
5252

5353
pub(crate) fn handle_workspace_reload(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
54-
// FIXME: use `Arc::from_iter` when it becomes available
55-
state.proc_macro_clients = Arc::from(Vec::new());
54+
state.proc_macro_clients = Arc::from_iter([]);
5655
state.proc_macro_changed = false;
5756

5857
state.fetch_workspaces_queue.request_op("reload workspace request".to_string(), false);
5958
Ok(())
6059
}
6160

6261
pub(crate) fn handle_proc_macros_rebuild(state: &mut GlobalState, _: ()) -> anyhow::Result<()> {
63-
// FIXME: use `Arc::from_iter` when it becomes available
64-
state.proc_macro_clients = Arc::from(Vec::new());
62+
state.proc_macro_clients = Arc::from_iter([]);
6563
state.proc_macro_changed = false;
6664

6765
state.fetch_build_data_queue.request_op("rebuild proc macros request".to_string(), ());

crates/rust-analyzer/src/reload.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -437,28 +437,22 @@ impl GlobalState {
437437
if self.config.expand_proc_macros() {
438438
tracing::info!("Spawning proc-macro servers");
439439

440-
// FIXME: use `Arc::from_iter` when it becomes available
441-
self.proc_macro_clients = Arc::from(
442-
self.workspaces
443-
.iter()
444-
.map(|ws| {
445-
let path = match self.config.proc_macro_srv() {
446-
Some(path) => path,
447-
None => ws.find_sysroot_proc_macro_srv()?,
448-
};
449-
450-
tracing::info!("Using proc-macro server at {path}");
451-
ProcMacroServer::spawn(path.clone()).map_err(|err| {
452-
tracing::error!(
453-
"Failed to run proc-macro server from path {path}, error: {err:?}",
454-
);
455-
anyhow::format_err!(
456-
"Failed to run proc-macro server from path {path}, error: {err:?}",
457-
)
458-
})
459-
})
460-
.collect::<Vec<_>>(),
461-
)
440+
self.proc_macro_clients = Arc::from_iter(self.workspaces.iter().map(|ws| {
441+
let path = match self.config.proc_macro_srv() {
442+
Some(path) => path,
443+
None => ws.find_sysroot_proc_macro_srv()?,
444+
};
445+
446+
tracing::info!("Using proc-macro server at {path}");
447+
ProcMacroServer::spawn(path.clone()).map_err(|err| {
448+
tracing::error!(
449+
"Failed to run proc-macro server from path {path}, error: {err:?}",
450+
);
451+
anyhow::format_err!(
452+
"Failed to run proc-macro server from path {path}, error: {err:?}",
453+
)
454+
})
455+
}))
462456
};
463457
}
464458

0 commit comments

Comments
 (0)