FIX: do not allow *start > end* in error make spans (#8570)

This should close #8567.

# Description
this PR throws an error when `start > end` in the most complete branch
of `ErrorMake::run`, i.e. when `$.msg`, `$.label.text`, `$.label.start`
and `$.label.end` are defined.

i've also added a `error_start_bigger_than_end_should_fail` test to
check that it does indeed return the right error.

# User-Facing Changes
no more crash when manipulating span bounds and a clear error, e.g.
```bash
>_ error make {msg: "msg" label: {text: "text" start: 1010 end: 1000}}
Error:
  × invalid error format.
   ╭─[entry #3:1:1]
 1 │ error make {msg: "msg" label: {text: "text" start: 1010 end: 1000}}
   ·                               ──────────────────┬─────────────────
   ·                                                 ╰── `$.label.start` is stricly bigger than `$.label.end`
   ╰────
  help: 1010 > 1000
```
or
```bash
>_ error make {
:::     msg: "msg"
:::     label: {
:::         text: "text"
:::         start: ($nu.scope.engine_state.source_bytes - 90)
:::         end: ($nu.scope.engine_state.source_bytes - 100)
:::     }
::: }
Error:
  × invalid error format.
   ╭─[entry #4:2:1]
 2 │         msg: "msg"
 3 │ ╭─▶     label: {
 4 │ │           text: "text"
 5 │ │           start: ($nu.scope.engine_state.source_bytes - 90)
 6 │ │           end: ($nu.scope.engine_state.source_bytes - 100)
 7 │ ├─▶     }
   · ╰──── `$.label.start` is stricly bigger than `$.label.end`
 8 │     }
   ╰────
  help: 204525 > 204515
```

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🔴 `toolkit test`

# After Submitting
```
$nothing
```
This commit is contained in:
Antoine Stevan 2023-03-23 20:31:06 +01:00 committed by GitHub
parent 5c2a767987
commit 05ff7a9925
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View file

@ -134,13 +134,25 @@ fn make_error(value: &Value, throw_span: Option<Span>) -> Option<ShellError> {
Some(Value::String {
val: label_text, ..
}),
) => Some(ShellError::GenericError(
message,
label_text,
Some(Span::new(start as usize, end as usize)),
None,
Vec::new(),
)),
) => {
if start > end {
Some(ShellError::GenericError(
"invalid error format.".into(),
"`$.label.start` should be smaller than `$.label.end`".into(),
label_span,
Some(format!("{} > {}", start, end)),
Vec::new(),
))
} else {
Some(ShellError::GenericError(
message,
label_text,
Some(Span::new(start as usize, end as usize)),
None,
Vec::new(),
))
}
}
(
None,
None,

View file

@ -24,3 +24,18 @@ fn no_span_if_unspanned() {
assert!(!actual.err.contains("unseen"));
}
#[test]
fn error_start_bigger_than_end_should_fail() {
let actual = nu!(
cwd: ".", pipeline(
r#"
error make {msg: foo label: {text: bar start 456 end 123}}
"#
));
assert!(!actual.err.contains("invalid error format"));
assert!(!actual
.err
.contains("`$.label.start` should be smaller than `$.label.end`"));
}