Improve error message for type mismatch in generator arguments
This commit is contained in:
parent
c7dbe7a830
commit
cbd79836a5
4 changed files with 55 additions and 14 deletions
|
@ -730,7 +730,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let found_did = match *found_trait_ty.kind() {
|
let found_did = match *found_trait_ty.kind() {
|
||||||
ty::Closure(did, _) | ty::Foreign(did) | ty::FnDef(did, _) => Some(did),
|
ty::Closure(did, _)
|
||||||
|
| ty::Foreign(did)
|
||||||
|
| ty::FnDef(did, _)
|
||||||
|
| ty::Generator(did, ..) => Some(did),
|
||||||
ty::Adt(def, _) => Some(def.did),
|
ty::Adt(def, _) => Some(def.did),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1250,33 +1250,40 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||||
) -> String {
|
) -> String {
|
||||||
let inputs = trait_ref.skip_binder().substs.type_at(1);
|
let inputs = trait_ref.skip_binder().substs.type_at(1);
|
||||||
let sig = if let ty::Tuple(inputs) = inputs.kind() {
|
let sig = match inputs.kind() {
|
||||||
tcx.mk_fn_sig(
|
ty::Tuple(inputs)
|
||||||
inputs.iter().map(|k| k.expect_ty()),
|
if tcx.fn_trait_kind_from_lang_item(trait_ref.def_id()).is_some() =>
|
||||||
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0))),
|
{
|
||||||
false,
|
tcx.mk_fn_sig(
|
||||||
hir::Unsafety::Normal,
|
inputs.iter().map(|k| k.expect_ty()),
|
||||||
abi::Abi::Rust,
|
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0))),
|
||||||
)
|
false,
|
||||||
} else {
|
hir::Unsafety::Normal,
|
||||||
tcx.mk_fn_sig(
|
abi::Abi::Rust,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => tcx.mk_fn_sig(
|
||||||
std::iter::once(inputs),
|
std::iter::once(inputs),
|
||||||
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0))),
|
tcx.mk_ty_infer(ty::TyVar(ty::TyVid::from_u32(0))),
|
||||||
false,
|
false,
|
||||||
hir::Unsafety::Normal,
|
hir::Unsafety::Normal,
|
||||||
abi::Abi::Rust,
|
abi::Abi::Rust,
|
||||||
)
|
),
|
||||||
};
|
};
|
||||||
trait_ref.rebind(sig).to_string()
|
trait_ref.rebind(sig).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
let argument_is_closure = expected_ref.skip_binder().substs.type_at(0).is_closure();
|
let argument_kind = match expected_ref.skip_binder().substs.type_at(0) {
|
||||||
|
t if t.is_closure() => "closure",
|
||||||
|
t if t.is_generator() => "generator",
|
||||||
|
_ => "function",
|
||||||
|
};
|
||||||
let mut err = struct_span_err!(
|
let mut err = struct_span_err!(
|
||||||
self.tcx.sess,
|
self.tcx.sess,
|
||||||
span,
|
span,
|
||||||
E0631,
|
E0631,
|
||||||
"type mismatch in {} arguments",
|
"type mismatch in {} arguments",
|
||||||
if argument_is_closure { "closure" } else { "function" }
|
argument_kind
|
||||||
);
|
);
|
||||||
|
|
||||||
let found_str = format!("expected signature of `{}`", build_fn_sig_string(self.tcx, found));
|
let found_str = format!("expected signature of `{}`", build_fn_sig_string(self.tcx, found));
|
||||||
|
|
19
src/test/ui/generator/issue-88653.rs
Normal file
19
src/test/ui/generator/issue-88653.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// Regression test for #88653, where a confusing warning about a
|
||||||
|
// type mismatch in generator arguments was issued.
|
||||||
|
|
||||||
|
#![feature(generators, generator_trait)]
|
||||||
|
|
||||||
|
use std::ops::Generator;
|
||||||
|
|
||||||
|
fn foo(bar: bool) -> impl Generator<(bool,)> {
|
||||||
|
//~^ ERROR: type mismatch in generator arguments [E0631]
|
||||||
|
//~| NOTE: expected signature of `fn((bool,)) -> _`
|
||||||
|
|bar| {
|
||||||
|
//~^ NOTE: found signature of `fn(bool) -> _`
|
||||||
|
if bar {
|
||||||
|
yield bar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
12
src/test/ui/generator/issue-88653.stderr
Normal file
12
src/test/ui/generator/issue-88653.stderr
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
error[E0631]: type mismatch in generator arguments
|
||||||
|
--> $DIR/issue-88653.rs:8:22
|
||||||
|
|
|
||||||
|
LL | fn foo(bar: bool) -> impl Generator<(bool,)> {
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ expected signature of `fn((bool,)) -> _`
|
||||||
|
...
|
||||||
|
LL | |bar| {
|
||||||
|
| ----- found signature of `fn(bool) -> _`
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0631`.
|
Loading…
Add table
Add a link
Reference in a new issue