Skip to content

Commit 609d9a0

Browse files
authored
Rollup merge of #91450 - hkmatsumoto:hide-type-error, r=estebank
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. Fixes #91371.
2 parents a8f8f74 + 9b77a1e commit 609d9a0

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Diff for: 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};
@@ -1777,7 +1777,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
17771777
visitor.visit_ty(ty);
17781778
let mut diag = bad_placeholder_type(tcx, visitor.0, "return type");
17791779
let ret_ty = fn_sig.skip_binder().output();
1780-
if ret_ty != tcx.ty_error() {
1780+
if !ret_ty.references_error() {
17811781
if !ret_ty.is_closure() {
17821782
let ret_ty_str = match ret_ty.kind() {
17831783
// Suggest a function pointer return type instead of a unique function definition

Diff for: src/test/ui/typeck/issue-91450-inner-ty-error.rs

+7
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() {}

Diff for: src/test/ui/typeck/issue-91450-inner-ty-error.stderr

+21
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)