Skip to content

Commit 0ce616f

Browse files
committed
fix(parser): empty span of unbalanced delimiter
1 parent f3d92e3 commit 0ce616f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

crates/nu-command/tests/commands/let_.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,12 @@ fn let_raw_string() {
123123
let actual = nu!(r#"let x = r#'abc'#; $x"#);
124124
assert_eq!(actual.out, "abc");
125125
}
126+
127+
#[test]
128+
fn let_malformed_type() {
129+
let actual = nu!("let foo: )a");
130+
assert!(actual.err.contains("unbalanced ( and )"));
131+
132+
let actual = nu!("let foo: }a");
133+
assert!(actual.err.contains("unbalanced { and }"));
134+
}

crates/nu-parser/src/lex.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ pub fn lex_item(
194194
} else {
195195
// We encountered a closing `}` delimiter, but the last opening
196196
// delimiter was not a `{`. This is an error.
197+
*curr_offset += 1;
197198
let span = Span::new(span_offset + token_start, span_offset + *curr_offset);
198199

199-
*curr_offset += 1;
200200
return (
201201
Token {
202202
contents: TokenContents::Item,
@@ -205,7 +205,7 @@ pub fn lex_item(
205205
Some(ParseError::Unbalanced(
206206
"{".to_string(),
207207
"}".to_string(),
208-
Span::new(span.end, span.end + 1),
208+
Span::new(span.end - 1, span.end),
209209
)),
210210
);
211211
}
@@ -219,9 +219,9 @@ pub fn lex_item(
219219
} else {
220220
// We encountered a closing `)` delimiter, but the last opening
221221
// delimiter was not a `(`. This is an error.
222+
*curr_offset += 1;
222223
let span = Span::new(span_offset + token_start, span_offset + *curr_offset);
223224

224-
*curr_offset += 1;
225225
return (
226226
Token {
227227
contents: TokenContents::Item,
@@ -230,7 +230,7 @@ pub fn lex_item(
230230
Some(ParseError::Unbalanced(
231231
"(".to_string(),
232232
")".to_string(),
233-
Span::new(span.end, span.end + 1),
233+
Span::new(span.end - 1, span.end),
234234
)),
235235
);
236236
}

0 commit comments

Comments
 (0)