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

Fix invalid suggestion for mismatched types in closure arguments #114256

Merged
merged 1 commit into from
Jul 30, 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
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match binding_parent {
// Check that there is explicit type (ie this is not a closure param with inferred type)
// so we don't suggest moving something to the type that does not exist
hir::Node::Param(hir::Param { ty_span, .. }) if binding.span != *ty_span => {
hir::Node::Param(hir::Param { ty_span, pat, .. }) if pat.span != *ty_span => {
err.multipart_suggestion_verbose(
format!("to take parameter `{binding}` by reference, move `&{mutability}` to the type"),
vec![
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2342,7 +2342,7 @@ impl<'a> Parser<'a> {
let ty = if this.eat(&token::Colon) {
this.parse_ty()?
} else {
this.mk_ty(this.prev_token.span, TyKind::Infer)
this.mk_ty(pat.span, TyKind::Infer)
};

Ok((
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/impl-trait/hidden-type-is-opaque-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

fn reify_as() -> Thunk<impl FnOnce(Continuation) -> Continuation> {
Thunk::new(|mut cont| {
cont.reify_as(); //~ ERROR type annotations needed
//~^ ERROR type annotations needed
cont.reify_as();
cont
})
}
Expand All @@ -15,7 +16,8 @@ type Tait = impl FnOnce(Continuation) -> Continuation;

fn reify_as_tait() -> Thunk<Tait> {
Thunk::new(|mut cont| {
cont.reify_as(); //~ ERROR type annotations needed
//~^ ERROR type annotations needed
cont.reify_as();
cont
})
}
Expand Down
24 changes: 20 additions & 4 deletions tests/ui/impl-trait/hidden-type-is-opaque-2.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
error[E0282]: type annotations needed
--> $DIR/hidden-type-is-opaque-2.rs:9:9
--> $DIR/hidden-type-is-opaque-2.rs:8:17
|
LL | Thunk::new(|mut cont| {
| ^^^^^^^^
LL |
LL | cont.reify_as();
| ^^^^ cannot infer type
| ---- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
LL | Thunk::new(|mut cont: /* Type */| {
| ++++++++++++

error[E0282]: type annotations needed
--> $DIR/hidden-type-is-opaque-2.rs:18:9
--> $DIR/hidden-type-is-opaque-2.rs:18:17
|
LL | Thunk::new(|mut cont| {
| ^^^^^^^^
LL |
LL | cont.reify_as();
| ^^^^ cannot infer type
| ---- type must be known at this point
|
help: consider giving this closure parameter an explicit type
|
LL | Thunk::new(|mut cont: /* Type */| {
| ++++++++++++

error: aborting due to 2 previous errors

Expand Down
8 changes: 8 additions & 0 deletions tests/ui/mismatched_types/closure-ref-114180.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-fail
Copy link
Member

Choose a reason for hiding this comment

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

Wait, actually, can you make this into // run-rustfix test? (also // check-fail is the default iirc, you don't need it)

Copy link
Member Author

@Urgau Urgau Jul 30, 2023

Choose a reason for hiding this comment

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

Unfortunately no, since the current suggestion is not enough to fix the issue; but applying it will lead to another suggestion which fixes the issue.

Copy link
Member

Choose a reason for hiding this comment

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

oof, that's unfortunate


fn main() {
let mut v = vec![(1,)];
let compare = |(a,), (e,)| todo!();
v.sort_by(compare);
//~^ ERROR type mismatch in closure arguments
}
22 changes: 22 additions & 0 deletions tests/ui/mismatched_types/closure-ref-114180.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0631]: type mismatch in closure arguments
--> $DIR/closure-ref-114180.rs:6:15
|
LL | let compare = |(a,), (e,)| todo!();
| ------------ found signature defined here
LL | v.sort_by(compare);
| ------- ^^^^^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected closure signature `for<'a, 'b> fn(&'a ({integer},), &'b ({integer},)) -> _`
found closure signature `fn((_,), (_,)) -> _`
note: required by a bound in `slice::<impl [T]>::sort_by`
--> $SRC_DIR/alloc/src/slice.rs:LL:COL
help: consider adjusting the signature so it borrows its arguments
|
LL | let compare = |&(a,), &(e,)| todo!();
| + +

error: aborting due to previous error

For more information about this error, try `rustc --explain E0631`.
44 changes: 22 additions & 22 deletions tests/ui/mismatched_types/ref-pat-suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:11:23
|
LL | let _: fn(u32) = |&_a| ();
| ^--
| ||
| |expected due to this
| ^^^
| |
| expected `u32`, found `&_`
| expected due to this
|
= note: expected type `u32`
found reference `&_`
Expand All @@ -120,10 +120,10 @@ error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:12:23
|
LL | let _: fn(u32) = |&mut _a| ();
| ^^^^^--
| | |
| | expected due to this
| ^^^^^^^
| |
| expected `u32`, found `&mut _`
| expected due to this
|
= note: expected type `u32`
found mutable reference `&mut _`
Expand All @@ -142,10 +142,10 @@ error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:13:25
|
LL | let _: fn(&u32) = |&&_a| ();
| ^--
| ||
| |expected due to this
| expected `u32`, found `&_`
| -^^^
| ||
| |expected `u32`, found `&_`
| expected due to this
|
= note: expected type `u32`
found reference `&_`
Expand All @@ -159,10 +159,10 @@ error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:14:33
|
LL | let _: fn(&mut u32) = |&mut &_a| ();
| ^--
| ||
| |expected due to this
| expected `u32`, found `&_`
| -----^^^
| | |
| | expected `u32`, found `&_`
| expected due to this
|
= note: expected type `u32`
found reference `&_`
Expand All @@ -176,10 +176,10 @@ error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:15:25
|
LL | let _: fn(&u32) = |&&mut _a| ();
| ^^^^^--
| | |
| | expected due to this
| expected `u32`, found `&mut _`
| -^^^^^^^
| ||
| |expected `u32`, found `&mut _`
| expected due to this
|
= note: expected type `u32`
found mutable reference `&mut _`
Expand All @@ -193,10 +193,10 @@ error[E0308]: mismatched types
--> $DIR/ref-pat-suggestions.rs:16:33
|
LL | let _: fn(&mut u32) = |&mut &mut _a| ();
| ^^^^^--
| | |
| | expected due to this
| expected `u32`, found `&mut _`
| -----^^^^^^^
| | |
| | expected `u32`, found `&mut _`
| expected due to this
|
= note: expected type `u32`
found mutable reference `&mut _`
Expand Down
Loading