Skip to content

Commit 4ea1a3b

Browse files
Remove some unnecessary check logic for lang items in HIR typeck
1 parent fb4bca0 commit 4ea1a3b

27 files changed

+21
-445
lines changed

compiler/rustc_hir_typeck/messages.ftl

-5
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ hir_typeck_lossy_provenance_ptr2int =
9696
hir_typeck_method_call_on_unknown_raw_pointee =
9797
cannot call a method on a raw pointer with an unknown pointee type
9898
99-
hir_typeck_missing_fn_lang_items = failed to find an overloaded call trait for closure call
100-
.help = make sure the `fn`/`fn_mut`/`fn_once` lang items are defined and have correctly defined `call`/`call_mut`/`call_once` methods
101-
10299
hir_typeck_missing_parentheses_in_range = can't call method `{$method_name}` on type `{$ty_str}`
103100
104101
hir_typeck_no_associated_item = no {$item_kind} named `{$item_name}` found for {$ty_prefix} `{$ty_str}`{$trait_missing_method ->
@@ -108,8 +105,6 @@ hir_typeck_no_associated_item = no {$item_kind} named `{$item_name}` found for {
108105
109106
hir_typeck_note_edition_guide = for more on editions, read https://doc.rust-lang.org/edition-guide
110107
111-
hir_typeck_op_trait_generic_params = `{$method_name}` must not have any generic parameters
112-
113108
hir_typeck_option_result_asref = use `{$def_path}::as_ref` to convert `{$expected_ty}` to `{$expr_ty}`
114109
hir_typeck_option_result_cloned = use `{$def_path}::cloned` to clone the value inside the `{$def_path}`
115110
hir_typeck_option_result_copied = use `{$def_path}::copied` to copy the value inside the `{$def_path}`

compiler/rustc_hir_typeck/src/callee.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -253,28 +253,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
253253
adjusted_ty,
254254
opt_input_type.as_ref().map(slice::from_ref),
255255
) {
256-
// Check for `self` receiver on the method, otherwise we can't use this as a `Fn*` trait.
257-
if !self.tcx.associated_item(ok.value.def_id).fn_has_self_parameter {
258-
self.dcx().span_delayed_bug(
259-
call_expr.span,
260-
"input to overloaded call fn is not a self receiver",
261-
);
262-
return None;
263-
}
264-
265256
let method = self.register_infer_ok_obligations(ok);
266257
let mut autoref = None;
267258
if borrow {
268259
// Check for &self vs &mut self in the method signature. Since this is either
269260
// the Fn or FnMut trait, it should be one of those.
270261
let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() else {
271-
// The `fn`/`fn_mut` lang item is ill-formed, which should have
272-
// caused an error elsewhere.
273-
self.dcx().span_delayed_bug(
274-
call_expr.span,
275-
"input to call/call_mut is not a ref",
276-
);
277-
return None;
262+
bug!("Expected `FnMut`/`Fn` to take receiver by-ref/by-mut")
278263
};
279264

280265
// For initial two-phase borrow
@@ -948,9 +933,11 @@ impl<'a, 'tcx> DeferredCallResolution<'tcx> {
948933
);
949934
}
950935
None => {
951-
// This can happen if `#![no_core]` is used and the `fn/fn_mut/fn_once`
952-
// lang items are not defined (issue #86238).
953-
fcx.dcx().emit_err(errors::MissingFnLangItems { span: self.call_expr.span });
936+
span_bug!(
937+
call_expr.span,
938+
"Expected to find a suitable `Fn`/`FnMut`/`FnOnce` implementation for `{}`",
939+
self.adjusted_ty
940+
)
954941
}
955942
}
956943
}

compiler/rustc_hir_typeck/src/check.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,22 @@ pub(super) fn check_fn<'a, 'tcx>(
152152
}
153153

154154
fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>) {
155+
let span = tcx.def_span(fn_id);
156+
155157
let DefKind::Fn = tcx.def_kind(fn_id) else {
156-
let span = tcx.def_span(fn_id);
157158
tcx.dcx().span_err(span, "should be a function");
158159
return;
159160
};
160161

161162
let generic_counts = tcx.generics_of(fn_id).own_counts();
162163
if generic_counts.types != 0 {
163-
let span = tcx.def_span(fn_id);
164164
tcx.dcx().span_err(span, "should have no type parameters");
165165
}
166166
if generic_counts.consts != 0 {
167-
let span = tcx.def_span(fn_id);
168167
tcx.dcx().span_err(span, "should have no const parameters");
169168
}
170169

171-
let Some(panic_info_did) = tcx.lang_items().panic_info() else {
172-
tcx.dcx().err("language item required, but not found: `panic_info`");
173-
return;
174-
};
170+
let panic_info_did = tcx.require_lang_item(hir::LangItem::PanicInfo, Some(span));
175171

176172
// build type `for<'a, 'b> fn(&'a PanicInfo<'b>) -> !`
177173
let panic_info_ty = tcx.type_of(panic_info_did).instantiate(
@@ -203,11 +199,7 @@ fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>
203199

204200
let _ = check_function_signature(
205201
tcx,
206-
ObligationCause::new(
207-
tcx.def_span(fn_id),
208-
fn_id,
209-
ObligationCauseCode::LangFunctionType(sym::panic_impl),
210-
),
202+
ObligationCause::new(span, fn_id, ObligationCauseCode::LangFunctionType(sym::panic_impl)),
211203
fn_id.into(),
212204
expected_sig,
213205
);

compiler/rustc_hir_typeck/src/errors.rs

-16
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ pub struct MethodCallOnUnknownRawPointee {
8383
pub span: Span,
8484
}
8585

86-
#[derive(Diagnostic)]
87-
#[diag(hir_typeck_missing_fn_lang_items)]
88-
#[help]
89-
pub struct MissingFnLangItems {
90-
#[primary_span]
91-
pub span: Span,
92-
}
93-
9486
#[derive(Diagnostic)]
9587
#[diag(hir_typeck_functional_record_update_on_non_struct, code = E0436)]
9688
pub struct FunctionalRecordUpdateOnNonStruct {
@@ -193,14 +185,6 @@ pub struct AddMissingParenthesesInRange {
193185
pub right: Span,
194186
}
195187

196-
#[derive(Diagnostic)]
197-
#[diag(hir_typeck_op_trait_generic_params)]
198-
pub struct OpMethodGenericParams {
199-
#[primary_span]
200-
pub span: Span,
201-
pub method_name: String,
202-
}
203-
204188
pub struct TypeMismatchFruTypo {
205189
/// Span of the LHS of the range
206190
pub expr_span: Span,

compiler/rustc_hir_typeck/src/lib.rs

-9
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,6 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
444444
diag.emit()
445445
}
446446

447-
/// `expected` here is the expected number of explicit generic arguments on the trait.
448-
fn has_expected_num_generic_args(tcx: TyCtxt<'_>, trait_did: DefId, expected: usize) -> bool {
449-
let generics = tcx.generics_of(trait_did);
450-
generics.count()
451-
== expected
452-
+ if generics.has_self { 1 } else { 0 }
453-
+ if generics.host_effect_index.is_some() { 1 } else { 0 }
454-
}
455-
456447
pub fn provide(providers: &mut Providers) {
457448
method::provide(providers);
458449
*providers = Providers {

compiler/rustc_hir_typeck/src/method/mod.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod suggest;
1010
pub use self::suggest::SelfSource;
1111
pub use self::MethodError::*;
1212

13-
use crate::errors::OpMethodGenericParams;
1413
use crate::FnCtxt;
1514
use rustc_errors::{Applicability, Diagnostic, SubdiagnosticMessage};
1615
use rustc_hir as hir;
@@ -385,26 +384,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
385384
// type parameters or early-bound regions.
386385
let tcx = self.tcx;
387386
let Some(method_item) = self.associated_value(trait_def_id, m_name) else {
388-
tcx.dcx().span_delayed_bug(
389-
obligation.cause.span,
390-
"operator trait does not have corresponding operator method",
391-
);
392-
return None;
387+
bug!("expected associated item for operator trait")
393388
};
394389

395-
if method_item.kind != ty::AssocKind::Fn {
396-
self.dcx().span_delayed_bug(tcx.def_span(method_item.def_id), "not a method");
397-
return None;
398-
}
399-
400390
let def_id = method_item.def_id;
401-
let generics = tcx.generics_of(def_id);
402-
403-
if generics.params.len() != 0 {
404-
tcx.dcx().emit_fatal(OpMethodGenericParams {
405-
span: tcx.def_span(method_item.def_id),
406-
method_name: m_name.to_string(),
407-
});
391+
if method_item.kind != ty::AssocKind::Fn {
392+
span_bug!(tcx.def_span(def_id), "expected `{m_name}` to be an associated function");
408393
}
409394

410395
debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);

compiler/rustc_hir_typeck/src/op.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Code related to processing overloaded binary and unary operators.
22
33
use super::method::MethodCallee;
4-
use super::{has_expected_num_generic_args, FnCtxt};
4+
use super::FnCtxt;
55
use crate::Expectation;
66
use rustc_ast as ast;
77
use rustc_data_structures::packed::Pu128;
@@ -887,25 +887,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
887887
lhs_ty, op, opname, trait_did
888888
);
889889

890-
// Catches cases like #83893, where a lang item is declared with the
891-
// wrong number of generic arguments. Should have yielded an error
892-
// elsewhere by now, but we have to catch it here so that we do not
893-
// index `other_tys` out of bounds (if the lang item has too many
894-
// generic arguments, `other_tys` is too short).
895-
if !has_expected_num_generic_args(
896-
self.tcx,
897-
trait_did,
898-
match op {
899-
// Binary ops have a generic right-hand side, unary ops don't
900-
Op::Binary(..) => 1,
901-
Op::Unary(..) => 0,
902-
},
903-
) {
904-
self.dcx()
905-
.span_delayed_bug(span, "operator didn't have the right number of generic args");
906-
return Err(vec![]);
907-
}
908-
909890
let opname = Ident::with_dummy_span(opname);
910891
let (opt_rhs_expr, opt_rhs_ty) = opt_rhs.unzip();
911892
let input_types = opt_rhs_ty.as_slice();

compiler/rustc_hir_typeck/src/place_op.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::method::MethodCallee;
2-
use crate::{has_expected_num_generic_args, FnCtxt, PlaceOp};
2+
use crate::{FnCtxt, PlaceOp};
33
use rustc_ast as ast;
44
use rustc_errors::Applicability;
55
use rustc_hir as hir;
@@ -209,20 +209,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
209209
return None;
210210
};
211211

212-
// If the lang item was declared incorrectly, stop here so that we don't
213-
// run into an ICE (#83893). The error is reported where the lang item is
214-
// declared.
215-
if !has_expected_num_generic_args(
216-
self.tcx,
217-
imm_tr,
218-
match op {
219-
PlaceOp::Deref => 0,
220-
PlaceOp::Index => 1,
221-
},
222-
) {
223-
return None;
224-
}
225-
226212
self.lookup_method_in_trait(
227213
self.misc(span),
228214
Ident::with_dummy_span(imm_op),
@@ -249,20 +235,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
249235
return None;
250236
};
251237

252-
// If the lang item was declared incorrectly, stop here so that we don't
253-
// run into an ICE (#83893). The error is reported where the lang item is
254-
// declared.
255-
if !has_expected_num_generic_args(
256-
self.tcx,
257-
mut_tr,
258-
match op {
259-
PlaceOp::Deref => 0,
260-
PlaceOp::Index => 1,
261-
},
262-
) {
263-
return None;
264-
}
265-
266238
self.lookup_method_in_trait(
267239
self.misc(span),
268240
Ident::with_dummy_span(mut_op),

tests/ui/lang-items/bad-add-impl.rs

-18
This file was deleted.

tests/ui/lang-items/bad-add-impl.stderr

-11
This file was deleted.

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

-18
This file was deleted.

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

-18
This file was deleted.

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

-18
This file was deleted.

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

-18
This file was deleted.

0 commit comments

Comments
 (0)