Skip to content

Move Fn* traits malformedness protections to typeck #108138

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

Merged
merged 3 commits into from
Feb 21, 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
52 changes: 1 addition & 51 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_middle::mir::ConstraintCategory;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::trait_def::TraitSpecializationKind;
use rustc_middle::ty::{
self, ir::TypeVisitor, AdtKind, DefIdTree, GenericParamDefKind, Ty, TyCtxt, TypeFoldable,
self, ir::TypeVisitor, AdtKind, GenericParamDefKind, Ty, TyCtxt, TypeFoldable,
TypeSuperVisitable,
};
use rustc_middle::ty::{GenericArgKind, InternalSubsts};
Expand Down Expand Up @@ -277,56 +277,6 @@ fn check_trait_item(tcx: TyCtxt<'_>, trait_item: &hir::TraitItem<'_>) {
};
check_object_unsafe_self_trait_by_name(tcx, trait_item);
check_associated_item(tcx, def_id, span, method_sig);

let encl_trait_def_id = tcx.local_parent(def_id);
let encl_trait = tcx.hir().expect_item(encl_trait_def_id);
let encl_trait_def_id = encl_trait.owner_id.to_def_id();
let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() {
Some("fn")
} else if Some(encl_trait_def_id) == tcx.lang_items().fn_mut_trait() {
Some("fn_mut")
} else {
None
};

if let (Some(fn_lang_item_name), "call") =
(fn_lang_item_name, trait_item.ident.name.to_ident_string().as_str())
{
// We are looking at the `call` function of the `fn` or `fn_mut` lang item.
// Do some rudimentary sanity checking to avoid an ICE later (issue #83471).
if let Some(hir::FnSig { decl, span, .. }) = method_sig {
if let [self_ty, _] = decl.inputs {
if !matches!(self_ty.kind, hir::TyKind::Ref(_, _)) {
tcx.sess
.struct_span_err(
self_ty.span,
&format!(
"first argument of `call` in `{fn_lang_item_name}` lang item must be a reference",
),
)
.emit();
}
} else {
tcx.sess
.struct_span_err(
*span,
&format!(
"`call` function in `{fn_lang_item_name}` lang item takes exactly two arguments",
),
)
.emit();
}
} else {
tcx.sess
.struct_span_err(
trait_item.span,
&format!(
"`call` trait item in `{fn_lang_item_name}` lang item must be a function",
),
)
.emit();
}
}
}

/// Require that the user writes where clauses on GATs for the implicit
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
adjusted_ty,
opt_input_type.as_ref().map(slice::from_ref),
) {
// Check for `self` receiver on the method, otherwise we can't use this as a `Fn*` trait.
if !self.tcx.associated_item(ok.value.def_id).fn_has_self_parameter {
self.tcx.sess.delay_span_bug(
call_expr.span,
"input to overloaded call fn is not a self receiver",
);
return None;
}

let method = self.register_infer_ok_obligations(ok);
let mut autoref = None;
if borrow {
Expand All @@ -257,7 +266,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// caused an error elsewhere.
self.tcx
.sess
.delay_span_bug(call_expr.span, "input to call/call_mut is not a ref?");
.delay_span_bug(call_expr.span, "input to call/call_mut is not a ref");
return None;
};

Expand All @@ -271,6 +280,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
target: method.sig.inputs()[0],
});
}

return Some((autoref, method));
}
}
Expand Down Expand Up @@ -823,7 +833,7 @@ impl<'a, 'tcx> DeferredCallResolution<'tcx> {
);
err.help(
"make sure the `fn`/`fn_mut`/`fn_once` lang items are defined \
and have associated `call`/`call_mut`/`call_once` functions",
and have correctly defined `call`/`call_mut`/`call_once` methods",
);
err.emit();
}
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
return None;
};

if method_item.kind != ty::AssocKind::Fn {
self.tcx.sess.delay_span_bug(tcx.def_span(method_item.def_id), "not a method");
return None;
}

let def_id = method_item.def_id;
let generics = tcx.generics_of(def_id);

Expand Down
18 changes: 18 additions & 0 deletions tests/ui/lang-items/bad-add-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(no_core)]
#![feature(lang_items)]
#![no_core]

#[lang = "sized"]
trait Sized {}

#[lang = "add"]
trait Add<T> {
const add: u32 = 1u32;
}

impl Add<u32> for u32 {}

fn main() {
1u32 + 1u32;
//~^ ERROR cannot add `u32` to `u32`
}
11 changes: 11 additions & 0 deletions tests/ui/lang-items/bad-add-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0369]: cannot add `u32` to `u32`
--> $DIR/bad-add-impl.rs:16:10
|
LL | 1u32 + 1u32;
| ---- ^ ---- u32
| |
| u32

error: aborting due to previous error

For more information about this error, try `rustc --explain E0369`.
18 changes: 18 additions & 0 deletions tests/ui/lang-items/fn-fn_mut-call-ill-formed.bad_item.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:39:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:43:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

18 changes: 18 additions & 0 deletions tests/ui/lang-items/fn-fn_mut-call-ill-formed.bad_sig.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:39:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:43:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

18 changes: 18 additions & 0 deletions tests/ui/lang-items/fn-fn_mut-call-ill-formed.fn_bad_item.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

18 changes: 18 additions & 0 deletions tests/ui/lang-items/fn-fn_mut-call-ill-formed.fn_bad_sig.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:42:5
|
LL | a();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: failed to find an overloaded call trait for closure call
--> $DIR/fn-fn_mut-call-ill-formed.rs:47:5
|
LL | b();
| ^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to 2 previous errors

34 changes: 28 additions & 6 deletions tests/ui/lang-items/fn-fn_mut-call-ill-formed.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
// Make sure that an error is reported if the `call` function of the
// `fn`/`fn_mut` lang item is grossly ill-formed.
// revisions: fn_once_bad_item fn_once_bad_sig fn_mut_bad_item fn_mut_bad_sig fn_bad_item fn_bad_sig

#![feature(lang_items)]
#![feature(no_core)]
#![no_core]

#[lang = "sized"]
trait Sized {}

#[cfg(any(fn_bad_item, fn_bad_sig))]
#[lang = "fn"]
trait MyFn<T> {
#[cfg(fn_bad_sig)]
fn call(i: i32) -> i32 { 0 }

#[cfg(fn_bad_item)]
const call: i32 = 42;
//~^ ERROR: `call` trait item in `fn` lang item must be a function
}

#[cfg(any(fn_mut_bad_item, fn_mut_bad_sig))]
#[lang = "fn_mut"]
trait MyFnMut<T> {
fn call(i: i32, j: i32) -> i32 { i + j }
//~^ ERROR: first argument of `call` in `fn_mut` lang item must be a reference
#[cfg(fn_mut_bad_sig)]
fn call_mut(i: i32) -> i32 { 0 }

#[cfg(fn_mut_bad_item)]
const call_mut: i32 = 42;
}

#[cfg(any(fn_once_bad_item, fn_once_bad_sig))]
#[lang = "fn_once"]
trait MyFnOnce<T> {
#[cfg(fn_once_bad_sig)]
fn call_once(i: i32) -> i32 { 0 }

#[cfg(fn_once_bad_item)]
const call_once: i32 = 42;
}

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

let mut i = 0;
let mut b = || { i += 1; };
let mut b = || { };
b();
//~^ ERROR failed to find an overloaded call trait for closure call
}
14 changes: 0 additions & 14 deletions tests/ui/lang-items/fn-fn_mut-call-ill-formed.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/lang-items/issue-86238.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: failed to find an overloaded call trait for closure call
LL | one()
| ^^^^^
|
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have associated `call`/`call_mut`/`call_once` functions
= help: make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods

error: aborting due to previous error