Skip to content

Commit 7887941

Browse files
authored
Unrolled build for rust-lang#116158
Rollup merge of rust-lang#116158 - compiler-errors:unconstrained-type-var-sugg, r=wesleywiser Don't suggest nonsense suggestions for unconstrained type vars in `note_source_of_type_mismatch_constraint` The way we do type inference for suggestions in `note_source_of_type_mismatch_constraint` is a bit strange. We compute the "ideal" method signature, which takes the receiver that we *want* and uses it to compute the types of the arguments that would have given us that receiver via type inference, and use *that* to suggest how to change an argument to make sure our receiver type is inferred correctly. The problem is that sometimes we have totally unconstrained arguments (well, they're constrained by things outside of the type checker per se, like associated types), and therefore type suggestions are happy to coerce anything to that unconstrained argument. This leads to bogus suggestions, like rust-lang#116155. This is partly due to above, and partly due to the fact that `emit_type_mismatch_suggestions` doesn't double check that its suggestions are actually compatible with the program other than trying to satisfy the type mismatch. This adds a hack to make sure that at least the types are fully constrained, but I guess I could also rip out this logic altogether. There would be some sad diagnostics regressions though, such as `tests/ui/type/type-check/point-at-inference-4.rs`. Fixes rust-lang#116155
2 parents 4f75af9 + ac5aa8c commit 7887941

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)