Skip to content
10 changes: 10 additions & 0 deletions crates/sema/src/ty/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ impl<'gcx> Ty<'gcx> {
Result::Err(TyConvertError::NonDerivedContract)
}
}
// byte literal -> bytesN/bytes
// See: <https://docs.soliditylang.org/en/latest/types.html#index-34>
(StringLiteral(_, _), Elementary(Bytes)) => Ok(()),
(StringLiteral(_, size_from), Elementary(FixedBytes(size_to))) => {
if size_from.bytes() <= size_to.bytes() {
Ok(())
} else {
Result::Err(TyConvertError::InvalidConversion)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a new variant to match the solc error message?

}
}

// TODO: more implicit conversions
_ => Result::Err(TyConvertError::Incompatible),
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/typeck/literal_bytes_implicit_conversion.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@compile-flags: -Ztypeck
contract C {
function f() public pure {
// --- Valid: literal to fixed-size bytes (equal size) ---
bytes3 b3_1 = "abc";
bytes3 b3_2 = hex"123456";

// --- Valid: literal to fixed-size bytes (larger size) ---
bytes10 b10_1 = "abc";
bytes10 b10_2 = hex"123456";

// --- Invalid: literal to fixed-size bytes (smaller size) ---
bytes2 invalid_b2 = "abc"; //~ ERROR: mismatched types
bytes2 invalid_h2 = hex"123456"; //~ ERROR: mismatched types
bytes1 invalid_b1 = "ab"; //~ ERROR: mismatched types
}
}
20 changes: 20 additions & 0 deletions tests/ui/typeck/literal_bytes_implicit_conversion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: mismatched types
╭▸ ROOT/tests/ui/typeck/literal_bytes_implicit_conversion.sol:LL:CC
LL │ bytes2 invalid_b2 = "abc";
╰╴ ━━━━━ cannot convert `utf8_string_literal[3]` to `bytes2`

error: mismatched types
╭▸ ROOT/tests/ui/typeck/literal_bytes_implicit_conversion.sol:LL:CC
LL │ bytes2 invalid_h2 = hex"123456";
╰╴ ━━━━━━━━━━━ cannot convert `utf8_string_literal[3]` to `bytes2`

error: mismatched types
╭▸ ROOT/tests/ui/typeck/literal_bytes_implicit_conversion.sol:LL:CC
LL │ bytes1 invalid_b1 = "ab";
╰╴ ━━━━ cannot convert `utf8_string_literal[2]` to `bytes1`

error: aborting due to 3 previous errors

Loading