Skip to content

Commit

Permalink
Don't suggest nonsense suggestions for unconstrained type vars in not…
Browse files Browse the repository at this point in the history
…e_source_of_type_mismatch_constraint
  • Loading branch information
compiler-errors committed Sep 25, 2023
1 parent 4e21162 commit b8dd44d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_middle::ty::adjustment::AllowTwoPhase;
use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::fold::BottomUpFolder;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut, TypeFoldable};
use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut, TypeFoldable, TypeVisitableExt};
use rustc_span::symbol::sym;
use rustc_span::{BytePos, Span, DUMMY_SP};
use rustc_trait_selection::infer::InferCtxtExt as _;
Expand Down Expand Up @@ -503,12 +503,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// incompatible fix at the original mismatch site.
if matches!(source, TypeMismatchSource::Ty(_))
&& let Some(ideal_method) = ideal_method
&& let ideal_arg_ty = self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1])
// HACK(compiler-errors): We don't actually consider the implications
// of our inference guesses in `emit_type_mismatch_suggestions`, so
// only suggest things when we know our type error is precisely due to
// a type mismatch, and not via some projection or something. See #116155.
&& !ideal_arg_ty.has_non_region_infer()
{
self.emit_type_mismatch_suggestions(
err,
arg_expr,
arg_ty,
self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1]),
ideal_arg_ty,
None,
None,
);
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/type/type-check/point-at-inference-issue-116155.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct S<T>(T);

impl<T> S<T> {
fn new() -> Self {
loop {}
}

fn constrain<F: Fn() -> T>(&self, _f: F) {}
}

fn main() {
let s = S::new();
let c = || true;
s.constrain(c);
let _: S<usize> = s;
//~^ ERROR mismatched types
}
18 changes: 18 additions & 0 deletions tests/ui/type/type-check/point-at-inference-issue-116155.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/point-at-inference-issue-116155.rs:15:23
|
LL | s.constrain(c);
| - - this argument has type `[closure@$DIR/point-at-inference-issue-116155.rs:13:13: 13:15]`...
| |
| ... which causes `s` to have type `S<bool>`
LL | let _: S<usize> = s;
| -------- ^ expected `S<usize>`, found `S<bool>`
| |
| expected due to this
|
= note: expected struct `S<usize>`
found struct `S<bool>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit b8dd44d

Please sign in to comment.