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

Lines changed: 1 addition & 51 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 18 additions & 0 deletions
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+
Lines changed: 9 additions & 0 deletions
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`.

0 commit comments

Comments
 (0)