Skip to content

Commit ac5aa8c

Browse files
Don't suggest nonsense suggestions for unconstrained type vars in note_source_of_type_mismatch_constraint
1 parent 8e47113 commit ac5aa8c

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 _;
@@ -504,12 +504,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
504504
// incompatible fix at the original mismatch site.
505505
if matches!(source, TypeMismatchSource::Ty(_))
506506
&& let Some(ideal_method) = ideal_method
507+
&& let ideal_arg_ty = self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1])
508+
// HACK(compiler-errors): We don't actually consider the implications
509+
// of our inference guesses in `emit_type_mismatch_suggestions`, so
510+
// only suggest things when we know our type error is precisely due to
511+
// a type mismatch, and not via some projection or something. See #116155.
512+
&& !ideal_arg_ty.has_non_region_infer()
507513
{
508514
self.emit_type_mismatch_suggestions(
509515
err,
510516
arg_expr,
511517
arg_ty,
512-
self.resolve_vars_if_possible(ideal_method.sig.inputs()[idx + 1]),
518+
ideal_arg_ty,
513519
None,
514520
None,
515521
);
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)