about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-05-03 08:45:03 +0200
committerGitHub <noreply@github.com>2025-05-03 08:45:03 +0200
commit6cf4fd32bb82deeb86a28c9342b5f489be57609c (patch)
treec02e4cf6eaadbcd0bd67c31aaefcd8fddcc57745
parentb803f45e1f7dfe117a3a945702d3114e1d5f2ba5 (diff)
parentbfe3d54d817cd66e4ab85d94409db8a572fdadd7 (diff)
downloadrust-6cf4fd32bb82deeb86a28c9342b5f489be57609c.tar.gz
rust-6cf4fd32bb82deeb86a28c9342b5f489be57609c.zip
Rollup merge of #140548 - BoxyUwU:gci_patterns_user_ty_annotation, r=compiler-errors
Emit user type annotations for free consts in pattern position

This previously wasnt done because free consts couldn't have any generic parameters that need to be preserved for borrowck. This is no longer the case with `feature(generic_const_items)`

r? fmease
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/mod.rs3
-rw-r--r--tests/ui/generic-const-items/user_type_annotations_pattern.rs14
-rw-r--r--tests/ui/generic-const-items/user_type_annotations_pattern.stderr11
3 files changed, 26 insertions, 2 deletions
diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
index 8e69ff568b9..4f00a85004d 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
@@ -553,8 +553,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
         let res = self.typeck_results.qpath_res(qpath, id);
 
         let (def_id, user_ty) = match res {
-            Res::Def(DefKind::Const, def_id) => (def_id, None),
-            Res::Def(DefKind::AssocConst, def_id) => {
+            Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => {
                 (def_id, self.typeck_results.user_provided_types().get(id))
             }
 
diff --git a/tests/ui/generic-const-items/user_type_annotations_pattern.rs b/tests/ui/generic-const-items/user_type_annotations_pattern.rs
new file mode 100644
index 00000000000..aa3846df2bc
--- /dev/null
+++ b/tests/ui/generic-const-items/user_type_annotations_pattern.rs
@@ -0,0 +1,14 @@
+#![feature(generic_const_items)]
+#![expect(incomplete_features)]
+
+const FOO<'a: 'static>: usize = 10;
+
+fn bar<'a>() {
+    match 10_usize {
+        FOO::<'a> => todo!(),
+        //~^ ERROR: lifetime may not live long enough
+        _ => todo!(),
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/generic-const-items/user_type_annotations_pattern.stderr b/tests/ui/generic-const-items/user_type_annotations_pattern.stderr
new file mode 100644
index 00000000000..e15be275d29
--- /dev/null
+++ b/tests/ui/generic-const-items/user_type_annotations_pattern.stderr
@@ -0,0 +1,11 @@
+error: lifetime may not live long enough
+  --> $DIR/user_type_annotations_pattern.rs:8:9
+   |
+LL | fn bar<'a>() {
+   |        -- lifetime `'a` defined here
+LL |     match 10_usize {
+LL |         FOO::<'a> => todo!(),
+   |         ^^^^^^^^^ requires that `'a` must outlive `'static`
+
+error: aborting due to 1 previous error
+