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 ClosureKind::to_def_id #104724

Merged
merged 1 commit into from
Nov 23, 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
14 changes: 9 additions & 5 deletions compiler/rustc_middle/src/ty/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{mir, ty};

use std::fmt::Write;

use hir::LangItem;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -130,11 +131,14 @@ impl<'tcx> ClosureKind {
}

pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
match self {
ClosureKind::Fn => tcx.lang_items().fn_once_trait().unwrap(),
ClosureKind::FnMut => tcx.lang_items().fn_mut_trait().unwrap(),
ClosureKind::FnOnce => tcx.lang_items().fn_trait().unwrap(),
}
tcx.require_lang_item(
match self {
ClosureKind::Fn => LangItem::Fn,
ClosureKind::FnMut => LangItem::FnMut,
ClosureKind::FnOnce => LangItem::FnOnce,
},
None,
)
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/test/ui/mismatched_types/overloaded-calls-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ impl FnOnce<(isize,)> for S {
}
}

struct F;

impl FnOnce<(i32,)> for F {
type Output = ();

extern "rust-call" fn call_once(self, args: (i32,)) -> Self::Output {}
}

fn main() {
let mut s = S {
x: 3,
y: 3,
};
let ans = s("what"); //~ ERROR mismatched types
let mut s = S { x: 3, y: 3 };
let ans = s("what");
//~^ ERROR mismatched types
let ans = s();
//~^ ERROR this function takes 1 argument but 0 arguments were supplied
let ans = s("burma", "shave");
//~^ ERROR this function takes 1 argument but 2 arguments were supplied

F("");
//~^ ERROR mismatched types
}
22 changes: 18 additions & 4 deletions src/test/ui/mismatched_types/overloaded-calls-bad.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/overloaded-calls-bad.rs:28:17
--> $DIR/overloaded-calls-bad.rs:33:17
|
LL | let ans = s("what");
| - ^^^^^^ expected `isize`, found `&str`
Expand All @@ -13,7 +13,7 @@ LL | impl FnMut<(isize,)> for S {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0057]: this function takes 1 argument but 0 arguments were supplied
--> $DIR/overloaded-calls-bad.rs:29:15
--> $DIR/overloaded-calls-bad.rs:35:15
|
LL | let ans = s();
| ^-- an argument of type `isize` is missing
Expand All @@ -29,7 +29,7 @@ LL | let ans = s(/* isize */);
| ~~~~~~~~~~~~~

error[E0057]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/overloaded-calls-bad.rs:31:15
--> $DIR/overloaded-calls-bad.rs:37:15
|
LL | let ans = s("burma", "shave");
| ^ ------- ------- argument of type `&'static str` unexpected
Expand All @@ -46,7 +46,21 @@ help: remove the extra argument
LL | let ans = s(/* isize */);
| ~~~~~~~~~~~~~

error: aborting due to 3 previous errors
error[E0308]: mismatched types
--> $DIR/overloaded-calls-bad.rs:40:7
|
LL | F("");
| - ^^ expected `i32`, found `&str`
| |
| arguments to this struct are incorrect
|
note: implementation defined here
--> $DIR/overloaded-calls-bad.rs:25:1
|
LL | impl FnOnce<(i32,)> for F {
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0057, E0308.
For more information about an error, try `rustc --explain E0057`.