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

Allow to pass or propagate some RUSTFLAGS #283

Open
gui1117 opened this issue Jul 30, 2024 · 1 comment
Open

Allow to pass or propagate some RUSTFLAGS #283

gui1117 opened this issue Jul 30, 2024 · 1 comment

Comments

@gui1117
Copy link
Contributor

gui1117 commented Jul 30, 2024

Due to:
https://github.com/dtolnay/trybuild/pull/278/files#diff-36dcce9bb960d87a9efde38a0309b8e3e552a4193594bb2065ced1e749c9b743R41
the latests version doesn't propagate the RUSTFLAGS anymore.

In our usecase we pass deny warnings using the RUSTFLAGS environment variable.

This is very handy to ensure no warnings ever gets emitted from our macros.

#[test]
fn tests() {
	std::env::set_var("RUSTFLAGS", "--deny warnings");
	let t = trybuild::TestCases::new();
	t.compile_fail("tests/*rs");
}

Is there a way to allow giving rustflags maybe with a new TRYBUILD_RUSTFLAGS or something?

If you approve the direction I can make a PR

@gui1117
Copy link
Contributor Author

gui1117 commented Jul 30, 2024

I was thinking something like this maybe:

 pub(crate) fn toml() -> toml::Value {
-    let mut rustflags = vec!["--cfg", "trybuild", "--verbose"];
+    let mut rustflags = Vec::<String>::new();

+    // Propagate some rustflags from env:
+    for flag in rustflags::from_encoded(&std::env::var_os("TRYBUILD_RUSTFLAGS").unwrap_or_default()
+
+        ) {
+        match flag {
+            rustflags::Flag::Deny(s) => {
+                rustflags.push("-D".into());
+                rustflags.push(s);
+            },
+            rustflags::Flag::Forbid(s) => {
+                rustflags.push("-D".into());
+                rustflags.push(s);
+            },
+            rustflags::Flag::Allow(s) => {
+                rustflags.push("-A".into());
+                rustflags.push(s);
+            },
+            rustflags::Flag::Warn(s) => {
+                rustflags.push("-W".into());
+                rustflags.push(s);
+            },
+            rustflags::Flag::Cfg { name, value: Some(value) } => {
+                rustflags.push("--cfg".into());
+                rustflags.push(format!("{}=\"{}\"", name, value));
+            },
+            rustflags::Flag::Cfg { name, value: None } => {
+                rustflags.push("--cfg".into());
+                rustflags.push(name.into());
+            },
+            _ => (),
+        }
+    }
+
+    rustflags.extend_from_slice(&["--cfg".into(), "trybuild".into(), "--verbose".into()]);
 
     for &lint in IGNORED_LINTS {
-        rustflags.push("-A");
-        rustflags.push(lint);
+        rustflags.push("-A".into());
+        rustflags.push(lint.into());
     }
 
     toml::Value::try_from(rustflags).unwrap()
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant