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
41 changes: 40 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir::attrs::DivergingBlockBehavior;
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor;
use rustc_hir::{Expr, ExprKind, HirId, LangItem, Node, QPath, is_range_literal};
use rustc_hir::{Expr, ExprKind, FnRetTy, HirId, LangItem, Node, QPath, is_range_literal};
use rustc_hir_analysis::check::potentially_plural_count;
use rustc_hir_analysis::hir_ty_lowering::{HirTyLowerer, PermitVariants};
use rustc_index::IndexVec;
Expand Down Expand Up @@ -1587,6 +1587,45 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
err.span_note(spans, format!("{} defined here", self.tcx.def_descr(def_id)));
if let DefKind::Fn | DefKind::AssocFn = self.tcx.def_kind(def_id)
&& let ty::Param(_) =
self.tcx.fn_sig(def_id).instantiate_identity().skip_binder().output().kind()
&& let parent = self.tcx.hir_get_parent_item(call_expr.hir_id).def_id
&& let Some((output, body_id)) = match self.tcx.hir_node_by_def_id(parent) {
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn { sig, body, .. },
..
})
| hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body)),
..
})
| hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Fn(sig, body),
..
}) => Some((sig.decl.output, body)),
_ => None,
}
&& let expr = self.tcx.hir_body(*body_id).value
&& (expr.peel_blocks().span == call_expr.span
|| matches!(
self.tcx.parent_hir_node(call_expr.hir_id),
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Ret(_), .. })
))
{
err.span_label(
output.span(),
match output {
FnRetTy::DefaultReturn(_) => format!(
"this implicit `()` return type influences the call expression's return type"
),
FnRetTy::Return(_) => {
"this return type influences the call expression's return type"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this arm reachable? It doesn't occur in the tests, AFAICT.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.to_string()
}
},
);
}
} else if let Some(hir::Node::Expr(e)) = self.tcx.hir_get_if_local(def_id)
&& let hir::ExprKind::Closure(hir::Closure { body, .. }) = &e.kind
{
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/mismatched_types/expectation-from-return-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::ptr;

fn main() { //~ NOTE: this implicit `()` return type influences the call expression's return type
let a = 0;
ptr::read(&a)
//~^ ERROR: mismatched types
//~| NOTE: expected `*const ()`, found `&{integer}`
//~| NOTE: arguments to this function are incorrect
//~| NOTE: expected raw pointer
//~| NOTE: function defined here
}

fn foo() { //~ NOTE: this implicit `()` return type influences the call expression's return type
let a = 0;
return ptr::read(&a);
//~^ ERROR: mismatched types
//~| NOTE: expected `*const ()`, found `&{integer}`
//~| NOTE: arguments to this function are incorrect
//~| NOTE: expected raw pointer
//~| NOTE: function defined here
}
35 changes: 35 additions & 0 deletions tests/ui/mismatched_types/expectation-from-return-type.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0308]: mismatched types
--> $DIR/expectation-from-return-type.rs:5:15
|
LL | fn main() {
| - this implicit `()` return type influences the call expression's return type
LL | let a = 0;
LL | ptr::read(&a)
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

error[E0308]: mismatched types
--> $DIR/expectation-from-return-type.rs:15:22
|
LL | fn foo() {
| - this implicit `()` return type influences the call expression's return type
LL | let a = 0;
LL | return ptr::read(&a);
| --------- ^^ expected `*const ()`, found `&{integer}`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const ()`
found reference `&{integer}`
note: function defined here
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ LL | arg.map(|v| &**v)
error[E0308]: mismatched types
--> $DIR/transforming-option-ref-issue-127545.rs:9:19
|
LL | pub fn bar(arg: Option<&Vec<i32>>) -> &[i32] {
| ------ this return type influences the call expression's return type
LL | arg.unwrap_or(&[])
| --------- ^^^ expected `&Vec<i32>`, found `&[_; 0]`
| |
Expand All @@ -41,6 +43,8 @@ LL + arg.map_or(&[], |v| v)
error[E0308]: mismatched types
--> $DIR/transforming-option-ref-issue-127545.rs:13:19
|
LL | pub fn barzz<'a>(arg: Option<&'a Vec<i32>>, v: &'a [i32]) -> &'a [i32] {
| --------- this return type influences the call expression's return type
LL | arg.unwrap_or(v)
| --------- ^ expected `&Vec<i32>`, found `&[i32]`
| |
Expand All @@ -66,6 +70,8 @@ LL + arg.map_or(v, |v| v)
error[E0308]: mismatched types
--> $DIR/transforming-option-ref-issue-127545.rs:17:19
|
LL | pub fn convert_result(arg: Result<&Vec<i32>, ()>) -> &[i32] {
| ------ this return type influences the call expression's return type
LL | arg.unwrap_or(&[])
| --------- ^^^ expected `&Vec<i32>`, found `&[_; 0]`
| |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ LL + let x: u16 = (S {}).method(0u16);
error[E0308]: arguments to this function are incorrect
--> $DIR/wrong-call-return-type-due-to-generic-arg.rs:27:5
|
LL | fn main() {
| - this implicit `()` return type influences the call expression's return type
...
LL | function(0u32, 8u8)
| ^^^^^^^^ ---- --- expected `bool`, found `u8`
| |
Expand Down
Loading