diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 715afb5bad..49895c4641 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -6534,7 +6534,12 @@ pub fn discover_captures_in_expr( if !seen.contains(var_id) { if let Some(variable) = working_set.get_variable_if_possible(*var_id) { if variable.mutable { - return Err(ParseError::CaptureOfMutableVar(*span)); + let var_name = working_set.get_span_contents(*span); + return Err(ParseError::CaptureOfMutableVar { + var: String::from_utf8_lossy(var_name).into_owned(), + var_span: span.before(), + closure_span: block.span, + }); } } } diff --git a/crates/nu-protocol/src/errors/parse_error.rs b/crates/nu-protocol/src/errors/parse_error.rs index 34a481a113..6ab63a5793 100644 --- a/crates/nu-protocol/src/errors/parse_error.rs +++ b/crates/nu-protocol/src/errors/parse_error.rs @@ -145,9 +145,15 @@ pub enum ParseError { help: Option<&'static str>, }, - #[error("Capture of mutable variable.")] - #[diagnostic(code(nu::parser::expected_keyword))] - CaptureOfMutableVar(#[label("capture of mutable variable")] Span), + #[error("Cannot capture mutable variables inside closures.")] + #[diagnostic(code(nu::parser::mutable_capture))] + CaptureOfMutableVar { + var: String, + #[label("could not capture within this closure")] + closure_span: Option, + #[label("attempted to capture mutable variable {var}")] + var_span: Span, + }, #[error("Expected keyword.")] #[diagnostic(code(nu::parser::expected_keyword))] @@ -570,7 +576,7 @@ impl ParseError { ParseError::BuiltinCommandInPipeline(_, s) => *s, ParseError::AssignInPipeline(_, _, _, s) => *s, ParseError::NameIsBuiltinVar(_, s) => *s, - ParseError::CaptureOfMutableVar(s) => *s, + ParseError::CaptureOfMutableVar { var_span, .. } => *var_span, ParseError::IncorrectValue(_, s, _) => *s, ParseError::MultipleRestParams(s) => *s, ParseError::VariableNotFound(_, s) => *s, diff --git a/crates/nu-protocol/src/span.rs b/crates/nu-protocol/src/span.rs index 77e5bab681..f798262dca 100644 --- a/crates/nu-protocol/src/span.rs +++ b/crates/nu-protocol/src/span.rs @@ -140,6 +140,14 @@ impl Span { self.start <= span.start && span.end <= self.end && span.end != 0 } + /// Point to the space just before this span + pub fn before(&self) -> Self { + Self { + start: self.start, + end: self.start, + } + } + /// Point to the space just past this span, useful for missing values pub fn past(&self) -> Self { Self {