Skip to content

Commit bbea63f

Browse files
Rollup merge of rust-lang#111354 - jonas-schievink:enable-future-combinators, r=compiler-errors
Fix miscompilation when calling default methods on `Future` In rust-lang#111264 I discovered a lingering miscompilation when calling a default method on `Future` (none currently exist). rust-lang#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 rust-lang#111347, but no test is included here (assuming that future methods include their own tests that would cover this sufficiently). r? `@compiler-errors`
2 parents 24ba82d + 3bbb69e commit bbea63f

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

compiler/rustc_ty_utils/src/instance.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,18 @@ fn resolve_associated_item<'tcx>(
194194
})
195195
}
196196
traits::ImplSource::Future(future_data) => {
197-
if cfg!(debug_assertions) && tcx.item_name(trait_item_id) != sym::poll {
198-
// For compiler developers who'd like to add new items to `Future`,
199-
// you either need to generate a shim body, or perhaps return
200-
// `InstanceDef::Item` pointing to a trait default method body if
201-
// it is given a default implementation by the trait.
202-
span_bug!(
203-
tcx.def_span(future_data.generator_def_id),
204-
"no definition for `{trait_ref}::{}` for built-in async generator type",
205-
tcx.item_name(trait_item_id)
206-
)
197+
if Some(trait_item_id) == tcx.lang_items().future_poll_fn() {
198+
// `Future::poll` is generated by the compiler.
199+
Some(Instance {
200+
def: ty::InstanceDef::Item(future_data.generator_def_id),
201+
substs: future_data.substs,
202+
})
203+
} else {
204+
// All other methods are default methods of the `Future` trait.
205+
// (this assumes that `ImplSource::Future` is only used for methods on `Future`)
206+
debug_assert!(tcx.impl_defaultness(trait_item_id).has_value());
207+
Some(Instance::new(trait_item_id, rcvr_substs))
207208
}
208-
Some(Instance {
209-
def: ty::InstanceDef::Item(future_data.generator_def_id),
210-
substs: future_data.substs,
211-
})
212209
}
213210
traits::ImplSource::Closure(closure_data) => {
214211
if cfg!(debug_assertions)

0 commit comments

Comments
 (0)