Skip to content

Resolved issue with mismatched types triggering ICE in certain scenarios #141236

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

Merged
merged 1 commit into from
May 20, 2025
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
14 changes: 8 additions & 6 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

self.annotate_expected_due_to_let_ty(err, expr, error);
self.annotate_loop_expected_due_to_inference(err, expr, error);
if self.annotate_mut_binding_to_immutable_binding(err, expr, error) {
if self.annotate_mut_binding_to_immutable_binding(err, expr, expr_ty, expected, error) {
return;
}

Expand Down Expand Up @@ -799,17 +799,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// Detect the following case
///
/// ```text
/// fn change_object(mut a: &Ty) {
/// fn change_object(mut b: &Ty) {
/// let a = Ty::new();
/// b = a;
/// }
/// ```
///
/// where the user likely meant to modify the value behind there reference, use `a` as an out
/// where the user likely meant to modify the value behind there reference, use `b` as an out
/// parameter, instead of mutating the local binding. When encountering this we suggest:
///
/// ```text
/// fn change_object(a: &'_ mut Ty) {
/// fn change_object(b: &'_ mut Ty) {
/// let a = Ty::new();
/// *b = a;
/// }
Expand All @@ -818,13 +818,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
err: &mut Diag<'_>,
expr: &hir::Expr<'_>,
expr_ty: Ty<'tcx>,
expected: Ty<'tcx>,
error: Option<TypeError<'tcx>>,
) -> bool {
if let Some(TypeError::Sorts(ExpectedFound { expected, found })) = error
if let Some(TypeError::Sorts(ExpectedFound { .. })) = error
&& let ty::Ref(_, inner, hir::Mutability::Not) = expected.kind()

// The difference between the expected and found values is one level of borrowing.
&& self.can_eq(self.param_env, *inner, found)
&& self.can_eq(self.param_env, *inner, expr_ty)

// We have an `ident = expr;` assignment.
&& let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Assign(lhs, rhs, _), .. }) =
Expand Down
9 changes: 0 additions & 9 deletions tests/crashes/140823.rs

This file was deleted.

26 changes: 26 additions & 0 deletions tests/ui/fn/coerce-suggestion-infer-region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! Functions with a mismatch between the expected and found type where the difference is a
//! reference may trigger analysis for additional help. In this test the expected type will be
//! &'a Container<&'a u8> and the found type will be Container<&'?0 u8>.
//!
//! This test exercises a scenario where the found type being analyzed contains an inference region
//! variable ('?0). This cannot be used in comparisons because the variable no longer exists by the
//! time the later analysis is performed.
//!
//! This is a regression test of #140823

trait MyFn<P> {}

struct Container<T> {
data: T,
}

struct Desugared {
callback: Box<dyn for<'a> MyFn<&'a Container<&'a u8>>>,
}

fn test(callback: Box<dyn for<'a> MyFn<Container<&'a u8>>>) -> Desugared {
Desugared { callback }
//~^ ERROR mismatched types
}

fn main() {}
12 changes: 12 additions & 0 deletions tests/ui/fn/coerce-suggestion-infer-region.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0308]: mismatched types
--> $DIR/coerce-suggestion-infer-region.rs:22:17
|
LL | Desugared { callback }
| ^^^^^^^^ expected `Box<dyn MyFn<&Container<&u8>>>`, found `Box<dyn MyFn<Container<&u8>>>`
|
= note: expected struct `Box<(dyn for<'a> MyFn<&'a Container<&'a u8>> + 'static)>`
found struct `Box<(dyn for<'a> MyFn<Container<&'a u8>> + 'static)>`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Loading