about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-03-01 11:34:00 +0100
committerGitHub <noreply@github.com>2025-03-01 11:34:00 +0100
commitb7853ef3cfc2d86f859cb6017913f682c848ee1e (patch)
tree172a31432d4d7f91e2c7721c90760a406cc2fac1
parent1d9992d4d27ac773c444f46b6c80f6b43e6e4608 (diff)
parent5bf22375cf14d31a81362fe4230e4756e01483a0 (diff)
downloadrust-b7853ef3cfc2d86f859cb6017913f682c848ee1e.tar.gz
rust-b7853ef3cfc2d86f859cb6017913f682c848ee1e.zip
Rollup merge of #137769 - compiler-errors:empty-unsafe-fmt, r=ytmimi
Do not yeet `unsafe<>` from type when formatting unsafe binder

Unsafe binders are types like `unsafe<'a, 'b> Ty<'a, 'b>`. However, users can also specify unsafe binder types with no bound vars, like `unsafe<> Ty`.

When I added nightly formatting for unsafe binders, I didn't consider this, so on nightly we are stripping the `unsafe<>` part, which gives us back `Ty` which is a different type!

This PR fixes that.

r? ``@ytmimi``
-rw-r--r--src/tools/rustfmt/src/types.rs6
-rw-r--r--src/tools/rustfmt/tests/source/unsafe-binders.rs3
-rw-r--r--src/tools/rustfmt/tests/target/unsafe-binders.rs2
3 files changed, 10 insertions, 1 deletions
diff --git a/src/tools/rustfmt/src/types.rs b/src/tools/rustfmt/src/types.rs
index 0009490e86f..7b44b47c719 100644
--- a/src/tools/rustfmt/src/types.rs
+++ b/src/tools/rustfmt/src/types.rs
@@ -1019,7 +1019,11 @@ impl Rewrite for ast::Ty {
             }
             ast::TyKind::UnsafeBinder(ref binder) => {
                 let mut result = String::new();
-                if let Some(ref lifetime_str) =
+                if binder.generic_params.is_empty() {
+                    // We always want to write `unsafe<>` since `unsafe<> Ty`
+                    // and `Ty` are distinct types.
+                    result.push_str("unsafe<> ")
+                } else if let Some(ref lifetime_str) =
                     rewrite_bound_params(context, shape, &binder.generic_params)
                 {
                     result.push_str("unsafe<");
diff --git a/src/tools/rustfmt/tests/source/unsafe-binders.rs b/src/tools/rustfmt/tests/source/unsafe-binders.rs
index ccf7c8bb9af..2f43af54d20 100644
--- a/src/tools/rustfmt/tests/source/unsafe-binders.rs
+++ b/src/tools/rustfmt/tests/source/unsafe-binders.rs
@@ -9,3 +9,6 @@ struct Foo {
 struct Bar(unsafe<'a> &'a ());
 
 impl Trait for unsafe<'a> &'a () {}
+
+fn empty()
+-> unsafe<> () {}
diff --git a/src/tools/rustfmt/tests/target/unsafe-binders.rs b/src/tools/rustfmt/tests/target/unsafe-binders.rs
index 9d308f4a894..d52dc559519 100644
--- a/src/tools/rustfmt/tests/target/unsafe-binders.rs
+++ b/src/tools/rustfmt/tests/target/unsafe-binders.rs
@@ -7,3 +7,5 @@ struct Foo {
 struct Bar(unsafe<'a> &'a ());
 
 impl Trait for unsafe<'a> &'a () {}
+
+fn empty() -> unsafe<> () {}