Skip to content

Commit

Permalink
[move-compiler-v2] Print a better error message for a string without …
Browse files Browse the repository at this point in the history
…b or x (#14687)

Fixes #14364.
  • Loading branch information
brmataptos authored Sep 23, 2024
1 parent e87224f commit 208988e
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

Diagnostics:
error: invalid hex string
┌─ tests/folding/constants_hexstring.move:6:29
6 │ const C12: vector<u8> = x"hello";
│ ^^^^^^^^ Odd number of characters in hex string. Expected 2 hexadecimal digits for each byte

error: invalid hex string
┌─ tests/folding/constants_hexstring.move:7:31
7 │ const C13: vector<u8> = x"hello!";
│ ^ Invalid hexadecimal character: 'h'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
address 0x42 {
module M {
fun u(): u64 { 0 }

const C11: vector<u8> = x"deadbeef";
const C12: vector<u8> = x"hello";
const C13: vector<u8> = x"hello!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Diagnostics:
error: invalid byte string
┌─ tests/folding/constants_quoted_string.move:5:29
5 │ const C12: vector<u8> = "<empty>";
│ ^ String literal must begin with b" (for a byte string) or x" (for a hex string)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
address 0x42 {
module M {
fun u(): u64 { 0 }

const C12: vector<u8> = "<empty>";
const C13: vector<u8> = "foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module M {
const C7: u256 = 4 / 3 + 4 - 1 << 143;
const C8: u16 = 123;
const C9: u32 = (453 as u32);

const C10: vector<u8> = b"<empty>";
const C11: vector<u8> = x"deadbeef";
const C12: vector<u8> = b"\"foo\"";
const C13: vector<u8> = b"\"\x48";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Diagnostics:
error: invalid character
┌─ tests/folding/constants_single_quote.move:4:29
4 │ const C13: vector<u8> = "foo
│ ^ Invalid character: '"'; string literal must begin with `b"` and closing quote `"` must appear on same line
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
address 0x42 {
module M {
fun u(): u64 { 0 }
const C13: vector<u8> = "foo
";
const C14: vector<u8> = "bar";
const C15: vector<u8> = b"baz";
const C16: vector<u8> = x"quux";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Diagnostics:
error: invalid character
┌─ tests/folding/constants_string_non_utf8.move:5:31
5 │ const C12: vector<u8> = b"ÿ";
│ ^ Invalid character 'ÿ' found when reading file. Only ASCII printable characters, tabs (\t), lf (\n) and crlf (\r\n) are permitted.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
address 0x42 {
module M {
fun u(): u64 { 0 }

const C12: vector<u8> = b"ÿ";
}
}
19 changes: 19 additions & 0 deletions third_party/move/move-compiler/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,25 @@ fn find_token(
(get_name_token(&text[..len]), len)
}
},
'"' => {
let line = &text.lines().next().unwrap()[1..];
match get_string_len(line) {
Some(_last_quote) => {
let loc = make_loc(file_hash, start_offset, start_offset);
return Err(Box::new(diag!(
Syntax::InvalidByteString,
(loc, "String literal must begin with b\" (for a byte string) or x\" (for a hex string)")
)));
},
None => {
let loc = make_loc(file_hash, start_offset, start_offset);
return Err(Box::new(diag!(
Syntax::InvalidCharacter,
(loc, format!("Invalid character: '{}'; string literal must begin with `b\"` and closing quote `\"` must appear on same line", c))
)));
},
}
},
'&' => {
if text.starts_with("&mut ") {
(Tok::AmpMut, 5)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E01008]: invalid hex string
┌─ tests/move_check/parser/constants_hexstring.move:6:29
6 │ const C12: vector<u8> = x"hello";
│ ^^^^^^^^ Odd number of characters in hex string. Expected 2 hexadecimal digits for each byte

error[E08001]: cannot compute constant value
┌─ tests/move_check/parser/constants_hexstring.move:6:29
6 │ const C12: vector<u8> = x"hello";
│ ^^^^^^^^ Invalid expression in 'const'. This expression could not be evaluated to a value

error[E08001]: cannot compute constant value
┌─ tests/move_check/parser/constants_hexstring.move:7:29
7 │ const C13: vector<u8> = x"hello!";
│ ^^^^^^^^^ Invalid expression in 'const'. This expression could not be evaluated to a value

error[E01008]: invalid hex string
┌─ tests/move_check/parser/constants_hexstring.move:7:31
7 │ const C13: vector<u8> = x"hello!";
│ ^ Invalid hexadecimal character: 'h'

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
address 0x42 {
module M {
fun u(): u64 { 0 }

const C11: vector<u8> = x"deadbeef";
const C12: vector<u8> = x"hello";
const C13: vector<u8> = x"hello!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E01007]: invalid byte string
┌─ tests/move_check/parser/constants_quoted_string.move:5:29
5 │ const C12: vector<u8> = "<empty>";
│ ^ String literal must begin with b" (for a byte string) or x" (for a hex string)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
address 0x42 {
module M {
fun u(): u64 { 0 }

const C12: vector<u8> = "<empty>";
const C13: vector<u8> = "foo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module M {
const C7: u256 = 4 / 3 + 4 - 1 << 143;
const C8: u16 = 123;
const C9: u32 = (453 as u32);

const C10: vector<u8> = b"<empty>";
const C11: vector<u8> = x"deadbeef";
const C12: vector<u8> = b"\"foo\"";
const C13: vector<u8> = b"\"\x48";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E01001]: invalid character
┌─ tests/move_check/parser/constants_single_quote.move:4:29
4 │ const C13: vector<u8> = "foo
│ ^ Invalid character: '"'; string literal must begin with `b"` and closing quote `"` must appear on same line

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
address 0x42 {
module M {
fun u(): u64 { 0 }
const C13: vector<u8> = "foo
";
const C14: vector<u8> = "bar";
const C15: vector<u8> = b"baz";
const C16: vector<u8> = x"quux";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error[E01001]: invalid character
┌─ tests/move_check/parser/constants_string_non_utf8.move:5:31
5 │ const C12: vector<u8> = b"ÿ";
│ ^ Invalid character 'ÿ' found when reading file. Only ASCII printable characters, tabs (\t), lf (\n) and crlf (\r\n) are permitted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
address 0x42 {
module M {
fun u(): u64 { 0 }

const C12: vector<u8> = b"ÿ";
}
}

0 comments on commit 208988e

Please sign in to comment.