Skip to content

Commit 9b77a1e

Browse files
committed
Don't suggest types whose inner type is erroneous
Currently, we check if the returned type equals to `tcx.ty_error()` not to emit erroneous types, but this has a pitfall; for example, `Option<[type error]> != tcx.ty_error()` holds.
1 parent 887999d commit 9b77a1e

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_middle::ty::subst::InternalSubsts;
4141
use rustc_middle::ty::util::Discr;
4242
use rustc_middle::ty::util::IntTypeExt;
4343
use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, Ty, TyCtxt};
44-
use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
44+
use rustc_middle::ty::{ReprOptions, ToPredicate, TypeFoldable, WithConstness};
4545
use rustc_session::lint;
4646
use rustc_session::parse::feature_err;
4747
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -1779,7 +1779,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17791779
visitor.visit_ty(ty);
17801780
let mut diag = bad_placeholder_type(tcx, visitor.0, "return type");
17811781
let ret_ty = fn_sig.skip_binder().output();
1782-
if ret_ty != tcx.ty_error() {
1782+
if !ret_ty.references_error() {
17831783
if !ret_ty.is_closure() {
17841784
let ret_ty_str = match ret_ty.kind() {
17851785
// Suggest a function pointer return type instead of a unique function definition
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Regression test for #91450.
2+
// This test ensures that the compiler does not suggest `Foo<[type error]>` in diagnostic messages.
3+
4+
fn foo() -> Option<_> {} //~ ERROR: [E0308]
5+
//~^ ERROR: the type placeholder `_` is not allowed
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-91450-inner-ty-error.rs:4:13
3+
|
4+
LL | fn foo() -> Option<_> {}
5+
| --- ^^^^^^^^^ expected enum `Option`, found `()`
6+
| |
7+
| implicitly returns `()` as its body has no tail or `return` expression
8+
|
9+
= note: expected enum `Option<_>`
10+
found unit type `()`
11+
12+
error[E0121]: the type placeholder `_` is not allowed within types on item signatures for return types
13+
--> $DIR/issue-91450-inner-ty-error.rs:4:20
14+
|
15+
LL | fn foo() -> Option<_> {}
16+
| ^ not allowed in type signatures
17+
18+
error: aborting due to 2 previous errors
19+
20+
Some errors have detailed explanations: E0121, E0308.
21+
For more information about an error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)