Skip to content

Commit 3dd638f

Browse files
Move call trait lang item malformed check to typeck
1 parent c528357 commit 3dd638f

12 files changed

+170
-71
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-51
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_middle::mir::ConstraintCategory;
1616
use rustc_middle::ty::query::Providers;
1717
use rustc_middle::ty::trait_def::TraitSpecializationKind;
1818
use rustc_middle::ty::{
19-
self, ir::TypeVisitor, AdtKind, DefIdTree, GenericParamDefKind, Ty, TyCtxt, TypeFoldable,
19+
self, ir::TypeVisitor, AdtKind, GenericParamDefKind, Ty, TyCtxt, TypeFoldable,
2020
TypeSuperVisitable,
2121
};
2222
use rustc_middle::ty::{GenericArgKind, InternalSubsts};
@@ -277,56 +277,6 @@ fn check_trait_item(tcx: TyCtxt<'_>, trait_item: &hir::TraitItem<'_>) {
277277
};
278278
check_object_unsafe_self_trait_by_name(tcx, trait_item);
279279
check_associated_item(tcx, def_id, span, method_sig);
280-
281-
let encl_trait_def_id = tcx.local_parent(def_id);
282-
let encl_trait = tcx.hir().expect_item(encl_trait_def_id);
283-
let encl_trait_def_id = encl_trait.owner_id.to_def_id();
284-
let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() {
285-
Some("fn")
286-
} else if Some(encl_trait_def_id) == tcx.lang_items().fn_mut_trait() {
287-
Some("fn_mut")
288-
} else {
289-
None
290-
};
291-
292-
if let (Some(fn_lang_item_name), "call") =
293-
(fn_lang_item_name, trait_item.ident.name.to_ident_string().as_str())
294-
{
295-
// We are looking at the `call` function of the `fn` or `fn_mut` lang item.
296-
// Do some rudimentary sanity checking to avoid an ICE later (issue #83471).
297-
if let Some(hir::FnSig { decl, span, .. }) = method_sig {
298-
if let [self_ty, _] = decl.inputs {
299-
if !matches!(self_ty.kind, hir::TyKind::Ref(_, _)) {
300-
tcx.sess
301-
.struct_span_err(
302-
self_ty.span,
303-
&format!(
304-
"first argument of `call` in `{fn_lang_item_name}` lang item must be a reference",
305-
),
306-
)
307-
.emit();
308-
}
309-
} else {
310-
tcx.sess
311-
.struct_span_err(
312-
*span,
313-
&format!(
314-
"`call` function in `{fn_lang_item_name}` lang item takes exactly two arguments",
315-
),
316-
)
317-
.emit();
318-
}
319-
} else {
320-
tcx.sess
321-
.struct_span_err(
322-
trait_item.span,
323-
&format!(
324-
"`call` trait item in `{fn_lang_item_name}` lang item must be a function",
325-
),
326-
)
327-
.emit();
328-
}
329-
}
330280
}
331281

332282
/// Require that the user writes where clauses on GATs for the implicit

compiler/rustc_hir_typeck/src/method/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
380380
);
381381
return None;
382382
};
383+
384+
if method_item.kind != ty::AssocKind::Fn {
385+
self.tcx.sess.delay_span_bug(tcx.def_span(method_item.def_id), "not a method");
386+
return None;
387+
}
388+
383389
let def_id = method_item.def_id;
384390
let generics = tcx.generics_of(def_id);
385391

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:39:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:43:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:39:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:43:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: failed to find an overloaded call trait for closure call
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
3+
|
4+
LL | a();
5+
| ^^^
6+
|
7+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
8+
9+
error: failed to find an overloaded call trait for closure call
10+
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
11+
|
12+
LL | b();
13+
| ^^^
14+
|
15+
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
3+
|
4+
LL | b();
5+
| ^^^ expected `i32`, found `()`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
1-
// Make sure that an error is reported if the `call` function of the
2-
// `fn`/`fn_mut` lang item is grossly ill-formed.
1+
// revisions: fn_once_bad_item fn_once_bad_sig fn_mut_bad_item fn_mut_bad_sig fn_bad_item fn_bad_sig
32

43
#![feature(lang_items)]
54
#![feature(no_core)]
65
#![no_core]
76

7+
#[lang = "sized"]
8+
trait Sized {}
9+
10+
#[cfg(any(fn_bad_item, fn_bad_sig))]
811
#[lang = "fn"]
912
trait MyFn<T> {
13+
#[cfg(fn_bad_sig)]
14+
fn call(i: i32) -> i32 { 0 }
15+
16+
#[cfg(fn_bad_item)]
1017
const call: i32 = 42;
11-
//~^ ERROR: `call` trait item in `fn` lang item must be a function
1218
}
1319

20+
#[cfg(any(fn_mut_bad_item, fn_mut_bad_sig))]
1421
#[lang = "fn_mut"]
1522
trait MyFnMut<T> {
16-
fn call(i: i32, j: i32) -> i32 { i + j }
17-
//~^ ERROR: first argument of `call` in `fn_mut` lang item must be a reference
23+
#[cfg(fn_mut_bad_sig)]
24+
fn call_mut(i: i32) -> i32 { 0 }
25+
26+
#[cfg(fn_mut_bad_item)]
27+
const call_mut: i32 = 42;
28+
}
29+
30+
#[cfg(any(fn_once_bad_item, fn_once_bad_sig))]
31+
#[lang = "fn_once"]
32+
trait MyFnOnce<T> {
33+
#[cfg(fn_once_bad_sig)]
34+
fn call_once(i: i32) -> i32 { 0 }
35+
36+
#[cfg(fn_once_bad_item)]
37+
const call_once: i32 = 42;
1838
}
1939

2040
fn main() {
2141
let a = || 42;
2242
a();
43+
//~^ ERROR failed to find an overloaded call trait for closure call
2344

2445
let mut i = 0;
25-
let mut b = || { i += 1; };
46+
let mut b = || { };
2647
b();
48+
//~^ ERROR failed to find an overloaded call trait for closure call
2749
}

tests/ui/lang-items/fn-fn_mut-call-ill-formed.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)