You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #129313 - RalfJung:coroutine-niches, r=compiler-errors
Supress niches in coroutines to avoid aliasing violations
As mentioned [here](#63818 (comment)), using niches in fields of coroutines that are referenced by other fields is unsound: the discriminant accesses violate the aliasing requirements of the reference pointing to the relevant field. This issue causes [Miri errors in practice](rust-lang/miri#3780).
The "obvious" fix for this is to suppress niches in coroutines. That's what this PR does. However, we have several tests explicitly ensuring that we *do* use niches in coroutines. So I see two options:
- We guard this behavior behind a `-Z` flag (that Miri will set by default). There is no known case of these aliasing violations causing miscompilations. But absence of evidence is not evidence of absence...
- (What this PR does right now.) We temporarily adjust the coroutine layout logic and the associated tests until the proper fix lands. The "proper fix" here is to wrap fields that other fields can point to in [`UnsafePinned`](#125735) and make `UnsafePinned` suppress niches; that would then still permit using niches of *other* fields (those that never get borrowed). However, I know that coroutine sizes are already a problem, so I am not sure if this temporary size regression is acceptable.
`@compiler-errors` any opinion? Also who else should be Cc'd here?
Copy file name to clipboardExpand all lines: compiler/rustc_ty_utils/src/layout.rs
+7-1
Original file line number
Diff line number
Diff line change
@@ -1001,7 +1001,13 @@ fn coroutine_layout<'tcx>(
1001
1001
},
1002
1002
fields: outer_fields,
1003
1003
abi,
1004
-
largest_niche: prefix.largest_niche,
1004
+
// Suppress niches inside coroutines. If the niche is inside a field that is aliased (due to
1005
+
// self-referentiality), getting the discriminant can cause aliasing violations.
1006
+
// `UnsafeCell` blocks niches for the same reason, but we don't yet have `UnsafePinned` that
1007
+
// would do the same for us here.
1008
+
// See <https://github.com/rust-lang/rust/issues/63818>, <https://github.com/rust-lang/miri/issues/3780>.
1009
+
// FIXME: Remove when <https://github.com/rust-lang/rust/issues/125735> is implemented and aliased coroutine fields are wrapped in `UnsafePinned`.
0 commit comments