Skip to content

Commit 1e65fcc

Browse files
authored
Rollup merge of rust-lang#104724 - WaffleLapkin:to_def_idn't, r=compiler-errors
Fix `ClosureKind::to_def_id` `Fn` and `FnOnce` were mixed up in rust-lang#99131.
2 parents 48bbae7 + 04610ad commit 1e65fcc

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

compiler/rustc_middle/src/ty/closure.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{mir, ty};
55

66
use std::fmt::Write;
77

8+
use hir::LangItem;
89
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
910
use rustc_hir as hir;
1011
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -130,11 +131,14 @@ impl<'tcx> ClosureKind {
130131
}
131132

132133
pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
133-
match self {
134-
ClosureKind::Fn => tcx.lang_items().fn_once_trait().unwrap(),
135-
ClosureKind::FnMut => tcx.lang_items().fn_mut_trait().unwrap(),
136-
ClosureKind::FnOnce => tcx.lang_items().fn_trait().unwrap(),
137-
}
134+
tcx.require_lang_item(
135+
match self {
136+
ClosureKind::Fn => LangItem::Fn,
137+
ClosureKind::FnMut => LangItem::FnMut,
138+
ClosureKind::FnOnce => LangItem::FnOnce,
139+
},
140+
None,
141+
)
138142
}
139143
}
140144

src/test/ui/mismatched_types/overloaded-calls-bad.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ impl FnOnce<(isize,)> for S {
2020
}
2121
}
2222

23+
struct F;
24+
25+
impl FnOnce<(i32,)> for F {
26+
type Output = ();
27+
28+
extern "rust-call" fn call_once(self, args: (i32,)) -> Self::Output {}
29+
}
30+
2331
fn main() {
24-
let mut s = S {
25-
x: 3,
26-
y: 3,
27-
};
28-
let ans = s("what"); //~ ERROR mismatched types
32+
let mut s = S { x: 3, y: 3 };
33+
let ans = s("what");
34+
//~^ ERROR mismatched types
2935
let ans = s();
3036
//~^ ERROR this function takes 1 argument but 0 arguments were supplied
3137
let ans = s("burma", "shave");
3238
//~^ ERROR this function takes 1 argument but 2 arguments were supplied
39+
40+
F("");
41+
//~^ ERROR mismatched types
3342
}

src/test/ui/mismatched_types/overloaded-calls-bad.stderr

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/overloaded-calls-bad.rs:28:17
2+
--> $DIR/overloaded-calls-bad.rs:33:17
33
|
44
LL | let ans = s("what");
55
| - ^^^^^^ expected `isize`, found `&str`
@@ -13,7 +13,7 @@ LL | impl FnMut<(isize,)> for S {
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

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

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

49-
error: aborting due to 3 previous errors
49+
error[E0308]: mismatched types
50+
--> $DIR/overloaded-calls-bad.rs:40:7
51+
|
52+
LL | F("");
53+
| - ^^ expected `i32`, found `&str`
54+
| |
55+
| arguments to this struct are incorrect
56+
|
57+
note: implementation defined here
58+
--> $DIR/overloaded-calls-bad.rs:25:1
59+
|
60+
LL | impl FnOnce<(i32,)> for F {
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
62+
63+
error: aborting due to 4 previous errors
5064

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

0 commit comments

Comments
 (0)