# Pattern binding modes +!

**URL:** <https://internals.rust-lang.org/t/pattern-binding-modes/16829>\
**Category:** Uncategorized\
**Created:** [June 16, 2022, 1:31pm UTC](https://internals.rust-lang.org/t/pattern-binding-modes/16829 "2022-06-16T13:31:24Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![afetisov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/afetisov/32/8508_2.png) [@afetisov](https://internals.rust-lang.org/u/afetisov)\
**Post date:** [June 18, 2022, 1:22pm UTC](https://internals.rust-lang.org/t/pattern-binding-modes/16829/21 "2022-06-18T13:22:51Z")

</div>

> [@CAD97](#):
>
> `&!` could still potentially have not (unsafe but) valid values still, if we make it part of the validity invariant that the pointee type is inhabited.

AFAIK shared references are marked as "dereferenceable" in LLVM IR, and that attribute implies that the compiler is free to load the value at any time. [Quote](https://llvm.org/docs/LangRef.html#parameter-attributes):

> A pointer that is dereferenceable can be loaded from speculatively without a risk of trapping.

Now, the issue is complicated by the fact that `!` is supposed to be a zero-sized type, which means that nothing can actually be loaded (i.e. `&!` should be `dereferenceable(0)`), but this still makes me nervous to declare that `&!` is inhabited. That sounds logically inconsistent, which makes it quite likely that either there exists some weird way to cause UB, or that there will be a bug in the compiler based on the inconsistent assumptions.

---

<div class="post-metadata">

**Author:** ![felix.s](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/felix.s/32/8073_2.png) [@felix.s](https://internals.rust-lang.org/u/felix.s)\
**Post date:** [June 18, 2022, 1:24pm UTC](https://internals.rust-lang.org/t/pattern-binding-modes/16829/22 "2022-06-18T13:24:05Z")

</div>

> [@CAD97](#):
>
> making validity recursive is really hard to check for basically no optimization benefit

I think some forms of speculative execution, like loop-invariant code motion may benefit from it. For a contrived example:

```rust
fn foo0(n: usize, x: &bool) {
	for _ in 0..n {
		println!("{}", match *x {
			true => "true",
			false => "false",
		});
	}
}

fn foo1(n: usize, x: &bool) {
	let s = match *x {
		true => "true",
		false => "false",
	};

	for _ in 0..n {
		println!("{}", s);
	}
}

```

If a reference pointing to an invalid value is itself still valid, then it follows that `foo0(0, unsafe { mem::transmute<&bool>(&3) })` is not UB, but `foo1(0, unsafe { mem::transmute<&bool>(&3) })` is, so `foo0` cannot be rewritten into `foo1`.

I’m not necessarily insisting on having the validity of the referent matter, but it’s not like ignoring it is giving up _nothing_.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 16, 2022, 1:25pm UTC](https://internals.rust-lang.org/t/pattern-binding-modes/16829/23 "2022-09-16T13:25:01Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/pattern-binding-modes/16829.md?page=1)
