Skip to content

Commit b8dd44d

Browse files
Don't suggest nonsense suggestions for unconstrained type vars in note_source_of_type_mismatch_constraint
1 parent 4e21162 commit b8dd44d

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::adjustment::AllowTwoPhase;
1414
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1515
use rustc_middle::ty::fold::BottomUpFolder;
1616
use rustc_middle::ty::print::with_no_trimmed_paths;
17-
use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut, TypeFoldable};
17+
use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut, TypeFoldable, TypeVisitableExt};
1818
use rustc_span::symbol::sym;
1919
use rustc_span::{BytePos, Span, DUMMY_SP};
2020
use rustc_trait_selection::infer::InferCtxtExt as _;
@@ -503,12 +503,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
503503
// incompatible fix at the original mismatch site.
504504
if matches!(source, TypeMismatchSource::Ty(_))
505505
&& let Some(ideal_method) = ideal_method
506+
&& let ideal_arg_ty = self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1])
507+
// HACK(compiler-errors): We don't actually consider the implications
508+
// of our inference guesses in `emit_type_mismatch_suggestions`, so
509+
// only suggest things when we know our type error is precisely due to
510+
// a type mismatch, and not via some projection or something. See #116155.
511+
&& !ideal_arg_ty.has_non_region_infer()
506512
{
507513
self.emit_type_mismatch_suggestions(
508514
err,
509515
arg_expr,
510516
arg_ty,
511-
self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1]),
517+
ideal_arg_ty,
512518
None,
513519
None,
514520
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct S<T>(T);
2+
3+
impl<T> S<T> {
4+
fn new() -> Self {
5+
loop {}
6+
}
7+
8+
fn constrain<F: Fn() -> T>(&self, _f: F) {}
9+
}
10+
11+
fn main() {
12+
let s = S::new();
13+
let c = || true;
14+
s.constrain(c);
15+
let _: S<usize> = s;
16+
//~^ ERROR mismatched types
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/point-at-inference-issue-116155.rs:15:23
3+
|
4+
LL | s.constrain(c);
5+
| - - this argument has type `[closure@$DIR/point-at-inference-issue-116155.rs:13:13: 13:15]`...
6+
| |
7+
| ... which causes `s` to have type `S<bool>`
8+
LL | let _: S<usize> = s;
9+
| -------- ^ expected `S<usize>`, found `S<bool>`
10+
| |
11+
| expected due to this
12+
|
13+
= note: expected struct `S<usize>`
14+
found struct `S<bool>`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)