mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
Rollup merge of #111354 - jonas-schievink:enable-future-combinators, r=compiler-errors
Fix miscompilation when calling default methods on `Future` In https://github.com/rust-lang/rust/issues/111264 I discovered a lingering miscompilation when calling a default method on `Future` (none currently exist). https://github.com/rust-lang/rust/pull/111279 added a debug assertion, which sadly doesn't help much since to my knowledge stage0 is not built with them enabled, and it still doesn't make default methods work like they should. This PR fixes `resolve_instance` to resolve default methods on `Future` correctly, allowing library contributors to add `Future` combinators without running into ICEs or miscompilations. I've tested this as part of https://github.com/rust-lang/rust/pull/111347, but no test is included here (assuming that future methods include their own tests that would cover this sufficiently). r? `@compiler-errors`
This commit is contained in:
commit
bbea63f627
@ -194,21 +194,18 @@ fn resolve_associated_item<'tcx>(
|
||||
})
|
||||
}
|
||||
traits::ImplSource::Future(future_data) => {
|
||||
if cfg!(debug_assertions) && tcx.item_name(trait_item_id) != sym::poll {
|
||||
// For compiler developers who'd like to add new items to `Future`,
|
||||
// you either need to generate a shim body, or perhaps return
|
||||
// `InstanceDef::Item` pointing to a trait default method body if
|
||||
// it is given a default implementation by the trait.
|
||||
span_bug!(
|
||||
tcx.def_span(future_data.generator_def_id),
|
||||
"no definition for `{trait_ref}::{}` for built-in async generator type",
|
||||
tcx.item_name(trait_item_id)
|
||||
)
|
||||
if Some(trait_item_id) == tcx.lang_items().future_poll_fn() {
|
||||
// `Future::poll` is generated by the compiler.
|
||||
Some(Instance {
|
||||
def: ty::InstanceDef::Item(future_data.generator_def_id),
|
||||
substs: future_data.substs,
|
||||
})
|
||||
} else {
|
||||
// All other methods are default methods of the `Future` trait.
|
||||
// (this assumes that `ImplSource::Future` is only used for methods on `Future`)
|
||||
debug_assert!(tcx.impl_defaultness(trait_item_id).has_value());
|
||||
Some(Instance::new(trait_item_id, rcvr_substs))
|
||||
}
|
||||
Some(Instance {
|
||||
def: ty::InstanceDef::Item(future_data.generator_def_id),
|
||||
substs: future_data.substs,
|
||||
})
|
||||
}
|
||||
traits::ImplSource::Closure(closure_data) => {
|
||||
if cfg!(debug_assertions)
|
||||
|
Loading…
Reference in New Issue
Block a user