Rust for Linux List
 help / color / mirror / Atom feed
* From<bool> and Bounded's invariant for signed N = 1
@ 2026-08-13  9:46 Younes Akhouayri
  2026-08-13 10:16 ` Miguel Ojeda
  2026-08-13 13:04 ` Alexandre Courbot
  0 siblings, 2 replies; 4+ messages in thread
From: Younes Akhouayri @ 2026-08-13  9:46 UTC (permalink / raw)
  To: Rust For Linux; +Cc: Acourbot, Yury Norov, Ojeda, Yu Whisper Personal, Git

Hi,

There seems to be an invariant violation in the generic From<bool>
implementation for Bounded.

As I understand it, Bounded<i8, 1> can represent only -1 and 0, while
i8::from(true) produces 1. However, this safe code is accepted:

let value: Bounded<i8, 1> = true.into();
let _raw: i8 = *value;

Bounded::<i8, 1>::try_new(1) rejects the same value, but From<bool>
passes it directly to the unsafe __new() constructor. The resulting
Bounded value violates the invariant relied upon by Deref, whose
failure branch calls unreachable_unchecked().

Am I missing an intended constraint on this conversion? If not, it
seems the signed N = 1 case needs to be excluded or handled
differently.

Any thoughts?

Thanks,
Younes

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: From<bool> and Bounded's invariant for signed N = 1
  2026-08-13  9:46 From<bool> and Bounded's invariant for signed N = 1 Younes Akhouayri
@ 2026-08-13 10:16 ` Miguel Ojeda
  2026-08-13 13:04 ` Alexandre Courbot
  1 sibling, 0 replies; 4+ messages in thread
From: Miguel Ojeda @ 2026-08-13 10:16 UTC (permalink / raw)
  To: Younes Akhouayri
  Cc: Rust For Linux, Acourbot, Yury Norov, Ojeda, Yu Whisper Personal

On Thu, Aug 13, 2026 at 11:46 AM Younes Akhouayri <git@younes.io> wrote:
>
> Any thoughts?

Thanks for the report!

That is correct, good eyes -- indeed, the `Deref` bites us again like
in 223aa25aee82.

Do you want to write a patch?

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: From<bool> and Bounded's invariant for signed N = 1
  2026-08-13  9:46 From<bool> and Bounded's invariant for signed N = 1 Younes Akhouayri
  2026-08-13 10:16 ` Miguel Ojeda
@ 2026-08-13 13:04 ` Alexandre Courbot
  2026-08-13 19:52   ` Younes Akhouayri
  1 sibling, 1 reply; 4+ messages in thread
From: Alexandre Courbot @ 2026-08-13 13:04 UTC (permalink / raw)
  To: Younes Akhouayri; +Cc: Rust For Linux, Yury Norov, Ojeda, Yu Whisper Personal

On Thu Aug 13, 2026 at 6:46 PM JST, Younes Akhouayri wrote:
> Hi,
>
> There seems to be an invariant violation in the generic From<bool>
> implementation for Bounded.
>
> As I understand it, Bounded<i8, 1> can represent only -1 and 0, while
> i8::from(true) produces 1. However, this safe code is accepted:
>
> let value: Bounded<i8, 1> = true.into();
> let _raw: i8 = *value;
>
> Bounded::<i8, 1>::try_new(1) rejects the same value, but From<bool>
> passes it directly to the unsafe __new() constructor. The resulting
> Bounded value violates the invariant relied upon by Deref, whose
> failure branch calls unreachable_unchecked().
>
> Am I missing an intended constraint on this conversion? If not, it
> seems the signed N = 1 case needs to be excluded or handled
> differently.
>
> Any thoughts?

I think you are correct. There is a `Signedness` associated type on the
`Integer` trait, so it should be trivial to exclude signed integers - do
you want to send a patch for that?

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: From<bool> and Bounded's invariant for signed N = 1
  2026-08-13 13:04 ` Alexandre Courbot
@ 2026-08-13 19:52   ` Younes Akhouayri
  0 siblings, 0 replies; 4+ messages in thread
From: Younes Akhouayri @ 2026-08-13 19:52 UTC (permalink / raw)
  To: Alexandre Courbot; +Cc: Rust For Linux, Yury Norov, Ojeda, Yu Whisper Personal

Thank you both for the feedback. Yes, I can send a patch.

Just to confirm the intended scope: excluding signed integers entirely would also remove valid conversions such as Bounded::<i8, 2>::from(true), since signed Bounded values with N >= 2 can represent both 0 and 1.

Would you prefer the simpler unsigned-only restriction, or should the patch preserve signed conversions for N >= 2?


--

Best regards,
Younes



Aug 13, 2026, 15:04 by acourbot@nvidia.com:

> On Thu Aug 13, 2026 at 6:46 PM JST, Younes Akhouayri wrote:
>
>> Hi,
>>
>> There seems to be an invariant violation in the generic From<bool>
>> implementation for Bounded.
>>
>> As I understand it, Bounded<i8, 1> can represent only -1 and 0, while
>> i8::from(true) produces 1. However, this safe code is accepted:
>>
>> let value: Bounded<i8, 1> = true.into();
>> let _raw: i8 = *value;
>>
>> Bounded::<i8, 1>::try_new(1) rejects the same value, but From<bool>
>> passes it directly to the unsafe __new() constructor. The resulting
>> Bounded value violates the invariant relied upon by Deref, whose
>> failure branch calls unreachable_unchecked().
>>
>> Am I missing an intended constraint on this conversion? If not, it
>> seems the signed N = 1 case needs to be excluded or handled
>> differently.
>>
>> Any thoughts?
>>
>
> I think you are correct. There is a `Signedness` associated type on the
> `Integer` trait, so it should be trivial to exclude signed integers - do
> you want to send a patch for that?
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-13 19:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-13  9:46 From<bool> and Bounded's invariant for signed N = 1 Younes Akhouayri
2026-08-13 10:16 ` Miguel Ojeda
2026-08-13 13:04 ` Alexandre Courbot
2026-08-13 19:52   ` Younes Akhouayri

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox