Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7bdf48c

Browse files
committedDec 20, 2023
Auto merge of #16165 - Veykril:meta-vars, r=Veykril
fix: Update metavariable expression implementation Fixes #16154 This duplicates behavior of that before and after PR rust-lang/rust#117050 based on the toolchain version. There are some 1.76 nightlies that are still broken (any before that PR basically) but fetching and storing the commit makes little sense to me (opposed to the toolchain version).
2 parents 831d0e0 + f48ecb6 commit 7bdf48c

25 files changed

+316
-153
lines changed
 

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ nohash-hasher = "0.2.0"
110110
rayon = "1.8.0"
111111
rust-analyzer-salsa = "0.17.0-pre.4"
112112
rustc-hash = "1.1.0"
113+
semver = "1.0.14"
113114
serde = { version = "1.0.192", features = ["derive"] }
114115
serde_json = "1.0.108"
115116
smallvec = { version = "1.10.0", features = [

‎crates/base-db/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ la-arena.workspace = true
1616
rust-analyzer-salsa.workspace = true
1717
rustc-hash.workspace = true
1818
triomphe.workspace = true
19+
semver.workspace = true
1920

2021
# local deps
2122
cfg.workspace = true

‎crates/base-db/src/input.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{fmt, mem, ops, str::FromStr};
1111
use cfg::CfgOptions;
1212
use la_arena::{Arena, Idx};
1313
use rustc_hash::{FxHashMap, FxHashSet};
14+
use semver::Version;
1415
use syntax::SmolStr;
1516
use triomphe::Arc;
1617
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
@@ -258,7 +259,7 @@ impl ReleaseChannel {
258259

259260
pub fn from_str(str: &str) -> Option<Self> {
260261
Some(match str {
261-
"" => ReleaseChannel::Stable,
262+
"" | "stable" => ReleaseChannel::Stable,
262263
"nightly" => ReleaseChannel::Nightly,
263264
_ if str.starts_with("beta") => ReleaseChannel::Beta,
264265
_ => return None,
@@ -289,7 +290,7 @@ pub struct CrateData {
289290
// things. This info does need to be somewhat present though as to prevent deduplication from
290291
// happening across different workspaces with different layouts.
291292
pub target_layout: TargetLayoutLoadResult,
292-
pub channel: Option<ReleaseChannel>,
293+
pub toolchain: Option<Version>,
293294
}
294295

295296
impl CrateData {
@@ -346,6 +347,10 @@ impl CrateData {
346347

347348
slf_deps.eq(other_deps)
348349
}
350+
351+
pub fn channel(&self) -> Option<ReleaseChannel> {
352+
self.toolchain.as_ref().and_then(|v| ReleaseChannel::from_str(&v.pre))
353+
}
349354
}
350355

351356
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -428,7 +433,7 @@ impl CrateGraph {
428433
is_proc_macro: bool,
429434
origin: CrateOrigin,
430435
target_layout: Result<Arc<str>, Arc<str>>,
431-
channel: Option<ReleaseChannel>,
436+
toolchain: Option<Version>,
432437
) -> CrateId {
433438
let data = CrateData {
434439
root_file_id,
@@ -442,7 +447,7 @@ impl CrateGraph {
442447
origin,
443448
target_layout,
444449
is_proc_macro,
445-
channel,
450+
toolchain,
446451
};
447452
self.arena.alloc(data)
448453
}

‎crates/base-db/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub use salsa::{self, Cancelled};
2323
pub use span::{FilePosition, FileRange};
2424
pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};
2525

26+
pub use semver::{BuildMetadata, Prerelease, Version, VersionReq};
27+
2628
#[macro_export]
2729
macro_rules! impl_intern_key {
2830
($name:ident) => {

‎crates/hir-def/src/macro_expansion_tests/mbe/meta_syntax.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ macro_rules! m {
1818
($($false:ident)*) => ($false);
1919
(double_dollar) => ($$);
2020
($) => (m!($););
21-
($($t:tt)*) => ($( ${ignore(t)} ${index()} )-*);
21+
($($t:tt)*) => ($( ${ignore($t)} ${index()} )-*);
2222
}
2323
m!($);
2424
"#,
@@ -33,7 +33,7 @@ macro_rules! m {
3333
($($false:ident)*) => ($false);
3434
(double_dollar) => ($$);
3535
($) => (m!($););
36-
($($t:tt)*) => ($( ${ignore(t)} ${index()} )-*);
36+
($($t:tt)*) => ($( ${ignore($t)} ${index()} )-*);
3737
}
3838
m!($);
3939
"#]],

‎crates/hir-def/src/macro_expansion_tests/mbe/metavar_expr.rs

+40-38
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ fn test_metavar_exprs() {
7777
check(
7878
r#"
7979
macro_rules! m {
80-
( $( $t:tt )* ) => ( $( ${ignore(t)} -${index()} )-* );
80+
( $( $t:tt )* ) => ( $( ${ignore($t)} -${index()} )-* );
8181
}
8282
const _: i32 = m!(a b c);
8383
"#,
8484
expect![[r#"
8585
macro_rules! m {
86-
( $( $t:tt )* ) => ( $( ${ignore(t)} -${index()} )-* );
86+
( $( $t:tt )* ) => ( $( ${ignore($t)} -${index()} )-* );
8787
}
8888
const _: i32 = -0--1--2;
8989
"#]],
@@ -96,7 +96,7 @@ fn count_basic() {
9696
r#"
9797
macro_rules! m {
9898
($($t:ident),*) => {
99-
${count(t)}
99+
${count($t)}
100100
}
101101
}
102102
@@ -109,7 +109,7 @@ fn test() {
109109
expect![[r#"
110110
macro_rules! m {
111111
($($t:ident),*) => {
112-
${count(t)}
112+
${count($t)}
113113
}
114114
}
115115
@@ -130,9 +130,9 @@ macro_rules! foo {
130130
($( $( $($t:ident)* ),* );*) => {
131131
$(
132132
{
133-
let depth_none = ${count(t)};
134-
let depth_zero = ${count(t, 0)};
135-
let depth_one = ${count(t, 1)};
133+
let depth_none = ${count($t)};
134+
let depth_zero = ${count($t, 0)};
135+
let depth_one = ${count($t, 1)};
136136
}
137137
)*
138138
}
@@ -150,21 +150,21 @@ macro_rules! foo {
150150
($( $( $($t:ident)* ),* );*) => {
151151
$(
152152
{
153-
let depth_none = ${count(t)};
154-
let depth_zero = ${count(t, 0)};
155-
let depth_one = ${count(t, 1)};
153+
let depth_none = ${count($t)};
154+
let depth_zero = ${count($t, 0)};
155+
let depth_one = ${count($t, 1)};
156156
}
157157
)*
158158
}
159159
}
160160
161161
fn bar() {
162162
{
163-
let depth_none = 6;
163+
let depth_none = 3;
164164
let depth_zero = 3;
165165
let depth_one = 6;
166166
} {
167-
let depth_none = 3;
167+
let depth_none = 1;
168168
let depth_zero = 1;
169169
let depth_one = 3;
170170
}
@@ -178,12 +178,12 @@ fn count_depth_out_of_bounds() {
178178
check(
179179
r#"
180180
macro_rules! foo {
181-
($($t:ident)*) => { ${count(t, 1)} };
182-
($( $( $l:literal )* );*) => { $(${count(l, 1)};)* }
181+
($($t:ident)*) => { ${count($t, 1)} };
182+
($( $( $l:literal )* );*) => { $(${count($l, 1)};)* }
183183
}
184184
macro_rules! bar {
185-
($($t:ident)*) => { ${count(t, 1024)} };
186-
($( $( $l:literal )* );*) => { $(${count(l, 8192)};)* }
185+
($($t:ident)*) => { ${count($t, 1024)} };
186+
($( $( $l:literal )* );*) => { $(${count($l, 8192)};)* }
187187
}
188188
189189
fn test() {
@@ -195,19 +195,21 @@ fn test() {
195195
"#,
196196
expect![[r#"
197197
macro_rules! foo {
198-
($($t:ident)*) => { ${count(t, 1)} };
199-
($( $( $l:literal )* );*) => { $(${count(l, 1)};)* }
198+
($($t:ident)*) => { ${count($t, 1)} };
199+
($( $( $l:literal )* );*) => { $(${count($l, 1)};)* }
200200
}
201201
macro_rules! bar {
202-
($($t:ident)*) => { ${count(t, 1024)} };
203-
($( $( $l:literal )* );*) => { $(${count(l, 8192)};)* }
202+
($($t:ident)*) => { ${count($t, 1024)} };
203+
($( $( $l:literal )* );*) => { $(${count($l, 8192)};)* }
204204
}
205205
206206
fn test() {
207-
/* error: ${count} out of bounds */;
208-
/* error: ${count} out of bounds */;
209-
/* error: ${count} out of bounds */;
210-
/* error: ${count} out of bounds */;
207+
2;
208+
2;
209+
1;;
210+
2;
211+
2;
212+
1;;
211213
}
212214
"#]],
213215
);
@@ -218,8 +220,8 @@ fn misplaced_count() {
218220
check(
219221
r#"
220222
macro_rules! foo {
221-
($($t:ident)*) => { $(${count(t)})* };
222-
($l:literal) => { ${count(l)} }
223+
($($t:ident)*) => { $(${count($t)})* };
224+
($l:literal) => { ${count($l)} }
223225
}
224226
225227
fn test() {
@@ -229,13 +231,13 @@ fn test() {
229231
"#,
230232
expect![[r#"
231233
macro_rules! foo {
232-
($($t:ident)*) => { $(${count(t)})* };
233-
($l:literal) => { ${count(l)} }
234+
($($t:ident)*) => { $(${count($t)})* };
235+
($l:literal) => { ${count($l)} }
234236
}
235237
236238
fn test() {
237-
/* error: ${count} misplaced */;
238-
/* error: ${count} misplaced */;
239+
1 1 1;
240+
1;
239241
}
240242
"#]],
241243
);
@@ -246,13 +248,13 @@ fn malformed_count() {
246248
check(
247249
r#"
248250
macro_rules! too_many_args {
249-
($($t:ident)*) => { ${count(t, 1, leftover)} }
251+
($($t:ident)*) => { ${count($t, 1, leftover)} }
250252
}
251253
macro_rules! depth_suffixed {
252-
($($t:ident)*) => { ${count(t, 0usize)} }
254+
($($t:ident)*) => { ${count($t, 0usize)} }
253255
}
254256
macro_rules! depth_too_large {
255-
($($t:ident)*) => { ${count(t, 18446744073709551616)} }
257+
($($t:ident)*) => { ${count($t, 18446744073709551616)} }
256258
}
257259
258260
fn test() {
@@ -263,13 +265,13 @@ fn test() {
263265
"#,
264266
expect![[r#"
265267
macro_rules! too_many_args {
266-
($($t:ident)*) => { ${count(t, 1, leftover)} }
268+
($($t:ident)*) => { ${count($t, 1, leftover)} }
267269
}
268270
macro_rules! depth_suffixed {
269-
($($t:ident)*) => { ${count(t, 0usize)} }
271+
($($t:ident)*) => { ${count($t, 0usize)} }
270272
}
271273
macro_rules! depth_too_large {
272-
($($t:ident)*) => { ${count(t, 18446744073709551616)} }
274+
($($t:ident)*) => { ${count($t, 18446744073709551616)} }
273275
}
274276
275277
fn test() {
@@ -288,7 +290,7 @@ fn count_interaction_with_empty_binding() {
288290
r#"
289291
macro_rules! m {
290292
($($t:ident),*) => {
291-
${count(t, 100)}
293+
${count($t, 100)}
292294
}
293295
}
294296
@@ -299,7 +301,7 @@ fn test() {
299301
expect![[r#"
300302
macro_rules! m {
301303
($($t:ident),*) => {
302-
${count(t, 100)}
304+
${count($t, 100)}
303305
}
304306
}
305307
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.