about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2023-04-30 16:05:09 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2023-05-03 17:55:27 +0000
commit83453408a0ce91b9e3d3ae6e7f117b1fd28b487d (patch)
tree1c348a320184de4961cf5051ae3c6ceca1e91cf4
parent89158e210ce0b9550b42c65ede244087f2e144b5 (diff)
downloadrust-83453408a0ce91b9e3d3ae6e7f117b1fd28b487d.tar.gz
rust-83453408a0ce91b9e3d3ae6e7f117b1fd28b487d.zip
Validate resolution for SelfCtor too.
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs2
-rw-r--r--compiler/rustc_resolve/src/ident.rs5
-rw-r--r--tests/ui/self/self-ctor-inner-const.rs17
-rw-r--r--tests/ui/self/self-ctor-inner-const.stderr33
4 files changed, 55 insertions, 2 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index 4b7048eac04..ed8c3b60c1f 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -550,7 +550,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
 
                 let sm = self.tcx.sess.source_map();
                 let def_id = match outer_res {
-                    Res::SelfTyParam { .. } => {
+                    Res::SelfTyParam { .. } | Res::SelfCtor(_) => {
                         err.span_label(span, "can't use `Self` here");
                         return err;
                     }
diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs
index 5a56d7b99a9..72a71599236 100644
--- a/compiler/rustc_resolve/src/ident.rs
+++ b/compiler/rustc_resolve/src/ident.rs
@@ -1171,7 +1171,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
                     return Res::Err;
                 }
             }
-            Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
+            Res::Def(DefKind::TyParam, _)
+            | Res::SelfTyParam { .. }
+            | Res::SelfTyAlias { .. }
+            | Res::SelfCtor(_) => {
                 for rib in ribs {
                     let has_generic_params: HasGenericParams = match rib.kind {
                         NormalRibKind
diff --git a/tests/ui/self/self-ctor-inner-const.rs b/tests/ui/self/self-ctor-inner-const.rs
new file mode 100644
index 00000000000..b015397a5bc
--- /dev/null
+++ b/tests/ui/self/self-ctor-inner-const.rs
@@ -0,0 +1,17 @@
+// Verify that we ban usage of `Self` as constructor from inner items.
+
+struct S0<T>(T);
+
+impl<T> S0<T> {
+    fn foo() {
+        const C: S0<u8> = Self(0);
+        //~^ ERROR can't use generic parameters from outer function
+        fn bar() -> Self {
+            //~^ ERROR can't use generic parameters from outer function
+            Self(0)
+            //~^ ERROR can't use generic parameters from outer function
+        }
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/self/self-ctor-inner-const.stderr b/tests/ui/self/self-ctor-inner-const.stderr
new file mode 100644
index 00000000000..7287c64c659
--- /dev/null
+++ b/tests/ui/self/self-ctor-inner-const.stderr
@@ -0,0 +1,33 @@
+error[E0401]: can't use generic parameters from outer function
+  --> $DIR/self-ctor-inner-const.rs:7:27
+   |
+LL |         const C: S0<u8> = Self(0);
+   |                           ^^^^
+   |                           |
+   |                           use of generic parameter from outer function
+   |                           can't use `Self` here
+
+error[E0401]: can't use generic parameters from outer function
+  --> $DIR/self-ctor-inner-const.rs:9:21
+   |
+LL | impl<T> S0<T> {
+   | ---- `Self` type implicitly declared here, by this `impl`
+...
+LL |         fn bar() -> Self {
+   |                     ^^^^
+   |                     |
+   |                     use of generic parameter from outer function
+   |                     use a type here instead
+
+error[E0401]: can't use generic parameters from outer function
+  --> $DIR/self-ctor-inner-const.rs:11:13
+   |
+LL |             Self(0)
+   |             ^^^^
+   |             |
+   |             use of generic parameter from outer function
+   |             can't use `Self` here
+
+error: aborting due to 3 previous errors
+
+For more information about this error, try `rustc --explain E0401`.