From ac5aa8c1a4370b34674d18ef20b642645d2172e0 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 25 Sep 2023 18:57:12 +0000 Subject: [PATCH] Don't suggest nonsense suggestions for unconstrained type vars in note_source_of_type_mismatch_constraint --- compiler/rustc_hir_typeck/src/demand.rs | 10 ++++++++-- .../point-at-inference-issue-116155.rs | 17 +++++++++++++++++ .../point-at-inference-issue-116155.stderr | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 tests/ui/type/type-check/point-at-inference-issue-116155.rs create mode 100644 tests/ui/type/type-check/point-at-inference-issue-116155.stderr diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index d97691369c9..5c3f2b85966 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -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 _; @@ -504,12 +504,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, ); diff --git a/tests/ui/type/type-check/point-at-inference-issue-116155.rs b/tests/ui/type/type-check/point-at-inference-issue-116155.rs new file mode 100644 index 00000000000..1e9942d42e8 --- /dev/null +++ b/tests/ui/type/type-check/point-at-inference-issue-116155.rs @@ -0,0 +1,17 @@ +struct S(T); + +impl S { + fn new() -> Self { + loop {} + } + + fn constrain T>(&self, _f: F) {} +} + +fn main() { + let s = S::new(); + let c = || true; + s.constrain(c); + let _: S = s; + //~^ ERROR mismatched types +} diff --git a/tests/ui/type/type-check/point-at-inference-issue-116155.stderr b/tests/ui/type/type-check/point-at-inference-issue-116155.stderr new file mode 100644 index 00000000000..c8c01603cb8 --- /dev/null +++ b/tests/ui/type/type-check/point-at-inference-issue-116155.stderr @@ -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` +LL | let _: S = s; + | -------- ^ expected `S`, found `S` + | | + | expected due to this + | + = note: expected struct `S` + found struct `S` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.