about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-04-04 12:54:56 -0400
committerMichael Goulet <michael@errs.io>2024-04-15 16:45:01 -0400
commit42ba57c0133306ff510bf641e9d90b33419d7ac7 (patch)
treedf7fe9e9b5b5976f17da016354e838a87fe5cd3d /compiler/rustc_resolve/src
parent41cf87b71b792b40155cd79f96234a89ae7dc27f (diff)
downloadrust-42ba57c0133306ff510bf641e9d90b33419d7ac7.tar.gz
rust-42ba57c0133306ff510bf641e9d90b33419d7ac7.zip
Validation and other things
Diffstat (limited to 'compiler/rustc_resolve/src')
-rw-r--r--compiler/rustc_resolve/src/late.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs
index c7e12284b50..7203adab135 100644
--- a/compiler/rustc_resolve/src/late.rs
+++ b/compiler/rustc_resolve/src/late.rs
@@ -1054,7 +1054,10 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
 
     fn visit_precise_capturing_arg(&mut self, arg: &'ast PreciseCapturingArg) {
         match arg {
+            // Lower the lifetime regularly; we'll resolve the lifetime and check
+            // it's a parameter later on in HIR lowering.
             PreciseCapturingArg::Lifetime(_) => visit::walk_precise_capturing_arg(self, arg),
+
             PreciseCapturingArg::Arg(ident, node_id) => {
                 let ident = ident.normalize_to_macros_2_0();
                 'found: {
@@ -1064,6 +1067,38 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
                         if let Some(res) = rib_t.bindings.get(&ident).or(rib_v.bindings.get(&ident))
                         {
                             self.r.record_partial_res(*node_id, PartialRes::new(*res));
+
+                            // Validate that this is a parameter
+                            match res {
+                                Res::Def(DefKind::TyParam | DefKind::ConstParam, _)
+                                | Res::SelfTyParam { .. } => {}
+                                Res::SelfTyAlias { .. } => {
+                                    self.report_error(
+                                        ident.span,
+                                        ResolutionError::FailedToResolve {
+                                            segment: Some(ident.name),
+                                            label: "`Self` cannot be captured because it is not a type parameter".to_string(),
+                                            suggestion: None,
+                                            module: None,
+                                        },
+                                    );
+                                }
+                                _ => {
+                                    self.report_error(
+                                        ident.span,
+                                        ResolutionError::FailedToResolve {
+                                            segment: Some(ident.name),
+                                            label: format!(
+                                                "expected type or const parameter, found {}",
+                                                res.descr()
+                                            ),
+                                            suggestion: None,
+                                            module: None,
+                                        },
+                                    );
+                                }
+                            }
+
                             break 'found;
                         }
                     }