Skip to content

Commit 906b6e8

Browse files
committed
Suppress suggest try wrap when found is unresolved var
Signed-off-by: xizheyin <[email protected]>
1 parent df5a341 commit 906b6e8

File tree

7 files changed

+16
-25
lines changed

7 files changed

+16
-25
lines changed

compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use rustc_hir::{MatchSource, Node};
1111
use rustc_middle::traits::{MatchExpressionArmCause, ObligationCause, ObligationCauseCode};
1212
use rustc_middle::ty::error::TypeError;
1313
use rustc_middle::ty::print::with_no_trimmed_paths;
14-
use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt};
14+
use rustc_middle::ty::{
15+
self as ty, GenericArgKind, IsSuggestable, Ty, TypeFlags, TypeVisitableExt,
16+
};
1517
use rustc_span::{Span, sym};
1618
use tracing::debug;
1719

@@ -89,6 +91,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
8991
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
9092
diag: &mut Diag<'_>,
9193
) {
94+
// when found is unresolved var, we can't suggest anything
95+
if exp_found.found.has_type_flags(TypeFlags::HAS_INFER_TY_VAR) {
96+
return;
97+
}
98+
9299
// Heavily inspired by `FnCtxt::suggest_compatible_variants`, with
93100
// some modifications due to that being in typeck and this being in infer.
94101
if let ObligationCauseCode::Pattern { .. } = cause.code()

compiler/rustc_type_ir/src/flags.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ bitflags::bitflags! {
133133

134134
/// Does this type have any coroutines in it?
135135
const HAS_TY_CORO = 1 << 24;
136+
137+
/// Does this type have any Infer(TyVar(_)) in it?
138+
const HAS_INFER_TY_VAR = 1 << 25;
136139
}
137140
}
138141

@@ -267,9 +270,10 @@ impl<I: Interner> FlagComputation<I> {
267270
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
268271
self.add_flags(TypeFlags::HAS_TY_FRESH)
269272
}
270-
271-
ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_) => {
272-
self.add_flags(TypeFlags::HAS_TY_INFER)
273+
ty::IntVar(_) | ty::FloatVar(_) => self.add_flags(TypeFlags::HAS_TY_INFER),
274+
ty::TyVar(_) => {
275+
self.add_flags(TypeFlags::HAS_INFER_TY_VAR);
276+
self.add_flags(TypeFlags::HAS_TY_INFER);
273277
}
274278
},
275279

tests/ui/issues/issue-3680.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ LL | Err(_) => ()
88
|
99
= note: expected enum `Option<_>`
1010
found enum `Result<_, _>`
11-
help: try wrapping the pattern in `Some`
12-
|
13-
LL | Some(Err(_)) => ()
14-
| +++++ +
1511

1612
error: aborting due to 1 previous error
1713

tests/ui/issues/issue-5358-1.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ LL | Either::Right(_) => {}
88
|
99
= note: expected struct `S`
1010
found enum `Either<_, _>`
11-
help: try wrapping the pattern in `S`
12-
|
13-
LL | S(Either::Right(_)) => {}
14-
| ++ +
1511
help: you might have meant to use field `0` whose type is `Either<usize, usize>`
1612
|
1713
LL | match S(Either::Left(5)).0 {

tests/ui/match/issue-12552.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ LL | Some(k) => match k {
88
|
99
= note: expected enum `Result<_, {integer}>`
1010
found enum `Option<_>`
11-
help: try wrapping the pattern in `Ok`
12-
|
13-
LL | Ok(Some(k)) => match k {
14-
| +++ +
1511

1612
error[E0308]: mismatched types
1713
--> $DIR/issue-12552.rs:9:5
@@ -24,10 +20,6 @@ LL | None => ()
2420
|
2521
= note: expected enum `Result<_, {integer}>`
2622
found enum `Option<_>`
27-
help: try wrapping the pattern in `Ok`
28-
|
29-
LL | Ok(None) => ()
30-
| +++ +
3123

3224
error: aborting due to 2 previous errors
3325

tests/ui/type/type-mismatch-suggest-wrap-issue-145634.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// We should not suggest wrapping rhs with `Some`
22
// when the found type is an unresolved type variable
3-
//
3+
//
44
// See https://github.com/rust-lang/rust/issues/145634
55

66
fn main() {

tests/ui/type/type-mismatch-suggest-wrap-issue-145634.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ LL | assert!(matches!(foo, &Some((1, 2))));
88
|
99
= note: expected enum `Option<&({integer}, {integer})>`
1010
found reference `&_`
11-
help: try wrapping the pattern in `Some`
12-
|
13-
LL | assert!(matches!(foo, Some(&Some((1, 2)))));
14-
| +++++ +
1511

1612
error: aborting due to 1 previous error
1713

0 commit comments

Comments
 (0)