Skip to content
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
3 changes: 2 additions & 1 deletion compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir::{ExprKind, ItemKind, Node};
use rustc_infer::infer;
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Ty};
use rustc_middle::ty::{self, Binder, Ty};
use rustc_span::symbol::kw;

use std::iter;
Expand Down Expand Up @@ -487,6 +487,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let found = self.resolve_vars_with_obligations(found);
if let hir::FnRetTy::Return(ty) = fn_decl.output {
let ty = AstConv::ast_ty_to_ty(self, ty);
let ty = self.tcx.erase_late_bound_regions(Binder::bind(ty));
let ty = self.normalize_associated_types_in(expr.span, ty);
if self.can_coerce(found, ty) {
err.multipart_suggestion(
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/return/issue-82612-return-mutable-reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Regression test for #82612.

use std::marker::PhantomData;

pub trait SparseSetIndex {
fn sparse_set_index(&self) -> usize;
}
pub struct SparseArray<I, V = I> {
values: Vec<Option<V>>,
marker: PhantomData<I>,
}

impl<I: SparseSetIndex, V> SparseArray<I, V> {
pub fn get_or_insert_with(&mut self, index: I, func: impl FnOnce() -> V) -> &mut V {
let index = index.sparse_set_index();
if index < self.values.len() {
let value = unsafe { self.values.get_unchecked_mut(index) };
value.get_or_insert_with(func) //~ ERROR mismatched types
}
unsafe { self.values.get_unchecked_mut(index).as_mut().unwrap() }
}
}

fn main() {}
28 changes: 28 additions & 0 deletions src/test/ui/return/issue-82612-return-mutable-reference.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0308]: mismatched types
--> $DIR/issue-82612-return-mutable-reference.rs:18:13
|
LL | / if index < self.values.len() {
LL | | let value = unsafe { self.values.get_unchecked_mut(index) };
LL | | value.get_or_insert_with(func)
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `&mut V`
LL | | }
| |_________- expected this to be `()`
|
= note: expected unit type `()`
found mutable reference `&mut V`
help: consider using a semicolon here
|
LL | value.get_or_insert_with(func);
| ^
help: consider using a semicolon here
|
LL | };
| ^
help: you might have meant to return this value
|
LL | return value.get_or_insert_with(func);
| ^^^^^^ ^

error: aborting due to previous error

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