Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't suggest nonsense suggestions for unconstrained type vars in note_source_of_type_mismatch_constraint #116158

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
);
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`.
Loading