Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix code snippet for old diagnostics #6507

Merged
merged 8 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 92 additions & 31 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,43 +716,104 @@ fn construct_window<'a>(
let total_lines_of_highlight = end.line - start.line;
debug_assert!(total_lines_in_input >= total_lines_of_highlight);

let mut current_line = 0;
let mut lines_to_start_of_snippet = 0;
let mut calculated_start_ix = None;
let mut calculated_end_ix = None;
let mut pos = 0;
for character in input.chars() {
let mut current_line = 1usize;

xunilrj marked this conversation as resolved.
Show resolved Hide resolved
let mut chars = input.char_indices().map(|(char_offset, character)| {
let r = (current_line, char_offset);
if character == '\n' {
current_line += 1
current_line += 1;
}
r
});

if current_line + NUM_LINES_BUFFER >= start.line && calculated_start_ix.is_none() {
calculated_start_ix = Some(pos);
lines_to_start_of_snippet = current_line;
}
// Find the first char of the first line
let first_char = chars
.by_ref()
.find(|(current_line, _)| current_line + NUM_LINES_BUFFER >= start.line);

if current_line >= end.line + NUM_LINES_BUFFER && calculated_end_ix.is_none() {
calculated_end_ix = Some(pos);
}
// Find the last char of the last line
let last_char = chars
.by_ref()
.find(|(current_line, _)| *current_line > end.line + NUM_LINES_BUFFER)
.map(|x| x.1);

if calculated_start_ix.is_some() && calculated_end_ix.is_some() {
break;
// this releases the borrow of `current_line`
drop(chars);

let (first_char_line, first_char_offset, last_char_offset) = match (first_char, last_char) {
// has first and last
(Some((first_char_line, first_char_offset)), Some(last_char_offset)) => {
(first_char_line, first_char_offset, last_char_offset)
}
pos += character.len_utf8();
// has first and no last
(Some((first_char_line, first_char_offset)), None) => {
(first_char_line, first_char_offset, input.len())
}
// others
_ => (current_line, input.len(), input.len()),
};

IGI-111 marked this conversation as resolved.
Show resolved Hide resolved
// adjust indices to be inside the returned window
start.line = first_char_line;
*start_ix = start_ix.saturating_sub(first_char_offset);
*end_ix = end_ix.saturating_sub(first_char_offset);

&input[first_char_offset..last_char_offset]
}

#[test]
fn ok_construct_window() {
fn t(
start_line: usize,
start_col: usize,
end_line: usize,
end_col: usize,
start_char: usize,
end_char: usize,
input: &str,
) -> (usize, usize, &str) {
let mut s = LineCol {
line: start_line,
col: start_col,
};
let mut start = start_char;
let mut end = end_char;
let r = construct_window(
&mut s,
LineCol {
line: end_line,
col: end_col,
},
&mut start,
&mut end,
input,
);
(start, end, r)
}
let calculated_start_ix = calculated_start_ix.unwrap_or(0);
let calculated_end_ix = calculated_end_ix.unwrap_or(input.len());

let start_ix_bytes = *start_ix - std::cmp::min(calculated_start_ix, *start_ix);
let end_ix_bytes = *end_ix - std::cmp::min(calculated_start_ix, *end_ix);
// We want the start_ix and end_ix in terms of chars and not bytes, so translate.
*start_ix = input[calculated_start_ix..(calculated_start_ix + start_ix_bytes)]
.chars()
.count();
*end_ix = input[calculated_start_ix..(calculated_start_ix + end_ix_bytes)]
.chars()
.count();

start.line = lines_to_start_of_snippet;
&input[calculated_start_ix..calculated_end_ix]
// Invalid Empty file
assert_eq!(t(0, 0, 0, 0, 0, 0, ""), (0, 0, ""));

// Valid Empty File
assert_eq!(t(1, 1, 1, 1, 0, 0, ""), (0, 0, ""));

// One line, error after the last char
assert_eq!(t(1, 7, 1, 7, 6, 6, "script"), (6, 6, "script"));

// 01 23 45 67 89 AB CD E
let eight_lines = "1\n2\n3\n4\n5\n6\n7\n8";

assert_eq!(t(1, 1, 1, 1, 0, 1, eight_lines), (0, 1, "1\n2\n3\n"));
assert_eq!(t(2, 1, 2, 1, 2, 3, eight_lines), (2, 3, "1\n2\n3\n4\n"));
assert_eq!(t(3, 1, 3, 1, 4, 5, eight_lines), (4, 5, "1\n2\n3\n4\n5\n"));
assert_eq!(t(4, 1, 4, 1, 6, 7, eight_lines), (4, 5, "2\n3\n4\n5\n6\n"));
assert_eq!(t(5, 1, 5, 1, 8, 9, eight_lines), (4, 5, "3\n4\n5\n6\n7\n"));
assert_eq!(t(6, 1, 6, 1, 10, 11, eight_lines), (4, 5, "4\n5\n6\n7\n8"));
assert_eq!(t(7, 1, 7, 1, 12, 13, eight_lines), (4, 5, "5\n6\n7\n8"));
assert_eq!(t(8, 1, 8, 1, 14, 15, eight_lines), (4, 5, "6\n7\n8"));

// Invalid lines
assert_eq!(t(9, 1, 9, 1, 14, 15, eight_lines), (2, 3, "7\n8"));
assert_eq!(t(10, 1, 10, 1, 14, 15, eight_lines), (0, 1, "8"));
assert_eq!(t(11, 1, 11, 1, 14, 15, eight_lines), (0, 0, ""));
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stderr:
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:41:27
|
39 |
39 | // unexpected Option<u8>
40 | let a = [None, Some(1), Some(1u8)];
41 | let _b: Option<u16> = a[1];
| ^^^^ Mismatched types.
Expand All @@ -27,7 +27,7 @@ ____
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:45:18
|
43 |
43 | // unexpected u8
44 | let a = [8, 256u16, 8u8];
45 | let b: u32 = a[2];
| ^^^^ Mismatched types.
Expand All @@ -42,7 +42,7 @@ ____
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:9:22
|
7 |
7 | fn main() {
8 | // unexpected u16
9 | let a: [u8;1] = [1u16];
| ^^^^ Mismatched types.
Expand All @@ -65,13 +65,14 @@ expected: u8
found: u16.

13 | let a = [1, 2u8, 3u16, 4u32, 5u64];
14 |
|
____

error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:13:22
|
11 |
11 | // unexpected u16
12 | let _ = [1u8, 1u16];
13 | let a = [1, 2u8, 3u16, 4u32, 5u64];
| ^^^^ Mismatched types.
Expand All @@ -86,7 +87,7 @@ ____
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:13:28
|
11 |
11 | // unexpected u16
12 | let _ = [1u8, 1u16];
13 | let a = [1, 2u8, 3u16, 4u32, 5u64];
| ^^^^ Mismatched types.
Expand All @@ -101,7 +102,7 @@ ____
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:13:34
|
11 |
11 | // unexpected u16
12 | let _ = [1u8, 1u16];
13 | let a = [1, 2u8, 3u16, 4u32, 5u64];
| ^^^^ Mismatched types.
Expand Down Expand Up @@ -221,7 +222,7 @@ ____
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:32:20
|
30 |
30 | // unexpected str
31 | let _ = [1, "", 1u16];
32 | let _ = [1, 2, "hello"];
| ^^^^^^^ Mismatched types.
Expand All @@ -236,21 +237,22 @@ ____
error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:33:25
|
31 |
31 | let _ = [1, "", 1u16];
32 | let _ = [1, 2, "hello"];
33 | let _ = [1, return, "", 1u16];
| ^^ Mismatched types.
expected: u16
found: str.

34 | let _ = [1, "", return, 1u16];
35 |
|
____

error
--> test/src/e2e_vm_tests/test_programs/should_fail/array_wrong_elements_types/src/main.sw:34:17
|
32 |
32 | let _ = [1, 2, "hello"];
33 | let _ = [1, return, "", 1u16];
34 | let _ = [1, "", return, 1u16];
| ^^ Mismatched types.
Expand Down Expand Up @@ -288,6 +290,7 @@ expected: u16
found: u8.

45 | let b: u32 = a[2];
46 |
|
____

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ category = "fail"
# check: $()None::<T>;
# nextln: $()Could not find symbol "T" in this scope.

# check: $()error
# check: $()None::<T>;
# nextln: $()Unknown type name "T"

# check: $()error
# check: $()foo()
# nextln: $()Cannot infer type for type parameter "T". Insufficient type information provided. Try annotating its type.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ stderr:
error
--> test/src/e2e_vm_tests/test_programs/should_fail/type_check_analyze_errors/src/main.sw:5:14
|
3 |
3 | fn main() {
4 | // 0x100 does not fit into a u8
5 | let _a = 0x100;
| ^^^^^ Literal would overflow because its value does not fit into "u8"
6 | Vec::<u8>::new().push(_a);
7 |
|
____

Expand All @@ -28,6 +29,7 @@ error
9 | let _a = 0x10000;
| ^^^^^^^ Literal would overflow because its value does not fit into "u16"
10 | Vec::<u16>::new().push(_a);
11 |
|
____

Expand All @@ -39,6 +41,7 @@ error
13 | let _a = 0x100000000;
| ^^^^^^^^^^^ Literal would overflow because its value does not fit into "u32"
14 | Vec::<u32>::new().push(_a);
15 |
|
____

Expand Down
Loading