Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update metavariable expression implementation #16165

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ nohash-hasher = "0.2.0"
rayon = "1.8.0"
rust-analyzer-salsa = "0.17.0-pre.4"
rustc-hash = "1.1.0"
semver = "1.0.14"
serde = { version = "1.0.192", features = ["derive"] }
serde_json = "1.0.108"
smallvec = { version = "1.10.0", features = [
Expand Down
1 change: 1 addition & 0 deletions crates/base-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ la-arena.workspace = true
rust-analyzer-salsa.workspace = true
rustc-hash.workspace = true
triomphe.workspace = true
semver.workspace = true

# local deps
cfg.workspace = true
Expand Down
13 changes: 9 additions & 4 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{fmt, mem, ops, str::FromStr};
use cfg::CfgOptions;
use la_arena::{Arena, Idx};
use rustc_hash::{FxHashMap, FxHashSet};
use semver::Version;
use syntax::SmolStr;
use triomphe::Arc;
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
Expand Down Expand Up @@ -258,7 +259,7 @@ impl ReleaseChannel {

pub fn from_str(str: &str) -> Option<Self> {
Some(match str {
"" => ReleaseChannel::Stable,
"" | "stable" => ReleaseChannel::Stable,
"nightly" => ReleaseChannel::Nightly,
_ if str.starts_with("beta") => ReleaseChannel::Beta,
_ => return None,
Expand Down Expand Up @@ -289,7 +290,7 @@ pub struct CrateData {
// things. This info does need to be somewhat present though as to prevent deduplication from
// happening across different workspaces with different layouts.
pub target_layout: TargetLayoutLoadResult,
pub channel: Option<ReleaseChannel>,
pub toolchain: Option<Version>,
}

impl CrateData {
Expand Down Expand Up @@ -346,6 +347,10 @@ impl CrateData {

slf_deps.eq(other_deps)
}

pub fn channel(&self) -> Option<ReleaseChannel> {
self.toolchain.as_ref().and_then(|v| ReleaseChannel::from_str(&v.pre))
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -427,7 +432,7 @@ impl CrateGraph {
is_proc_macro: bool,
origin: CrateOrigin,
target_layout: Result<Arc<str>, Arc<str>>,
channel: Option<ReleaseChannel>,
toolchain: Option<Version>,
) -> CrateId {
let data = CrateData {
root_file_id,
Expand All @@ -441,7 +446,7 @@ impl CrateGraph {
origin,
target_layout,
is_proc_macro,
channel,
toolchain,
};
self.arena.alloc(data)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub use salsa::{self, Cancelled};
pub use span::{FilePosition, FileRange};
pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};

pub use semver::{BuildMetadata, Prerelease, Version, VersionReq};

#[macro_export]
macro_rules! impl_intern_key {
($name:ident) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/macro_expansion_tests/mbe/meta_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! m {
($($false:ident)*) => ($false);
(double_dollar) => ($$);
($) => (m!($););
($($t:tt)*) => ($( ${ignore(t)} ${index()} )-*);
($($t:tt)*) => ($( ${ignore($t)} ${index()} )-*);
}
m!($);
"#,
Expand All @@ -33,7 +33,7 @@ macro_rules! m {
($($false:ident)*) => ($false);
(double_dollar) => ($$);
($) => (m!($););
($($t:tt)*) => ($( ${ignore(t)} ${index()} )-*);
($($t:tt)*) => ($( ${ignore($t)} ${index()} )-*);
}
m!($);
"#]],
Expand Down
78 changes: 40 additions & 38 deletions crates/hir-def/src/macro_expansion_tests/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ fn test_metavar_exprs() {
check(
r#"
macro_rules! m {
( $( $t:tt )* ) => ( $( ${ignore(t)} -${index()} )-* );
( $( $t:tt )* ) => ( $( ${ignore($t)} -${index()} )-* );
}
const _: i32 = m!(a b c);
"#,
expect![[r#"
macro_rules! m {
( $( $t:tt )* ) => ( $( ${ignore(t)} -${index()} )-* );
( $( $t:tt )* ) => ( $( ${ignore($t)} -${index()} )-* );
}
const _: i32 = -0--1--2;
"#]],
Expand All @@ -96,7 +96,7 @@ fn count_basic() {
r#"
macro_rules! m {
($($t:ident),*) => {
${count(t)}
${count($t)}
}
}

Expand All @@ -109,7 +109,7 @@ fn test() {
expect![[r#"
macro_rules! m {
($($t:ident),*) => {
${count(t)}
${count($t)}
}
}

Expand All @@ -130,9 +130,9 @@ macro_rules! foo {
($( $( $($t:ident)* ),* );*) => {
$(
{
let depth_none = ${count(t)};
let depth_zero = ${count(t, 0)};
let depth_one = ${count(t, 1)};
let depth_none = ${count($t)};
let depth_zero = ${count($t, 0)};
let depth_one = ${count($t, 1)};
}
)*
}
Expand All @@ -150,21 +150,21 @@ macro_rules! foo {
($( $( $($t:ident)* ),* );*) => {
$(
{
let depth_none = ${count(t)};
let depth_zero = ${count(t, 0)};
let depth_one = ${count(t, 1)};
let depth_none = ${count($t)};
let depth_zero = ${count($t, 0)};
let depth_one = ${count($t, 1)};
}
)*
}
}

fn bar() {
{
let depth_none = 6;
let depth_none = 3;
let depth_zero = 3;
let depth_one = 6;
} {
let depth_none = 3;
let depth_none = 1;
let depth_zero = 1;
let depth_one = 3;
}
Expand All @@ -178,12 +178,12 @@ fn count_depth_out_of_bounds() {
check(
r#"
macro_rules! foo {
($($t:ident)*) => { ${count(t, 1)} };
($( $( $l:literal )* );*) => { $(${count(l, 1)};)* }
($($t:ident)*) => { ${count($t, 1)} };
($( $( $l:literal )* );*) => { $(${count($l, 1)};)* }
}
macro_rules! bar {
($($t:ident)*) => { ${count(t, 1024)} };
($( $( $l:literal )* );*) => { $(${count(l, 8192)};)* }
($($t:ident)*) => { ${count($t, 1024)} };
($( $( $l:literal )* );*) => { $(${count($l, 8192)};)* }
}

fn test() {
Expand All @@ -195,19 +195,21 @@ fn test() {
"#,
expect![[r#"
macro_rules! foo {
($($t:ident)*) => { ${count(t, 1)} };
($( $( $l:literal )* );*) => { $(${count(l, 1)};)* }
($($t:ident)*) => { ${count($t, 1)} };
($( $( $l:literal )* );*) => { $(${count($l, 1)};)* }
}
macro_rules! bar {
($($t:ident)*) => { ${count(t, 1024)} };
($( $( $l:literal )* );*) => { $(${count(l, 8192)};)* }
($($t:ident)*) => { ${count($t, 1024)} };
($( $( $l:literal )* );*) => { $(${count($l, 8192)};)* }
}

fn test() {
/* error: ${count} out of bounds */;
/* error: ${count} out of bounds */;
/* error: ${count} out of bounds */;
/* error: ${count} out of bounds */;
2;
2;
1;;
2;
2;
1;;
}
"#]],
);
Expand All @@ -218,8 +220,8 @@ fn misplaced_count() {
check(
r#"
macro_rules! foo {
($($t:ident)*) => { $(${count(t)})* };
($l:literal) => { ${count(l)} }
($($t:ident)*) => { $(${count($t)})* };
($l:literal) => { ${count($l)} }
}

fn test() {
Expand All @@ -229,13 +231,13 @@ fn test() {
"#,
expect![[r#"
macro_rules! foo {
($($t:ident)*) => { $(${count(t)})* };
($l:literal) => { ${count(l)} }
($($t:ident)*) => { $(${count($t)})* };
($l:literal) => { ${count($l)} }
}

fn test() {
/* error: ${count} misplaced */;
/* error: ${count} misplaced */;
1 1 1;
1;
}
"#]],
);
Expand All @@ -246,13 +248,13 @@ fn malformed_count() {
check(
r#"
macro_rules! too_many_args {
($($t:ident)*) => { ${count(t, 1, leftover)} }
($($t:ident)*) => { ${count($t, 1, leftover)} }
}
macro_rules! depth_suffixed {
($($t:ident)*) => { ${count(t, 0usize)} }
($($t:ident)*) => { ${count($t, 0usize)} }
}
macro_rules! depth_too_large {
($($t:ident)*) => { ${count(t, 18446744073709551616)} }
($($t:ident)*) => { ${count($t, 18446744073709551616)} }
}

fn test() {
Expand All @@ -263,13 +265,13 @@ fn test() {
"#,
expect![[r#"
macro_rules! too_many_args {
($($t:ident)*) => { ${count(t, 1, leftover)} }
($($t:ident)*) => { ${count($t, 1, leftover)} }
}
macro_rules! depth_suffixed {
($($t:ident)*) => { ${count(t, 0usize)} }
($($t:ident)*) => { ${count($t, 0usize)} }
}
macro_rules! depth_too_large {
($($t:ident)*) => { ${count(t, 18446744073709551616)} }
($($t:ident)*) => { ${count($t, 18446744073709551616)} }
}

fn test() {
Expand All @@ -288,7 +290,7 @@ fn count_interaction_with_empty_binding() {
r#"
macro_rules! m {
($($t:ident),*) => {
${count(t, 100)}
${count($t, 100)}
}
}

Expand All @@ -299,7 +301,7 @@ fn test() {
expect![[r#"
macro_rules! m {
($($t:ident),*) => {
${count(t, 100)}
${count($t, 100)}
}
}

Expand Down
Loading