summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/proc-macro-test
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/tools/rust-analyzer/crates/proc-macro-test
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/rust-analyzer/crates/proc-macro-test')
-rw-r--r--src/tools/rust-analyzer/crates/proc-macro-test/Cargo.toml15
-rw-r--r--src/tools/rust-analyzer/crates/proc-macro-test/build.rs106
-rw-r--r--src/tools/rust-analyzer/crates/proc-macro-test/imp/Cargo.toml17
-rw-r--r--src/tools/rust-analyzer/crates/proc-macro-test/imp/src/lib.rs114
-rw-r--r--src/tools/rust-analyzer/crates/proc-macro-test/src/lib.rs6
5 files changed, 258 insertions, 0 deletions
diff --git a/src/tools/rust-analyzer/crates/proc-macro-test/Cargo.toml b/src/tools/rust-analyzer/crates/proc-macro-test/Cargo.toml
new file mode 100644
index 000000000..684477191
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/proc-macro-test/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "proc-macro-test"
+version = "0.0.0"
+license = "MIT OR Apache-2.0"
+edition = "2021"
+rust-version = "1.57"
+publish = false
+
+[lib]
+doctest = false
+
+[build-dependencies]
+proc-macro-test-impl = { path = "imp", version = "0.0.0" }
+toolchain = { path = "../toolchain", version = "0.0.0" }
+cargo_metadata = "0.15.0"
diff --git a/src/tools/rust-analyzer/crates/proc-macro-test/build.rs b/src/tools/rust-analyzer/crates/proc-macro-test/build.rs
new file mode 100644
index 000000000..a80c96261
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/proc-macro-test/build.rs
@@ -0,0 +1,106 @@
+//! This will build the proc macro in `imp`, and copy the resulting dylib artifact into the
+//! `OUT_DIR`.
+//!
+//! `proc-macro-test` itself contains only a path to that artifact.
+//!
+//! The `PROC_MACRO_TEST_TOOLCHAIN` environment variable can be exported to use
+//! a specific rustup toolchain: this allows testing against older ABIs (e.g.
+//! 1.58) and future ABIs (stage1, nightly)
+
+use std::{
+ env, fs,
+ path::{Path, PathBuf},
+ process::Command,
+};
+
+use cargo_metadata::Message;
+
+fn main() {
+ println!("cargo:rerun-if-changed=imp");
+ println!("cargo:rerun-if-env-changed=PROC_MACRO_TEST_TOOLCHAIN");
+
+ let out_dir = env::var_os("OUT_DIR").unwrap();
+ let out_dir = Path::new(&out_dir);
+
+ let name = "proc-macro-test-impl";
+ let version = "0.0.0";
+
+ let imp_dir = std::env::current_dir().unwrap().join("imp");
+
+ let staging_dir = out_dir.join("proc-macro-test-imp-staging");
+ // this'll error out if the staging dir didn't previously exist. using
+ // `std::fs::exists` would suffer from TOCTOU so just do our best to
+ // wipe it and ignore errors.
+ let _ = std::fs::remove_dir_all(&staging_dir);
+
+ println!("Creating {}", staging_dir.display());
+ std::fs::create_dir_all(&staging_dir).unwrap();
+
+ let src_dir = staging_dir.join("src");
+ println!("Creating {}", src_dir.display());
+ std::fs::create_dir_all(src_dir).unwrap();
+
+ for item_els in [&["Cargo.toml"][..], &["src", "lib.rs"]] {
+ let mut src = imp_dir.clone();
+ let mut dst = staging_dir.clone();
+ for el in item_els {
+ src.push(el);
+ dst.push(el);
+ }
+ println!("Copying {} to {}", src.display(), dst.display());
+ std::fs::copy(src, dst).unwrap();
+ }
+
+ let target_dir = out_dir.join("target");
+
+ let mut cmd = if let Ok(toolchain) = std::env::var("PROC_MACRO_TEST_TOOLCHAIN") {
+ // leverage rustup to find user-specific toolchain
+ let mut cmd = Command::new("cargo");
+ cmd.arg(format!("+{toolchain}"));
+ cmd
+ } else {
+ Command::new(toolchain::cargo())
+ };
+
+ cmd.current_dir(&staging_dir)
+ .args(&["build", "-p", "proc-macro-test-impl", "--message-format", "json"])
+ // Explicit override the target directory to avoid using the same one which the parent
+ // cargo is using, or we'll deadlock.
+ // This can happen when `CARGO_TARGET_DIR` is set or global config forces all cargo
+ // instance to use the same target directory.
+ .arg("--target-dir")
+ .arg(&target_dir);
+
+ println!("Running {:?}", cmd);
+
+ let output = cmd.output().unwrap();
+ if !output.status.success() {
+ println!("proc-macro-test-impl failed to build");
+ println!("============ stdout ============");
+ println!("{}", String::from_utf8_lossy(&output.stdout));
+ println!("============ stderr ============");
+ println!("{}", String::from_utf8_lossy(&output.stderr));
+ panic!("proc-macro-test-impl failed to build");
+ }
+
+ let mut artifact_path = None;
+ for message in Message::parse_stream(output.stdout.as_slice()) {
+ match message.unwrap() {
+ Message::CompilerArtifact(artifact) => {
+ if artifact.target.kind.contains(&"proc-macro".to_string()) {
+ let repr = format!("{} {}", name, version);
+ if artifact.package_id.repr.starts_with(&repr) {
+ artifact_path = Some(PathBuf::from(&artifact.filenames[0]));
+ }
+ }
+ }
+ _ => (), // Unknown message
+ }
+ }
+
+ // This file is under `target_dir` and is already under `OUT_DIR`.
+ let artifact_path = artifact_path.expect("no dylib for proc-macro-test-impl found");
+
+ let info_path = out_dir.join("proc_macro_test_location.txt");
+ fs::write(info_path, artifact_path.to_str().unwrap()).unwrap();
+}
diff --git a/src/tools/rust-analyzer/crates/proc-macro-test/imp/Cargo.toml b/src/tools/rust-analyzer/crates/proc-macro-test/imp/Cargo.toml
new file mode 100644
index 000000000..2d1fc3c5c
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/proc-macro-test/imp/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+name = "proc-macro-test-impl"
+version = "0.0.0"
+license = "MIT OR Apache-2.0"
+edition = "2021"
+rust-version = "1.57"
+publish = false
+
+[lib]
+doctest = false
+proc-macro = true
+
+[workspace]
+
+[dependencies]
+# this crate should not have any dependencies, since it uses its own workspace,
+# and its own `Cargo.lock`
diff --git a/src/tools/rust-analyzer/crates/proc-macro-test/imp/src/lib.rs b/src/tools/rust-analyzer/crates/proc-macro-test/imp/src/lib.rs
new file mode 100644
index 000000000..feeacdb64
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/proc-macro-test/imp/src/lib.rs
@@ -0,0 +1,114 @@
+//! Exports a few trivial procedural macros for testing.
+
+#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+
+use proc_macro::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
+
+#[proc_macro]
+pub fn fn_like_noop(args: TokenStream) -> TokenStream {
+ args
+}
+
+#[proc_macro]
+pub fn fn_like_panic(args: TokenStream) -> TokenStream {
+ panic!("fn_like_panic!({})", args);
+}
+
+#[proc_macro]
+pub fn fn_like_error(args: TokenStream) -> TokenStream {
+ format!("compile_error!(\"fn_like_error!({})\");", args).parse().unwrap()
+}
+
+#[proc_macro]
+pub fn fn_like_clone_tokens(args: TokenStream) -> TokenStream {
+ clone_stream(args)
+}
+
+#[proc_macro]
+pub fn fn_like_mk_literals(_args: TokenStream) -> TokenStream {
+ let trees: Vec<TokenTree> = vec![
+ TokenTree::from(Literal::byte_string(b"byte_string")),
+ TokenTree::from(Literal::character('c')),
+ TokenTree::from(Literal::string("string")),
+ // as of 2022-07-21, there's no method on `Literal` to build a raw
+ // string or a raw byte string
+ TokenTree::from(Literal::f64_suffixed(3.14)),
+ TokenTree::from(Literal::f64_unsuffixed(3.14)),
+ TokenTree::from(Literal::i64_suffixed(123)),
+ TokenTree::from(Literal::i64_unsuffixed(123)),
+ ];
+ TokenStream::from_iter(trees)
+}
+
+#[proc_macro]
+pub fn fn_like_mk_idents(_args: TokenStream) -> TokenStream {
+ let trees: Vec<TokenTree> = vec![
+ TokenTree::from(Ident::new("standard", Span::call_site())),
+ TokenTree::from(Ident::new_raw("raw", Span::call_site())),
+ ];
+ TokenStream::from_iter(trees)
+}
+
+#[proc_macro_attribute]
+pub fn attr_noop(_args: TokenStream, item: TokenStream) -> TokenStream {
+ item
+}
+
+#[proc_macro_attribute]
+pub fn attr_panic(args: TokenStream, item: TokenStream) -> TokenStream {
+ panic!("#[attr_panic {}] {}", args, item);
+}
+
+#[proc_macro_attribute]
+pub fn attr_error(args: TokenStream, item: TokenStream) -> TokenStream {
+ format!("compile_error!(\"#[attr_error({})] {}\");", args, item).parse().unwrap()
+}
+
+#[proc_macro_derive(DeriveEmpty)]
+pub fn derive_empty(_item: TokenStream) -> TokenStream {
+ TokenStream::new()
+}
+
+#[proc_macro_derive(DerivePanic)]
+pub fn derive_panic(item: TokenStream) -> TokenStream {
+ panic!("#[derive(DerivePanic)] {}", item);
+}
+
+#[proc_macro_derive(DeriveError)]
+pub fn derive_error(item: TokenStream) -> TokenStream {
+ format!("compile_error!(\"#[derive(DeriveError)] {}\");", item).parse().unwrap()
+}
+
+fn clone_stream(ts: TokenStream) -> TokenStream {
+ ts.into_iter().map(clone_tree).collect()
+}
+
+fn clone_tree(t: TokenTree) -> TokenTree {
+ match t {
+ TokenTree::Group(orig) => {
+ let mut new = Group::new(orig.delimiter(), clone_stream(orig.stream()));
+ new.set_span(orig.span());
+ TokenTree::Group(new)
+ }
+ TokenTree::Ident(orig) => {
+ let s = orig.to_string();
+ if let Some(rest) = s.strip_prefix("r#") {
+ TokenTree::Ident(Ident::new_raw(rest, orig.span()))
+ } else {
+ TokenTree::Ident(Ident::new(&s, orig.span()))
+ }
+ }
+ TokenTree::Punct(orig) => {
+ let mut new = Punct::new(orig.as_char(), orig.spacing());
+ new.set_span(orig.span());
+ TokenTree::Punct(new)
+ }
+ TokenTree::Literal(orig) => {
+ // this goes through `literal_from_str` as of 2022-07-18, cf.
+ // https://github.com/rust-lang/rust/commit/b34c79f8f1ef4d0149ad4bf77e1759c07a9a01a8
+ let mut new: Literal = orig.to_string().parse().unwrap();
+ new.set_span(orig.span());
+ TokenTree::Literal(new)
+ }
+ }
+}
diff --git a/src/tools/rust-analyzer/crates/proc-macro-test/src/lib.rs b/src/tools/rust-analyzer/crates/proc-macro-test/src/lib.rs
new file mode 100644
index 000000000..6d57bc81e
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/proc-macro-test/src/lib.rs
@@ -0,0 +1,6 @@
+//! Exports a few trivial procedural macros for testing.
+
+#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+
+pub static PROC_MACRO_TEST_LOCATION: &str =
+ include_str!(concat!(env!("OUT_DIR"), "/proc_macro_test_location.txt"));