From 25e64cd95f21bac4e4cb15dc93f8bfea063a43e0 Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 00:03:26 +0300
Subject: [PATCH 1/7] adds error emition when `#[pointee]` used incorrectly

---
 compiler/rustc_passes/src/check_attr.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index 86b18570f376b..bca209c75c288 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -158,6 +158,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
                     self.check_rustc_std_internal_symbol(attr, span, target)
                 }
                 [sym::naked] => self.check_naked(hir_id, attr, span, target, attrs),
+                [sym:pointee] => self.check_pointee(hir_id, attr, span, target, attrs),
                 [sym::rustc_never_returns_null_ptr] => {
                     self.check_applied_to_fn_or_method(hir_id, attr, span, target)
                 }
@@ -2279,6 +2280,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
             self.abort.set(true);
         }
     }
+
+    /// Check if `#[pointee]` has been applied to a function
+    fn check_pointee(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
+        self.check_applied_to_fn_or_method(hir_id, attr, span, target)
+    }
 }
 
 impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {

From 4058d49e39de3d04e6bfca4882e163dc4a5b3b05 Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 00:03:45 +0300
Subject: [PATCH 2/7] adds tests

---
 tests/ui/attributes/pointee.rs     | 34 +++++++++++++++++++
 tests/ui/attributes/pointee.stderr | 54 ++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
 create mode 100644 tests/ui/attributes/pointee.rs
 create mode 100644 tests/ui/attributes/pointee.stderr

diff --git a/tests/ui/attributes/pointee.rs b/tests/ui/attributes/pointee.rs
new file mode 100644
index 0000000000000..1dff2d38522dd
--- /dev/null
+++ b/tests/ui/attributes/pointee.rs
@@ -0,0 +1,34 @@
+#![feature(pointee)]
+#![feature(stmt_expr_attributes)]
+#![deny(unused_attributes)]
+#![allow(dead_code)]
+
+fn invalid() {
+    #[pointee] //~ ERROR attribute should be applied to a function definition
+    {
+        1
+    };
+}
+
+#[pointee] //~ ERROR attribute should be applied to a function definition
+type InvalidTy = ();
+
+#[pointee] //~ ERROR attribute should be applied to a function definition
+mod invalid_module {}
+
+fn main() {
+    let _ = #[pointee] //~ ERROR attribute should be applied to a function definition
+    (|| 1);
+}
+
+#[pointee] //~ ERROR attribute should be applied to a function definition
+struct F;
+
+#[pointee] //~ ERROR attribute should be applied to a function definition
+impl F {
+    #[pointee]
+    fn valid(&self) {}
+}
+
+#[pointee]
+fn valid() {}
diff --git a/tests/ui/attributes/pointee.stderr b/tests/ui/attributes/pointee.stderr
new file mode 100644
index 0000000000000..030c3e342bed3
--- /dev/null
+++ b/tests/ui/attributes/pointee.stderr
@@ -0,0 +1,54 @@
+error: attribute should be applied to a function definition
+  --> $DIR/pointee.rs:7:5
+   |
+LL |       #[pointee]
+   |       ^^^^^^^^^^
+LL | /     {
+LL | |         1
+LL | |     };
+   | |_____- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/pointee.rs:13:1
+   |
+LL | #[pointee]
+   | ^^^^^^^^^^
+LL | type InvalidTy = ();
+   | -------------------- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/pointee.rs:16:1
+   |
+LL | #[pointee]
+   | ^^^^^^^^^^
+LL | mod invalid_module {}
+   | --------------------- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/pointee.rs:20:13
+   |
+LL |     let _ = #[pointee]
+   |             ^^^^^^^^^^
+LL |     (|| 1);
+   |     ------ not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/pointee.rs:24:1
+   |
+LL | #[pointee]
+   | ^^^^^^^^^^
+LL | struct F;
+   | --------- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/pointee.rs:27:1
+   |
+LL |   #[pointee]
+   |   ^^^^^^^^^^
+LL | / impl F {
+LL | |     #[pointee]
+LL | |     fn valid(&self) {}
+LL | | }
+   | |_- not a function definition
+
+error: aborting due to 6 previous errors

From 04e2511ef99e9a5312b5ced6fd8995d50a4f5e40 Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 00:18:11 +0300
Subject: [PATCH 3/7] adds a missing `:`

---
 compiler/rustc_passes/src/check_attr.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index bca209c75c288..ee8a0ff23f6eb 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -158,7 +158,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
                     self.check_rustc_std_internal_symbol(attr, span, target)
                 }
                 [sym::naked] => self.check_naked(hir_id, attr, span, target, attrs),
-                [sym:pointee] => self.check_pointee(hir_id, attr, span, target, attrs),
+                [sym::pointee] => self.check_pointee(hir_id, attr, span, target, attrs),
                 [sym::rustc_never_returns_null_ptr] => {
                     self.check_applied_to_fn_or_method(hir_id, attr, span, target)
                 }

From be3804db67b1ae664281b5d16e55d3b0c0d7e903 Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 00:31:18 +0300
Subject: [PATCH 4/7] lints

---
 compiler/rustc_passes/src/check_attr.rs | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index ee8a0ff23f6eb..e488943c562f2 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -158,7 +158,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
                     self.check_rustc_std_internal_symbol(attr, span, target)
                 }
                 [sym::naked] => self.check_naked(hir_id, attr, span, target, attrs),
-                [sym::pointee] => self.check_pointee(hir_id, attr, span, target, attrs),
+                [sym::pointee] => {
+                    self.check_applied_to_fn_or_method(hir_id, attr, span, target)
+                }
                 [sym::rustc_never_returns_null_ptr] => {
                     self.check_applied_to_fn_or_method(hir_id, attr, span, target)
                 }
@@ -2280,11 +2282,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
             self.abort.set(true);
         }
     }
-
-    /// Check if `#[pointee]` has been applied to a function
-    fn check_pointee(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
-        self.check_applied_to_fn_or_method(hir_id, attr, span, target)
-    }
 }
 
 impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {

From 36b9c9eb046f38a432aadbbd54f48d68aecfed6b Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 00:55:03 +0300
Subject: [PATCH 5/7] tidy

---
 compiler/rustc_passes/src/check_attr.rs | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index e488943c562f2..af8e24c990606 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -158,9 +158,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
                     self.check_rustc_std_internal_symbol(attr, span, target)
                 }
                 [sym::naked] => self.check_naked(hir_id, attr, span, target, attrs),
-                [sym::pointee] => {
-                    self.check_applied_to_fn_or_method(hir_id, attr, span, target)
-                }
+                [sym::pointee] => self.check_applied_to_fn_or_method(hir_id, attr, span, target),
                 [sym::rustc_never_returns_null_ptr] => {
                     self.check_applied_to_fn_or_method(hir_id, attr, span, target)
                 }

From 9914733131bf1fcbabea96fc5284b8f3d81386a4 Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 01:29:25 +0300
Subject: [PATCH 6/7] adds smart_pointer attr

---
 tests/ui/attributes/pointee.rs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tests/ui/attributes/pointee.rs b/tests/ui/attributes/pointee.rs
index 1dff2d38522dd..fffa55fe7e1c8 100644
--- a/tests/ui/attributes/pointee.rs
+++ b/tests/ui/attributes/pointee.rs
@@ -1,5 +1,6 @@
 #![feature(pointee)]
 #![feature(stmt_expr_attributes)]
+#![feature(derive_smart_pointer)]
 #![deny(unused_attributes)]
 #![allow(dead_code)]
 

From 0895bfa62fa72b93107c31df5b0b95777ef34be9 Mon Sep 17 00:00:00 2001
From: Kaloyan Gangov <gangov1@gmail.com>
Date: Sat, 3 Aug 2024 11:27:40 +0300
Subject: [PATCH 7/7] lints the stderr file

---
 tests/ui/attributes/pointee.stderr | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/tests/ui/attributes/pointee.stderr b/tests/ui/attributes/pointee.stderr
index 030c3e342bed3..54ac19ca25abb 100644
--- a/tests/ui/attributes/pointee.stderr
+++ b/tests/ui/attributes/pointee.stderr
@@ -1,5 +1,5 @@
 error: attribute should be applied to a function definition
-  --> $DIR/pointee.rs:7:5
+  --> $DIR/pointee.rs:8:5
    |
 LL |       #[pointee]
    |       ^^^^^^^^^^
@@ -9,7 +9,7 @@ LL | |     };
    | |_____- not a function definition
 
 error: attribute should be applied to a function definition
-  --> $DIR/pointee.rs:13:1
+  --> $DIR/pointee.rs:14:1
    |
 LL | #[pointee]
    | ^^^^^^^^^^
@@ -17,7 +17,7 @@ LL | type InvalidTy = ();
    | -------------------- not a function definition
 
 error: attribute should be applied to a function definition
-  --> $DIR/pointee.rs:16:1
+  --> $DIR/pointee.rs:17:1
    |
 LL | #[pointee]
    | ^^^^^^^^^^
@@ -25,7 +25,7 @@ LL | mod invalid_module {}
    | --------------------- not a function definition
 
 error: attribute should be applied to a function definition
-  --> $DIR/pointee.rs:20:13
+  --> $DIR/pointee.rs:21:13
    |
 LL |     let _ = #[pointee]
    |             ^^^^^^^^^^
@@ -33,7 +33,7 @@ LL |     (|| 1);
    |     ------ not a function definition
 
 error: attribute should be applied to a function definition
-  --> $DIR/pointee.rs:24:1
+  --> $DIR/pointee.rs:25:1
    |
 LL | #[pointee]
    | ^^^^^^^^^^
@@ -41,7 +41,7 @@ LL | struct F;
    | --------- not a function definition
 
 error: attribute should be applied to a function definition
-  --> $DIR/pointee.rs:27:1
+  --> $DIR/pointee.rs:28:1
    |
 LL |   #[pointee]
    |   ^^^^^^^^^^