Add an explicit_type TLV syntax for avoiding certain cases of type inference

This new syntax is used to fix "dependency on fallback of ! -> ()".
This avoids cases where code compiles with a fallback of the
never type leading to the unit type. The behaviour in Rust edition 2024
would make this a compile error.

See: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/builtin/static.DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK.html#
This commit is contained in:
Duncan Dean 2024-09-06 12:26:19 +02:00
parent c7627dfd61
commit c0d84e85c7
No known key found for this signature in database
3 changed files with 28 additions and 3 deletions

View file

@ -335,8 +335,7 @@ jobs:
-A clippy::unnecessary_to_owned \ -A clippy::unnecessary_to_owned \
-A clippy::unnecessary_unwrap \ -A clippy::unnecessary_unwrap \
-A clippy::unused_unit \ -A clippy::unused_unit \
-A clippy::useless_conversion \ -A clippy::useless_conversion
-A dependency_on_unit_never_type_fallback
rustfmt: rustfmt:
runs-on: ubuntu-latest runs-on: ubuntu-latest

View file

@ -1412,7 +1412,7 @@ impl Readable for CommitmentTransaction {
(8, keys, required), (8, keys, required),
(10, built, required), (10, built, required),
(12, htlcs, required_vec), (12, htlcs, required_vec),
(14, _legacy_deserialization_prevention_marker, option), (14, _legacy_deserialization_prevention_marker, (option, explicit_type: ())),
(15, channel_type_features, option), (15, channel_type_features, option),
}); });

View file

@ -281,6 +281,12 @@ macro_rules! _check_decoded_tlv_order {
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
// no-op // no-op
}}; }};
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option, explicit_type: $fieldty: ty)) => {{
// no-op
}};
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (required, explicit_type: $fieldty: ty)) => {{
_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
}};
($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => {{ ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => {{
$crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required); $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
}}; }};
@ -332,6 +338,12 @@ macro_rules! _check_missing_tlv {
($last_seen_type: expr, $type: expr, $field: ident, option) => {{ ($last_seen_type: expr, $type: expr, $field: ident, option) => {{
// no-op // no-op
}}; }};
($last_seen_type: expr, $type: expr, $field: ident, (option, explicit_type: $fieldty: ty)) => {{
// no-op
}};
($last_seen_type: expr, $type: expr, $field: ident, (required, explicit_type: $fieldty: ty)) => {{
_check_missing_tlv!($last_seen_type, $type, $field, required);
}};
($last_seen_type: expr, $type: expr, $field: ident, optional_vec) => {{ ($last_seen_type: expr, $type: expr, $field: ident, optional_vec) => {{
// no-op // no-op
}}; }};
@ -372,6 +384,14 @@ macro_rules! _decode_tlv {
($outer_reader: expr, $reader: expr, $field: ident, option) => {{ ($outer_reader: expr, $reader: expr, $field: ident, option) => {{
$field = Some($crate::util::ser::Readable::read(&mut $reader)?); $field = Some($crate::util::ser::Readable::read(&mut $reader)?);
}}; }};
($outer_reader: expr, $reader: expr, $field: ident, (option, explicit_type: $fieldty: ty)) => {{
let _field: &Option<$fieldty> = &$field;
_decode_tlv!($outer_reader, $reader, $field, option);
}};
($outer_reader: expr, $reader: expr, $field: ident, (required, explicit_type: $fieldty: ty)) => {{
let _field: &$fieldty = &$field;
_decode_tlv!($outer_reader, $reader, $field, required);
}};
($outer_reader: expr, $reader: expr, $field: ident, optional_vec) => {{ ($outer_reader: expr, $reader: expr, $field: ident, optional_vec) => {{
let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?; let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
$field = Some(f.0); $field = Some(f.0);
@ -795,6 +815,12 @@ macro_rules! _init_tlv_field_var {
($field: ident, optional_vec) => { ($field: ident, optional_vec) => {
let mut $field = Some(Vec::new()); let mut $field = Some(Vec::new());
}; };
($field: ident, (option, explicit_type: $fieldty: ty)) => {
let mut $field: Option<$fieldty> = None;
};
($field: ident, (required, explicit_type: $fieldty: ty)) => {
let mut $field = $crate::util::ser::RequiredWrapper::<$fieldty>(None);
};
($field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => { ($field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
$crate::_init_tlv_field_var!($field, option); $crate::_init_tlv_field_var!($field, option);
}; };