Start improving mutable capture error

This commit is contained in:
132ikl 2025-03-03 18:20:56 -05:00
parent 9eaa8908d2
commit 9ed2b2df00
3 changed files with 24 additions and 5 deletions

View File

@ -6522,7 +6522,12 @@ pub fn discover_captures_in_expr(
if !seen.contains(var_id) { if !seen.contains(var_id) {
if let Some(variable) = working_set.get_variable_if_possible(*var_id) { if let Some(variable) = working_set.get_variable_if_possible(*var_id) {
if variable.mutable { 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,
});
} }
} }
} }

View File

@ -145,9 +145,15 @@ pub enum ParseError {
help: Option<&'static str>, help: Option<&'static str>,
}, },
#[error("Capture of mutable variable.")] #[error("Cannot capture mutable variables inside closures.")]
#[diagnostic(code(nu::parser::expected_keyword))] #[diagnostic(code(nu::parser::mutable_capture))]
CaptureOfMutableVar(#[label("capture of mutable variable")] Span), CaptureOfMutableVar {
var: String,
#[label("could not capture within this closure")]
closure_span: Option<Span>,
#[label("attempted to capture mutable variable {var}")]
var_span: Span,
},
#[error("Expected keyword.")] #[error("Expected keyword.")]
#[diagnostic(code(nu::parser::expected_keyword))] #[diagnostic(code(nu::parser::expected_keyword))]
@ -570,7 +576,7 @@ impl ParseError {
ParseError::BuiltinCommandInPipeline(_, s) => *s, ParseError::BuiltinCommandInPipeline(_, s) => *s,
ParseError::AssignInPipeline(_, _, _, s) => *s, ParseError::AssignInPipeline(_, _, _, s) => *s,
ParseError::NameIsBuiltinVar(_, s) => *s, ParseError::NameIsBuiltinVar(_, s) => *s,
ParseError::CaptureOfMutableVar(s) => *s, ParseError::CaptureOfMutableVar { var_span, .. } => *var_span,
ParseError::IncorrectValue(_, s, _) => *s, ParseError::IncorrectValue(_, s, _) => *s,
ParseError::MultipleRestParams(s) => *s, ParseError::MultipleRestParams(s) => *s,
ParseError::VariableNotFound(_, s) => *s, ParseError::VariableNotFound(_, s) => *s,

View File

@ -140,6 +140,14 @@ impl Span {
self.start <= span.start && span.end <= self.end && span.end != 0 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 /// Point to the space just past this span, useful for missing values
pub fn past(&self) -> Self { pub fn past(&self) -> Self {
Self { Self {