Skip to content

Commit 1f81f90

Browse files
committed
Auto merge of #135207 - lnicola:sync-from-ra, r=lnicola
Subtree update of `rust-analyzer` r? `@ghost`
2 parents 0707499 + fd1e955 commit 1f81f90

File tree

274 files changed

+32947
-5399
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

274 files changed

+32947
-5399
lines changed

src/tools/rust-analyzer/.github/workflows/ci.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
pull_request:
77
merge_group:
88

9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
11+
cancel-in-progress: true
12+
913
env:
1014
CARGO_INCREMENTAL: 0
1115
CARGO_NET_RETRY: 10
@@ -103,7 +107,7 @@ jobs:
103107
- name: Run analysis-stats on the rust standard libraries
104108
if: matrix.os == 'ubuntu-latest'
105109
env:
106-
RUSTC_BOOTSTRAP: 1
110+
RUSTC_BOOTSTRAP: 1
107111
run: target/${{ matrix.target }}/debug/rust-analyzer analysis-stats --with-deps --no-sysroot --no-test $(rustc --print sysroot)/lib/rustlib/src/rust/library/
108112

109113
- name: clippy

src/tools/rust-analyzer/Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,6 @@ dependencies = [
13751375
"memmap2",
13761376
"object 0.33.0",
13771377
"paths",
1378-
"proc-macro-api",
13791378
"proc-macro-test",
13801379
"ra-ap-rustc_lexer",
13811380
"span",
@@ -1390,6 +1389,7 @@ version = "0.0.0"
13901389
dependencies = [
13911390
"proc-macro-api",
13921391
"proc-macro-srv",
1392+
"tt",
13931393
]
13941394

13951395
[[package]]

src/tools/rust-analyzer/Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ span = { path = "./crates/span", version = "0.0.0" }
7979
stdx = { path = "./crates/stdx", version = "0.0.0" }
8080
syntax = { path = "./crates/syntax", version = "0.0.0" }
8181
syntax-bridge = { path = "./crates/syntax-bridge", version = "0.0.0" }
82+
test-utils = { path = "./crates/test-utils", version = "0.0.0" }
8283
toolchain = { path = "./crates/toolchain", version = "0.0.0" }
8384
tt = { path = "./crates/tt", version = "0.0.0" }
8485
vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
@@ -93,7 +94,6 @@ ra-ap-rustc_pattern_analysis = { version = "0.87", default-features = false }
9394

9495
# local crates that aren't published to crates.io. These should not have versions.
9596
test-fixture = { path = "./crates/test-fixture" }
96-
test-utils = { path = "./crates/test-utils" }
9797

9898
# In-tree crates that are published separately and follow semver. See lib/README.md
9999
line-index = { version = "0.1.2" }
@@ -203,6 +203,13 @@ new_ret_no_self = "allow"
203203
useless_asref = "allow"
204204
# Has false positives
205205
assigning_clones = "allow"
206+
# Does not work with macros
207+
vec_init_then_push = "allow"
208+
# Our tests have a lot of these
209+
literal_string_with_formatting_args = "allow"
210+
# This lint has been empowered but now also triggers on cases where its invalid to do so
211+
# due to it ignoring move analysis
212+
unnecessary_map_or = "allow"
206213

207214
## Following lints should be tackled at some point
208215
too_many_arguments = "allow"

src/tools/rust-analyzer/clippy.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ disallowed-types = [
33
{ path = "std::collections::HashSet", reason = "use FxHashSet" },
44
{ path = "std::collections::hash_map::RandomState", reason = "use BuildHasherDefault<FxHasher>"}
55
]
6+
7+
disallowed-methods = [
8+
{ path = "std::process::Command::new", reason = "use `toolchain::command` instead as it forces the choice of a working directory" },
9+
]

src/tools/rust-analyzer/crates/base-db/src/input.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -490,21 +490,25 @@ impl CrateGraph {
490490
}
491491
}
492492

493-
pub fn sort_deps(&mut self) {
494-
self.arena
495-
.iter_mut()
496-
.for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
497-
}
498-
499493
/// Extends this crate graph by adding a complete second crate
500494
/// graph and adjust the ids in the [`ProcMacroPaths`] accordingly.
501495
///
496+
/// This will deduplicate the crates of the graph where possible.
497+
/// Furthermore dependencies are sorted by crate id to make deduplication easier.
498+
///
502499
/// Returns a map mapping `other`'s IDs to the new IDs in `self`.
503500
pub fn extend(
504501
&mut self,
505502
mut other: CrateGraph,
506503
proc_macros: &mut ProcMacroPaths,
507504
) -> FxHashMap<CrateId, CrateId> {
505+
// Sorting here is a bit pointless because the input is likely already sorted.
506+
// However, the overhead is small and it makes the `extend` method harder to misuse.
507+
self.arena
508+
.iter_mut()
509+
.for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
510+
511+
let m = self.len();
508512
let topo = other.crates_in_topological_order();
509513
let mut id_map: FxHashMap<CrateId, CrateId> = FxHashMap::default();
510514
for topo in topo {
@@ -513,7 +517,8 @@ impl CrateGraph {
513517
crate_data.dependencies.iter_mut().for_each(|dep| dep.crate_id = id_map[&dep.crate_id]);
514518
crate_data.dependencies.sort_by_key(|dep| dep.crate_id);
515519

516-
let new_id = self.arena.alloc(crate_data.clone());
520+
let find = self.arena.iter().take(m).find_map(|(k, v)| (v == crate_data).then_some(k));
521+
let new_id = find.unwrap_or_else(|| self.arena.alloc(crate_data.clone()));
517522
id_map.insert(topo, new_id);
518523
}
519524

src/tools/rust-analyzer/crates/cfg/src/cfg_expr.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ impl From<CfgAtom> for CfgExpr {
4545

4646
impl CfgExpr {
4747
#[cfg(feature = "tt")]
48-
pub fn parse<S>(tt: &tt::Subtree<S>) -> CfgExpr {
49-
next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
48+
pub fn parse<S: Copy>(tt: &tt::TopSubtree<S>) -> CfgExpr {
49+
next_cfg_expr(&mut tt.iter()).unwrap_or(CfgExpr::Invalid)
5050
}
5151

5252
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
@@ -66,19 +66,19 @@ impl CfgExpr {
6666
}
6767

6868
#[cfg(feature = "tt")]
69-
fn next_cfg_expr<S>(it: &mut std::slice::Iter<'_, tt::TokenTree<S>>) -> Option<CfgExpr> {
69+
fn next_cfg_expr<S: Copy>(it: &mut tt::iter::TtIter<'_, S>) -> Option<CfgExpr> {
7070
use intern::sym;
71+
use tt::iter::TtElement;
7172

7273
let name = match it.next() {
7374
None => return None,
74-
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.sym.clone(),
75+
Some(TtElement::Leaf(tt::Leaf::Ident(ident))) => ident.sym.clone(),
7576
Some(_) => return Some(CfgExpr::Invalid),
7677
};
7778

78-
// Peek
79-
let ret = match it.as_slice().first() {
80-
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
81-
match it.as_slice().get(1) {
79+
let ret = match it.peek() {
80+
Some(TtElement::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
81+
match it.remaining().flat_tokens().get(1) {
8282
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
8383
it.next();
8484
it.next();
@@ -87,9 +87,8 @@ fn next_cfg_expr<S>(it: &mut std::slice::Iter<'_, tt::TokenTree<S>>) -> Option<C
8787
_ => return Some(CfgExpr::Invalid),
8888
}
8989
}
90-
Some(tt::TokenTree::Subtree(subtree)) => {
90+
Some(TtElement::Subtree(_, mut sub_it)) => {
9191
it.next();
92-
let mut sub_it = subtree.token_trees.iter();
9392
let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it));
9493
match name {
9594
s if s == sym::all => CfgExpr::All(subs.collect()),
@@ -104,7 +103,7 @@ fn next_cfg_expr<S>(it: &mut std::slice::Iter<'_, tt::TokenTree<S>>) -> Option<C
104103
};
105104

106105
// Eat comma separator
107-
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() {
106+
if let Some(TtElement::Leaf(tt::Leaf::Punct(punct))) = it.peek() {
108107
if punct.char == ',' {
109108
it.next();
110109
}

src/tools/rust-analyzer/crates/edition/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
22
name = "edition"
33
version = "0.0.0"
4+
description = "Rust edition support crate for rust-analyzer."
45
rust-version.workspace = true
56
edition.workspace = true
67
license.workspace = true

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

+31-45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A higher level attributes based on TokenTree, with also some shortcuts.
22
3-
use std::{borrow::Cow, hash::Hash, ops, slice};
3+
use std::{borrow::Cow, hash::Hash, ops};
44

55
use base_db::CrateId;
66
use cfg::{CfgExpr, CfgOptions};
@@ -17,6 +17,7 @@ use syntax::{
1717
AstPtr,
1818
};
1919
use triomphe::Arc;
20+
use tt::iter::{TtElement, TtIter};
2021

2122
use crate::{
2223
db::DefDatabase,
@@ -154,15 +155,15 @@ impl Attrs {
154155

155156
pub fn has_doc_hidden(&self) -> bool {
156157
self.by_key(&sym::doc).tt_values().any(|tt| {
157-
tt.delimiter.kind == DelimiterKind::Parenthesis &&
158-
matches!(&*tt.token_trees, [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::hidden)
158+
tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
159+
matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::hidden)
159160
})
160161
}
161162

162163
pub fn has_doc_notable_trait(&self) -> bool {
163164
self.by_key(&sym::doc).tt_values().any(|tt| {
164-
tt.delimiter.kind == DelimiterKind::Parenthesis &&
165-
matches!(&*tt.token_trees, [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::notable_trait)
165+
tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
166+
matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::notable_trait)
166167
})
167168
}
168169

@@ -245,8 +246,8 @@ impl From<DocAtom> for DocExpr {
245246
}
246247

247248
impl DocExpr {
248-
fn parse<S>(tt: &tt::Subtree<S>) -> DocExpr {
249-
next_doc_expr(&mut tt.token_trees.iter()).unwrap_or(DocExpr::Invalid)
249+
fn parse<S: Copy>(tt: &tt::TopSubtree<S>) -> DocExpr {
250+
next_doc_expr(tt.iter()).unwrap_or(DocExpr::Invalid)
250251
}
251252

252253
pub fn aliases(&self) -> &[Symbol] {
@@ -260,62 +261,47 @@ impl DocExpr {
260261
}
261262
}
262263

263-
fn next_doc_expr<S>(it: &mut slice::Iter<'_, tt::TokenTree<S>>) -> Option<DocExpr> {
264+
fn next_doc_expr<S: Copy>(mut it: TtIter<'_, S>) -> Option<DocExpr> {
264265
let name = match it.next() {
265266
None => return None,
266-
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.sym.clone(),
267+
Some(TtElement::Leaf(tt::Leaf::Ident(ident))) => ident.sym.clone(),
267268
Some(_) => return Some(DocExpr::Invalid),
268269
};
269270

270271
// Peek
271-
let ret = match it.as_slice().first() {
272-
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
273-
match it.as_slice().get(1) {
274-
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
272+
let ret = match it.peek() {
273+
Some(TtElement::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
274+
it.next();
275+
match it.next() {
276+
Some(TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
275277
symbol: text,
276278
kind: tt::LitKind::Str,
277279
..
278-
}))) => {
279-
it.next();
280-
it.next();
281-
DocAtom::KeyValue { key: name, value: text.clone() }.into()
282-
}
280+
}))) => DocAtom::KeyValue { key: name, value: text.clone() }.into(),
283281
_ => return Some(DocExpr::Invalid),
284282
}
285283
}
286-
Some(tt::TokenTree::Subtree(subtree)) => {
284+
Some(TtElement::Subtree(_, subtree_iter)) => {
287285
it.next();
288-
let subs = parse_comma_sep(subtree);
286+
let subs = parse_comma_sep(subtree_iter);
289287
match &name {
290288
s if *s == sym::alias => DocExpr::Alias(subs),
291289
_ => DocExpr::Invalid,
292290
}
293291
}
294292
_ => DocAtom::Flag(name).into(),
295293
};
296-
297-
// Eat comma separator
298-
if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() {
299-
if punct.char == ',' {
300-
it.next();
301-
}
302-
}
303294
Some(ret)
304295
}
305296

306-
fn parse_comma_sep<S>(subtree: &tt::Subtree<S>) -> Vec<Symbol> {
307-
subtree
308-
.token_trees
309-
.iter()
310-
.filter_map(|tt| match tt {
311-
tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
312-
kind: tt::LitKind::Str,
313-
symbol,
314-
..
315-
})) => Some(symbol.clone()),
316-
_ => None,
317-
})
318-
.collect()
297+
fn parse_comma_sep<S>(iter: TtIter<'_, S>) -> Vec<Symbol> {
298+
iter.filter_map(|tt| match tt {
299+
TtElement::Leaf(tt::Leaf::Literal(tt::Literal {
300+
kind: tt::LitKind::Str, symbol, ..
301+
})) => Some(symbol.clone()),
302+
_ => None,
303+
})
304+
.collect()
319305
}
320306

321307
impl AttrsWithOwner {
@@ -563,7 +549,7 @@ pub struct AttrQuery<'attr> {
563549
}
564550

565551
impl<'attr> AttrQuery<'attr> {
566-
pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::Subtree> {
552+
pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::TopSubtree> {
567553
self.attrs().filter_map(|attr| attr.token_tree_value())
568554
}
569555

@@ -585,7 +571,7 @@ impl<'attr> AttrQuery<'attr> {
585571

586572
pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
587573
let key = self.key;
588-
self.attrs.iter().filter(move |attr| attr.path.as_ident().map_or(false, |s| *s == *key))
574+
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == *key))
589575
}
590576

591577
/// Find string value for a specific key inside token tree
@@ -596,12 +582,12 @@ impl<'attr> AttrQuery<'attr> {
596582
/// ```
597583
pub fn find_string_value_in_tt(self, key: &'attr Symbol) -> Option<&'attr str> {
598584
self.tt_values().find_map(|tt| {
599-
let name = tt.token_trees.iter()
600-
.skip_while(|tt| !matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == *key))
585+
let name = tt.iter()
586+
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == *key))
601587
.nth(2);
602588

603589
match name {
604-
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal{ symbol: text, kind: tt::LitKind::Str | tt::LitKind::StrRaw(_) , ..}))) => Some(text.as_str()),
590+
Some(TtElement::Leaf(tt::Leaf::Literal(tt::Literal{ symbol: text, kind: tt::LitKind::Str | tt::LitKind::StrRaw(_) , ..}))) => Some(text.as_str()),
605591
_ => None
606592
}
607593
})

src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2216,11 +2216,11 @@ impl ExprCollector<'_> {
22162216
};
22172217
// This needs to match `Flag` in library/core/src/fmt/rt.rs.
22182218
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
2219-
| ((sign == Some(FormatSign::Minus)) as u32) << 1
2220-
| (alternate as u32) << 2
2221-
| (zero_pad as u32) << 3
2222-
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
2223-
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
2219+
| (((sign == Some(FormatSign::Minus)) as u32) << 1)
2220+
| ((alternate as u32) << 2)
2221+
| ((zero_pad as u32) << 3)
2222+
| (((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4)
2223+
| (((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5);
22242224
let flags = self.alloc_expr_desugared(Expr::Literal(Literal::Uint(
22252225
flags as u128,
22262226
Some(BuiltinUint::U32),
@@ -2468,7 +2468,7 @@ impl ExprCollector<'_> {
24682468

24692469
fn comma_follows_token(t: Option<syntax::SyntaxToken>) -> bool {
24702470
(|| syntax::algo::skip_trivia_token(t?.next_token()?, syntax::Direction::Next))()
2471-
.map_or(false, |it| it.kind() == syntax::T![,])
2471+
.is_some_and(|it| it.kind() == syntax::T![,])
24722472
}
24732473

24742474
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]

src/tools/rust-analyzer/crates/hir-def/src/body/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fn your_stack_belongs_to_me() {
5252
cov_mark::check!(your_stack_belongs_to_me);
5353
lower(
5454
r#"
55+
#![recursion_limit = "32"]
5556
macro_rules! n_nuple {
5657
($e:tt) => ();
5758
($($rest:tt)*) => {{
@@ -68,6 +69,7 @@ fn your_stack_belongs_to_me2() {
6869
cov_mark::check!(overflow_but_not_me);
6970
lower(
7071
r#"
72+
#![recursion_limit = "32"]
7173
macro_rules! foo {
7274
() => {{ foo!(); foo!(); }}
7375
}
@@ -78,8 +80,6 @@ fn main() { foo!(); }
7880

7981
#[test]
8082
fn recursion_limit() {
81-
cov_mark::check!(your_stack_belongs_to_me);
82-
8383
lower(
8484
r#"
8585
#![recursion_limit = "2"]

0 commit comments

Comments
 (0)