Skip to content

Be careful about expr_ty_adjusted when noting block tail type #101629

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
Sep 19, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -2713,12 +2713,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
Some(t) if t.hir_owner == parent_id => t,
_ => self.tcx.typeck(parent_id),
};
let ty = typeck_results.expr_ty_adjusted(expr);
let span = expr.peel_blocks().span;
let expr = expr.peel_blocks();
Copy link
Member Author

Choose a reason for hiding this comment

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

peeling first then looking for the expr lets us recover the type in ruby_style_closure, but it's still insufficient for the UI test I added, but oh well. It's probably just not being recorded for some other typeck reason.

let ty = typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error());
let span = expr.span;
if Some(span) != err.span.primary_span() {
err.span_label(
span,
&if ty.references_error() {
if ty.references_error() {
String::new()
} else {
format!("this tail expression is of type `{:?}`", ty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LL | let p = Some(45).and_then({
LL | |
LL | | |x| println!("doubling {}", x);
LL | | Some(x * 2)
| | -----------
| | ----------- this tail expression is of type `std::option::Option<_>`
LL | |
LL | | });
| |_____^ expected an `FnOnce<({integer},)>` closure, found `Option<_>`
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/suggestions/issue-101623.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub struct Stuff {
inner: *mut (),
}

pub struct Wrap<T>(T);

fn fun<T>(t: T) -> Wrap<T> {
todo!()
}

pub trait Trait<'de> {
fn do_stuff(_: Wrap<&'de mut Self>);
}

impl<'a> Trait<'a> for () {
fn do_stuff(_: Wrap<&'a mut Self>) {}
}

fn fun2(t: &mut Stuff) -> () {
let Stuff { inner, .. } = t;
Trait::do_stuff({ fun(&mut *inner) });
//~^ ERROR the trait bound `*mut (): Trait<'_>` is not satisfied
}

fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/issue-101623.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0277]: the trait bound `*mut (): Trait<'_>` is not satisfied
--> $DIR/issue-101623.rs:21:21
|
LL | Trait::do_stuff({ fun(&mut *inner) });
| --------------- ^^----------------^^
| | |
| | the trait `Trait<'_>` is not implemented for `*mut ()`
| required by a bound introduced by this call
|
= help: the trait `Trait<'a>` is implemented for `()`

error: aborting due to previous error

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