Skip to content

Commit fc82903

Browse files
authored
Rollup merge of #137173 - lnicola:sync-from-ra, r=lnicola
Subtree update of `rust-analyzer` r? `@ghost`
2 parents 10cc816 + e08736d commit fc82903

File tree

112 files changed

+9573
-2949
lines changed

Some content is hidden

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

112 files changed

+9573
-2949
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ jobs:
6464
run: |
6565
rustup update --no-self-update ${{ env.RUST_CHANNEL }}
6666
rustup default ${{ env.RUST_CHANNEL }}
67-
rustup component add --toolchain ${{ env.RUST_CHANNEL }} rustfmt rust-src
67+
rustup component add --toolchain ${{ env.RUST_CHANNEL }} rust-src
68+
# We always use a nightly rustfmt, regardless of channel, because we need
69+
# --file-lines.
70+
rustup toolchain add nightly --profile minimal
71+
rustup component add --toolchain nightly rustfmt
6872
# https://github.com/actions-rust-lang/setup-rust-toolchain/blob/main/rust.json
6973
- name: Install Rust Problem Matcher
7074
if: matrix.os == 'ubuntu-latest'

src/tools/rust-analyzer/Cargo.lock

+12-12
Original file line numberDiff line numberDiff line change
@@ -1514,9 +1514,9 @@ dependencies = [
15141514

15151515
[[package]]
15161516
name = "ra-ap-rustc_abi"
1517-
version = "0.94.0"
1517+
version = "0.95.0"
15181518
source = "registry+https://github.com/rust-lang/crates.io-index"
1519-
checksum = "2fa4333df7b71217edb44a36702cafd2bcfc9850677bdf78b32c9f2c98e5df40"
1519+
checksum = "b40c4e339b71a8f075a829b1acaf32f870a11b466d9b8623d50b0ce33e65af95"
15201520
dependencies = [
15211521
"bitflags 2.7.0",
15221522
"ra-ap-rustc_index",
@@ -1525,19 +1525,19 @@ dependencies = [
15251525

15261526
[[package]]
15271527
name = "ra-ap-rustc_index"
1528-
version = "0.94.0"
1528+
version = "0.95.0"
15291529
source = "registry+https://github.com/rust-lang/crates.io-index"
1530-
checksum = "d200275ff3d952cc11108f4dc6a692473659758623d63f2bdcea6104a7f1cec8"
1530+
checksum = "872072e2ba11d11147ebe9fde1608fe7f7d9b5c51dac524af28ee07c6dade468"
15311531
dependencies = [
15321532
"ra-ap-rustc_index_macros",
15331533
"smallvec",
15341534
]
15351535

15361536
[[package]]
15371537
name = "ra-ap-rustc_index_macros"
1538-
version = "0.94.0"
1538+
version = "0.95.0"
15391539
source = "registry+https://github.com/rust-lang/crates.io-index"
1540-
checksum = "06eb63df8c1ce2dcb07647305bed811c9c5ebd157def01a81c1b9479b8592b3b"
1540+
checksum = "ffcd77debcaf2ad690a57c2d041c11eb33fe66869754b2c5f35c52954b46af0c"
15411541
dependencies = [
15421542
"proc-macro2",
15431543
"quote",
@@ -1546,9 +1546,9 @@ dependencies = [
15461546

15471547
[[package]]
15481548
name = "ra-ap-rustc_lexer"
1549-
version = "0.94.0"
1549+
version = "0.95.0"
15501550
source = "registry+https://github.com/rust-lang/crates.io-index"
1551-
checksum = "b7a4d402b2f85650e8c1f78e2e2defc241b03948d6e30d9f5254c9b82755cc4d"
1551+
checksum = "49265cdf8823f8d246e476c79c60bd6e5b551c81ae76e1c8d6a5e0dc73df0bca"
15521552
dependencies = [
15531553
"memchr",
15541554
"unicode-properties",
@@ -1557,19 +1557,19 @@ dependencies = [
15571557

15581558
[[package]]
15591559
name = "ra-ap-rustc_parse_format"
1560-
version = "0.94.0"
1560+
version = "0.95.0"
15611561
source = "registry+https://github.com/rust-lang/crates.io-index"
1562-
checksum = "a23a382dbe392beb26360c1a8ce9193155ef74eeac59bcda0fa0a233e047323a"
1562+
checksum = "b3da239fdc971176de0db45cb631d71475b52033a3d0027d91964da7be89eee6"
15631563
dependencies = [
15641564
"ra-ap-rustc_index",
15651565
"ra-ap-rustc_lexer",
15661566
]
15671567

15681568
[[package]]
15691569
name = "ra-ap-rustc_pattern_analysis"
1570-
version = "0.94.0"
1570+
version = "0.95.0"
15711571
source = "registry+https://github.com/rust-lang/crates.io-index"
1572-
checksum = "d746955d67f315ab79767f1d0bbc17fee4f0970d4a00b9ad76bf09cc7d3cd17e"
1572+
checksum = "56057d08fdfa0d95494e461bbdd5d4b3fdb349cca6be05ad7759bc964be1b8d4"
15731573
dependencies = [
15741574
"ra-ap-rustc_index",
15751575
"rustc-hash 2.0.0",

src/tools/rust-analyzer/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
8787
vfs = { path = "./crates/vfs", version = "0.0.0" }
8888
edition = { path = "./crates/edition", version = "0.0.0" }
8989

90-
ra-ap-rustc_lexer = { version = "0.94", default-features = false }
91-
ra-ap-rustc_parse_format = { version = "0.94", default-features = false }
92-
ra-ap-rustc_index = { version = "0.94", default-features = false }
93-
ra-ap-rustc_abi = { version = "0.94", default-features = false }
94-
ra-ap-rustc_pattern_analysis = { version = "0.94", default-features = false }
90+
ra-ap-rustc_lexer = { version = "0.95", default-features = false }
91+
ra-ap-rustc_parse_format = { version = "0.95", default-features = false }
92+
ra-ap-rustc_index = { version = "0.95", default-features = false }
93+
ra-ap-rustc_abi = { version = "0.95", default-features = false }
94+
ra-ap-rustc_pattern_analysis = { version = "0.95", default-features = false }
9595

9696
# local crates that aren't published to crates.io. These should not have versions.
9797

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

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ bitflags::bitflags! {
250250
const RUSTC_HAS_INCOHERENT_INHERENT_IMPLS = 1 << 3;
251251
const SKIP_ARRAY_DURING_METHOD_DISPATCH = 1 << 4;
252252
const SKIP_BOXED_SLICE_DURING_METHOD_DISPATCH = 1 << 5;
253+
const RUSTC_PAREN_SUGAR = 1 << 6;
253254
}
254255
}
255256

@@ -294,6 +295,9 @@ impl TraitData {
294295
if attrs.by_key(&sym::rustc_has_incoherent_inherent_impls).exists() {
295296
flags |= TraitFlags::RUSTC_HAS_INCOHERENT_INHERENT_IMPLS;
296297
}
298+
if attrs.by_key(&sym::rustc_paren_sugar).exists() {
299+
flags |= TraitFlags::RUSTC_PAREN_SUGAR;
300+
}
297301

298302
let mut skip_array_during_method_dispatch =
299303
attrs.by_key(&sym::rustc_skip_array_during_method_dispatch).exists();

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pub mod keys {
3131

3232
use crate::{
3333
dyn_map::{DynMap, Policy},
34-
BlockId, ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId,
35-
LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId,
36-
TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId,
34+
BlockId, ConstId, EnumId, EnumVariantId, ExternBlockId, ExternCrateId, FieldId, FunctionId,
35+
ImplId, LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId,
36+
TraitAliasId, TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId,
3737
};
3838

3939
pub type Key<K, V> = crate::dyn_map::Key<AstPtr<K>, V, AstPtrPolicy<K, V>>;
@@ -44,6 +44,7 @@ pub mod keys {
4444
pub const STATIC: Key<ast::Static, StaticId> = Key::new();
4545
pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
4646
pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
47+
pub const EXTERN_BLOCK: Key<ast::ExternBlock, ExternBlockId> = Key::new();
4748
pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
4849
pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new();
4950
pub const STRUCT: Key<ast::Struct, StructId> = Key::new();

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ pub struct ExpressionStoreSourceMap {
112112
// AST expressions can create patterns in destructuring assignments. Therefore, `ExprSource` can also map
113113
// to `PatId`, and `PatId` can also map to `ExprSource` (the other way around is unaffected).
114114
expr_map: FxHashMap<ExprSource, ExprOrPatId>,
115-
expr_map_back: ArenaMap<ExprId, ExprSource>,
115+
expr_map_back: ArenaMap<ExprId, ExprOrPatSource>,
116116

117-
pat_map: FxHashMap<PatSource, PatId>,
117+
pat_map: FxHashMap<PatSource, ExprOrPatId>,
118118
pat_map_back: ArenaMap<PatId, ExprOrPatSource>,
119119

120120
label_map: FxHashMap<LabelSource, LabelId>,
@@ -606,12 +606,12 @@ impl Index<TypeRefId> for ExpressionStore {
606606
impl ExpressionStoreSourceMap {
607607
pub fn expr_or_pat_syntax(&self, id: ExprOrPatId) -> Result<ExprOrPatSource, SyntheticSyntax> {
608608
match id {
609-
ExprOrPatId::ExprId(id) => self.expr_syntax(id).map(|it| it.map(AstPtr::wrap_left)),
609+
ExprOrPatId::ExprId(id) => self.expr_syntax(id),
610610
ExprOrPatId::PatId(id) => self.pat_syntax(id),
611611
}
612612
}
613613

614-
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> {
614+
pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprOrPatSource, SyntheticSyntax> {
615615
self.expr_map_back.get(expr).cloned().ok_or(SyntheticSyntax)
616616
}
617617

@@ -633,7 +633,7 @@ impl ExpressionStoreSourceMap {
633633
self.pat_map_back.get(pat).cloned().ok_or(SyntheticSyntax)
634634
}
635635

636-
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
636+
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<ExprOrPatId> {
637637
self.pat_map.get(&node.map(AstPtr::new)).cloned()
638638
}
639639

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

+36-17
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use crate::{
4444
FormatPlaceholder, FormatSign, FormatTrait,
4545
},
4646
Array, Binding, BindingAnnotation, BindingId, BindingProblems, CaptureBy, ClosureKind,
47-
Expr, ExprId, Item, Label, LabelId, Literal, LiteralOrConst, MatchArm, Movability,
48-
OffsetOf, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
47+
Expr, ExprId, Item, Label, LabelId, Literal, MatchArm, Movability, OffsetOf, Pat, PatId,
48+
RecordFieldPat, RecordLitField, Statement,
4949
},
5050
item_scope::BuiltinShadowMode,
5151
lang_item::LangItem,
@@ -1784,23 +1784,33 @@ impl ExprCollector<'_> {
17841784
self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
17851785
this.collect_pat_opt(expanded_pat, binding_list)
17861786
});
1787-
self.source_map.pat_map.insert(src, pat);
1787+
self.source_map.pat_map.insert(src, pat.into());
17881788
return pat;
17891789
}
17901790
None => Pat::Missing,
17911791
},
1792-
// FIXME: implement in a way that also builds source map and calculates assoc resolutions in type inference.
17931792
ast::Pat::RangePat(p) => {
1794-
let mut range_part_lower = |p: Option<ast::Pat>| {
1795-
p.and_then(|it| match &it {
1796-
ast::Pat::LiteralPat(it) => {
1797-
Some(Box::new(LiteralOrConst::Literal(pat_literal_to_hir(it)?.0)))
1793+
let mut range_part_lower = |p: Option<ast::Pat>| -> Option<ExprId> {
1794+
p.and_then(|it| {
1795+
let ptr = PatPtr::new(&it);
1796+
match &it {
1797+
ast::Pat::LiteralPat(it) => Some(self.alloc_expr_from_pat(
1798+
Expr::Literal(pat_literal_to_hir(it)?.0),
1799+
ptr,
1800+
)),
1801+
ast::Pat::IdentPat(ident) if ident.is_simple_ident() => ident
1802+
.name()
1803+
.map(|name| name.as_name())
1804+
.map(Path::from)
1805+
.map(|path| self.alloc_expr_from_pat(Expr::Path(path), ptr)),
1806+
ast::Pat::PathPat(p) => p
1807+
.path()
1808+
.and_then(|path| self.parse_path(path))
1809+
.map(|parsed| self.alloc_expr_from_pat(Expr::Path(parsed), ptr)),
1810+
// We only need to handle literal, ident (if bare) and path patterns here,
1811+
// as any other pattern as a range pattern operand is semantically invalid.
1812+
_ => None,
17981813
}
1799-
pat @ (ast::Pat::IdentPat(_) | ast::Pat::PathPat(_)) => {
1800-
let subpat = self.collect_pat(pat.clone(), binding_list);
1801-
Some(Box::new(LiteralOrConst::Const(subpat)))
1802-
}
1803-
_ => None,
18041814
})
18051815
};
18061816
let start = range_part_lower(p.start());
@@ -1863,7 +1873,7 @@ impl ExprCollector<'_> {
18631873
}
18641874
});
18651875
if let Some(pat) = pat.left() {
1866-
self.source_map.pat_map.insert(src, pat);
1876+
self.source_map.pat_map.insert(src, pat.into());
18671877
}
18681878
pat
18691879
}
@@ -2490,7 +2500,7 @@ impl ExprCollector<'_> {
24902500
fn alloc_expr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
24912501
let src = self.expander.in_file(ptr);
24922502
let id = self.store.exprs.alloc(expr);
2493-
self.source_map.expr_map_back.insert(id, src);
2503+
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_left));
24942504
self.source_map.expr_map.insert(src, id.into());
24952505
id
24962506
}
@@ -2502,7 +2512,7 @@ impl ExprCollector<'_> {
25022512
fn alloc_expr_desugared_with_ptr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
25032513
let src = self.expander.in_file(ptr);
25042514
let id = self.store.exprs.alloc(expr);
2505-
self.source_map.expr_map_back.insert(id, src);
2515+
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_left));
25062516
// We intentionally don't fill this as it could overwrite a non-desugared entry
25072517
// self.source_map.expr_map.insert(src, id);
25082518
id
@@ -2526,11 +2536,20 @@ impl ExprCollector<'_> {
25262536
self.source_map.pat_map_back.insert(id, src.map(AstPtr::wrap_left));
25272537
id
25282538
}
2539+
2540+
fn alloc_expr_from_pat(&mut self, expr: Expr, ptr: PatPtr) -> ExprId {
2541+
let src = self.expander.in_file(ptr);
2542+
let id = self.store.exprs.alloc(expr);
2543+
self.source_map.pat_map.insert(src, id.into());
2544+
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_right));
2545+
id
2546+
}
2547+
25292548
fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
25302549
let src = self.expander.in_file(ptr);
25312550
let id = self.store.pats.alloc(pat);
25322551
self.source_map.pat_map_back.insert(id, src.map(AstPtr::wrap_right));
2533-
self.source_map.pat_map.insert(src, id);
2552+
self.source_map.pat_map.insert(src, id.into());
25342553
id
25352554
}
25362555
// FIXME: desugared pats don't have ptr, that's wrong and should be fixed somehow.

src/tools/rust-analyzer/crates/hir-def/src/expr_store/pretty.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use itertools::Itertools;
66
use span::Edition;
77

88
use crate::{
9-
hir::{
10-
Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, LiteralOrConst, Movability,
11-
Statement,
12-
},
9+
hir::{Array, BindingAnnotation, CaptureBy, ClosureKind, Literal, Movability, Statement},
1310
pretty::{print_generic_args, print_path, print_type_ref},
1411
};
1512

@@ -656,11 +653,11 @@ impl Printer<'_> {
656653
}
657654
Pat::Range { start, end } => {
658655
if let Some(start) = start {
659-
self.print_literal_or_const(start);
656+
self.print_expr(*start);
660657
}
661658
w!(self, "..=");
662659
if let Some(end) = end {
663-
self.print_literal_or_const(end);
660+
self.print_expr(*end);
664661
}
665662
}
666663
Pat::Slice { prefix, slice, suffix } => {
@@ -757,13 +754,6 @@ impl Printer<'_> {
757754
}
758755
}
759756

760-
fn print_literal_or_const(&mut self, literal_or_const: &LiteralOrConst) {
761-
match literal_or_const {
762-
LiteralOrConst::Literal(l) => self.print_literal(l),
763-
LiteralOrConst::Const(c) => self.print_pat(*c),
764-
}
765-
}
766-
767757
fn print_literal(&mut self, literal: &Literal) {
768758
match literal {
769759
Literal::String(it) => w!(self, "{:?}", it),

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

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
mod block;
22

3+
use crate::{hir::MatchArm, test_db::TestDB, ModuleDefId};
34
use expect_test::{expect, Expect};
45
use la_arena::RawIdx;
56
use test_fixture::WithFixture;
67

7-
use crate::{test_db::TestDB, ModuleDefId};
8-
98
use super::*;
109

1110
fn lower(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> (TestDB, Arc<Body>, DefWithBodyId) {
@@ -460,3 +459,45 @@ async fn foo(a: (), b: i32) -> u32 {
460459
expect!["fn foo(�: (), �: i32) -> impl ::core::future::Future::<Output = u32> �"]
461460
.assert_eq(&printed);
462461
}
462+
463+
#[test]
464+
fn range_bounds_are_hir_exprs() {
465+
let (_, body, _) = lower(
466+
r#"
467+
pub const L: i32 = 6;
468+
mod x {
469+
pub const R: i32 = 100;
470+
}
471+
const fn f(x: i32) -> i32 {
472+
match x {
473+
-1..=5 => x * 10,
474+
L..=x::R => x * 100,
475+
_ => x,
476+
}
477+
}"#,
478+
);
479+
480+
let mtch_arms = body
481+
.exprs
482+
.iter()
483+
.find_map(|(_, expr)| {
484+
if let Expr::Match { arms, .. } = expr {
485+
return Some(arms);
486+
}
487+
488+
None
489+
})
490+
.unwrap();
491+
492+
let MatchArm { pat, .. } = mtch_arms[1];
493+
match body.pats[pat] {
494+
Pat::Range { start, end } => {
495+
let hir_start = &body.exprs[start.unwrap()];
496+
let hir_end = &body.exprs[end.unwrap()];
497+
498+
assert!(matches!(hir_start, Expr::Path { .. }));
499+
assert!(matches!(hir_end, Expr::Path { .. }));
500+
}
501+
_ => {}
502+
}
503+
}

0 commit comments

Comments
 (0)