Rollup merge of #107499 - compiler-errors:deduce_sig_from_projection-generator-tweak, r=michaelwoerister

Do not depend on Generator trait when deducing closure signature

1. Do not depend on `Generator` trait when deducing closure signature.
2. Compare the name of the `Generator::Return` associated item, rather than its order in the trait. Seems more stable this way.
This commit is contained in:
Matthias Krüger 2023-02-01 05:54:39 +01:00 committed by GitHub
commit a37a59f45e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 46 deletions

View File

@ -15,6 +15,7 @@
use rustc_middle::ty::{self, Ty, TypeSuperVisitable, TypeVisitor};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span;
use rustc_span::sym;
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::error_reporting::ArgKind;
@ -288,21 +289,20 @@ fn deduce_sig_from_projection(
let trait_def_id = projection.trait_def_id(tcx);
let is_fn = tcx.is_fn_trait(trait_def_id);
let gen_trait = tcx.require_lang_item(LangItem::Generator, cause_span);
let is_gen = gen_trait == trait_def_id;
let gen_trait = tcx.lang_items().gen_trait();
let is_gen = gen_trait == Some(trait_def_id);
if !is_fn && !is_gen {
debug!("not fn or generator");
return None;
}
if is_gen {
// Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
// associated item and not yield.
let return_assoc_item = self.tcx.associated_item_def_ids(gen_trait)[1];
if return_assoc_item != projection.projection_def_id() {
debug!("not return assoc item of generator");
return None;
}
// Check that we deduce the signature from the `<_ as std::ops::Generator>::Return`
// associated item and not yield.
if is_gen && self.tcx.associated_item(projection.projection_def_id()).name != sym::Return {
debug!("not `Return` assoc item of `Generator`");
return None;
}
let input_tys = if is_fn {

View File

@ -1,21 +0,0 @@
// error-pattern: requires `generator` lang_item
#![feature(no_core, lang_items, unboxed_closures, tuple_trait)]
#![no_core]
#[lang = "sized"] pub trait Sized { }
#[lang = "tuple_trait"] pub trait Tuple { }
#[lang = "fn_once"]
#[rustc_paren_sugar]
pub trait FnOnce<Args: Tuple> {
type Output;
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}
pub fn abc() -> impl FnOnce(f32) {
|_| {}
}
fn main() {}

View File

@ -1,15 +0,0 @@
error[E0635]: unknown feature `tuple_trait`
--> $DIR/lang-item-missing-generator.rs:2:51
|
LL | #![feature(no_core, lang_items, unboxed_closures, tuple_trait)]
| ^^^^^^^^^^^
error: requires `generator` lang_item
--> $DIR/lang-item-missing-generator.rs:17:17
|
LL | pub fn abc() -> impl FnOnce(f32) {
| ^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0635`.