Skip to content

Commit

Permalink
fix: use char index in the compiler error message
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Nov 3, 2023
1 parent c050647 commit 7c5c7c5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 4 additions & 3 deletions kclvm/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ impl SessionDiagnostic for Diagnostic {
let line = source.get_line((msg.range.0.line - 1) as usize);
match line.as_ref() {
Some(content) => {
let length = content.chars().count();
let snippet = Snippet {
title: None,
footer: vec![],
Expand All @@ -428,12 +429,12 @@ impl SessionDiagnostic for Diagnostic {
origin: Some(&msg.range.0.filename),
annotations: vec![SourceAnnotation {
range: match msg.range.0.column {
Some(column) if content.len() >= 1 => {
Some(column) if length >= 1 => {
let column = column as usize;
// If the position exceeds the length of the content,
// put the annotation at the end of the line.
if column >= content.len() {
(content.len() - 1, content.len())
if column >= length {
(length - 1, length)
} else {
(column, column + 1)
}
Expand Down
1 change: 1 addition & 0 deletions kclvm/parser/src/tests/error_recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ parse_expr_snapshot! { config_recovery_11, "{if True: a = , b = 2}" }
parse_expr_snapshot! { config_recovery_12, "{if True: *a, b = 2}" }
parse_expr_snapshot! { config_recovery_13, "{if True: key: {}}" }
parse_expr_snapshot! { config_recovery_14, "{if True: key: []}" }
parse_expr_snapshot! { config_recovery_15, "{你好" }
parse_expr_snapshot! { comp_clause_recovery_0, "[i for i in [1,2,3]]" }
parse_expr_snapshot! { comp_clause_recovery_1, "[i, j for i in [1,2,3]]" }
parse_expr_snapshot! { comp_clause_recovery_2, "[for i in [1,2,3]]" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
source: parser/src/tests/error_recovery.rs
assertion_line: 73
expression: "crate::tests::parsing_expr_string(\"{你好\")"
---
Node {
node: Config(
ConfigExpr {
items: [
Node {
node: ConfigEntry {
key: Some(
Node {
node: Identifier(
Identifier {
names: [
Node {
node: "你好",
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 3,
},
],
pkgpath: "",
ctx: Load,
},
),
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 3,
},
),
value: Node {
node: Missing(
MissingExpr,
),
filename: "",
line: 1,
column: 3,
end_line: 1,
end_column: 3,
},
operation: Override,
insert_index: -1,
},
filename: "",
line: 1,
column: 1,
end_line: 1,
end_column: 3,
},
],
},
),
filename: "",
line: 1,
column: 0,
end_line: 1,
end_column: 3,
}

0 comments on commit 7c5c7c5

Please sign in to comment.