Skip to content

Commit 99b63d0

Browse files
committed
Auto merge of #115897 - eduardosm:check-fn-sig, r=compiler-errors
rustc_hir_analysis: add a helper to check function the signature mismatches This function is now used to check `#[panic_handler]`, `start` lang item, `main`, `#[start]` and intrinsic functions. The diagnosis produced are now closer to the ones produced by trait/impl method signature mismatch. This is the first time I do anything with rustc_hir_analysis/rustc_hir_typeck, so comments and suggestions about things I did wrong or that could be improved will be appreciated.
2 parents b3aa8e7 + 85d61b0 commit 99b63d0

File tree

48 files changed

+410
-387
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+410
-387
lines changed

compiler/rustc_hir_analysis/src/check/entry.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
1111

1212
use std::ops::Not;
1313

14+
use super::check_function_signature;
1415
use crate::errors;
15-
use crate::require_same_types;
1616

1717
pub(crate) fn check_for_entry_fn(tcx: TyCtxt<'_>) {
1818
match tcx.entry_fn(()) {
@@ -162,33 +162,33 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
162162
error = true;
163163
}
164164
// now we can take the return type of the given main function
165-
expected_return_type = main_fnsig.output();
165+
expected_return_type = norm_return_ty;
166166
} else {
167167
// standard () main return type
168-
expected_return_type = ty::Binder::dummy(Ty::new_unit(tcx));
168+
expected_return_type = tcx.types.unit;
169169
}
170170

171171
if error {
172172
return;
173173
}
174174

175-
let se_ty = Ty::new_fn_ptr(
176-
tcx,
177-
expected_return_type.map_bound(|expected_return_type| {
178-
tcx.mk_fn_sig([], expected_return_type, false, hir::Unsafety::Normal, Abi::Rust)
179-
}),
180-
);
175+
let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig(
176+
[],
177+
expected_return_type,
178+
false,
179+
hir::Unsafety::Normal,
180+
Abi::Rust,
181+
));
181182

182-
require_same_types(
183+
check_function_signature(
183184
tcx,
184-
&ObligationCause::new(
185+
ObligationCause::new(
185186
main_span,
186187
main_diagnostics_def_id,
187188
ObligationCauseCode::MainFunctionType,
188189
),
189-
param_env,
190-
se_ty,
191-
Ty::new_fn_ptr(tcx, main_fnsig),
190+
main_def_id,
191+
expected_sig,
192192
);
193193
}
194194

@@ -247,27 +247,23 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
247247
}
248248
}
249249

250-
let se_ty = Ty::new_fn_ptr(
251-
tcx,
252-
ty::Binder::dummy(tcx.mk_fn_sig(
253-
[tcx.types.isize, Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8))],
254-
tcx.types.isize,
255-
false,
256-
hir::Unsafety::Normal,
257-
Abi::Rust,
258-
)),
259-
);
250+
let expected_sig = ty::Binder::dummy(tcx.mk_fn_sig(
251+
[tcx.types.isize, Ty::new_imm_ptr(tcx, Ty::new_imm_ptr(tcx, tcx.types.u8))],
252+
tcx.types.isize,
253+
false,
254+
hir::Unsafety::Normal,
255+
Abi::Rust,
256+
));
260257

261-
require_same_types(
258+
check_function_signature(
262259
tcx,
263-
&ObligationCause::new(
260+
ObligationCause::new(
264261
start_span,
265262
start_def_id,
266263
ObligationCauseCode::StartFunctionType,
267264
),
268-
ty::ParamEnv::empty(), // start should not have any where bounds.
269-
se_ty,
270-
Ty::new_fn_ptr(tcx, tcx.fn_sig(start_def_id).instantiate_identity()),
265+
start_def_id.into(),
266+
expected_sig,
271267
);
272268
}
273269
_ => {

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Type-checking for the rust-intrinsic and platform-intrinsic
22
//! intrinsics that the compiler exposes.
33
4+
use crate::check::check_function_signature;
45
use crate::errors::{
56
UnrecognizedAtomicOperation, UnrecognizedIntrinsicFunction,
67
WrongNumberOfGenericArgumentsToIntrinsic,
78
};
8-
use crate::require_same_types;
99

1010
use hir::def_id::DefId;
1111
use rustc_errors::{struct_span_err, DiagnosticMessage};
@@ -53,15 +53,12 @@ fn equate_intrinsic_type<'tcx>(
5353
&& gen_count_ok(own_counts.types, n_tps, "type")
5454
&& gen_count_ok(own_counts.consts, 0, "const")
5555
{
56-
let fty = Ty::new_fn_ptr(tcx, sig);
5756
let it_def_id = it.owner_id.def_id;
58-
let cause = ObligationCause::new(it.span, it_def_id, ObligationCauseCode::IntrinsicType);
59-
require_same_types(
57+
check_function_signature(
6058
tcx,
61-
&cause,
62-
ty::ParamEnv::empty(), // FIXME: do all intrinsics have an empty param env?
63-
Ty::new_fn_ptr(tcx, tcx.fn_sig(it.owner_id).instantiate_identity()),
64-
fty,
59+
ObligationCause::new(it.span, it_def_id, ObligationCauseCode::IntrinsicType),
60+
it_def_id.into(),
61+
sig,
6562
);
6663
}
6764
}

compiler/rustc_hir_analysis/src/check/mod.rs

+86-2
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,31 @@ pub mod wfcheck;
7373

7474
pub use check::check_abi;
7575

76+
use std::num::NonZeroU32;
77+
7678
use check::check_mod_item_types;
7779
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
7880
use rustc_errors::{pluralize, struct_span_err, Diagnostic, DiagnosticBuilder};
7981
use rustc_hir::def_id::{DefId, LocalDefId};
8082
use rustc_hir::intravisit::Visitor;
8183
use rustc_index::bit_set::BitSet;
84+
use rustc_infer::infer::error_reporting::ObligationCauseExt as _;
85+
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
86+
use rustc_infer::infer::{self, TyCtxtInferExt as _};
87+
use rustc_infer::traits::ObligationCause;
8288
use rustc_middle::query::Providers;
89+
use rustc_middle::ty::error::{ExpectedFound, TypeError};
8390
use rustc_middle::ty::{self, Ty, TyCtxt};
8491
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
8592
use rustc_session::parse::feature_err;
8693
use rustc_span::source_map::DUMMY_SP;
8794
use rustc_span::symbol::{kw, Ident};
88-
use rustc_span::{self, BytePos, Span, Symbol};
95+
use rustc_span::{self, def_id::CRATE_DEF_ID, BytePos, Span, Symbol};
8996
use rustc_target::abi::VariantIdx;
9097
use rustc_target::spec::abi::Abi;
9198
use rustc_trait_selection::traits::error_reporting::suggestions::ReturnsVisitor;
92-
use std::num::NonZeroU32;
99+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
100+
use rustc_trait_selection::traits::ObligationCtxt;
93101

94102
use crate::errors;
95103
use crate::require_c_abi_if_c_variadic;
@@ -546,3 +554,79 @@ fn bad_non_zero_sized_fields<'tcx>(
546554
pub fn potentially_plural_count(count: usize, word: &str) -> String {
547555
format!("{} {}{}", count, word, pluralize!(count))
548556
}
557+
558+
pub fn check_function_signature<'tcx>(
559+
tcx: TyCtxt<'tcx>,
560+
mut cause: ObligationCause<'tcx>,
561+
fn_id: DefId,
562+
expected_sig: ty::PolyFnSig<'tcx>,
563+
) {
564+
let local_id = fn_id.as_local().unwrap_or(CRATE_DEF_ID);
565+
566+
let param_env = ty::ParamEnv::empty();
567+
568+
let infcx = &tcx.infer_ctxt().build();
569+
let ocx = ObligationCtxt::new(infcx);
570+
571+
let actual_sig = tcx.fn_sig(fn_id).instantiate_identity();
572+
573+
let norm_cause = ObligationCause::misc(cause.span, local_id);
574+
let actual_sig = ocx.normalize(&norm_cause, param_env, actual_sig);
575+
576+
let expected_ty = Ty::new_fn_ptr(tcx, expected_sig);
577+
let actual_ty = Ty::new_fn_ptr(tcx, actual_sig);
578+
579+
match ocx.eq(&cause, param_env, expected_ty, actual_ty) {
580+
Ok(()) => {
581+
let errors = ocx.select_all_or_error();
582+
if !errors.is_empty() {
583+
infcx.err_ctxt().report_fulfillment_errors(&errors);
584+
return;
585+
}
586+
}
587+
Err(err) => {
588+
let err_ctxt = infcx.err_ctxt();
589+
if fn_id.is_local() {
590+
cause.span = extract_span_for_error_reporting(tcx, err, &cause, local_id);
591+
}
592+
let failure_code = cause.as_failure_code_diag(err, cause.span, vec![]);
593+
let mut diag = tcx.sess.create_err(failure_code);
594+
err_ctxt.note_type_err(
595+
&mut diag,
596+
&cause,
597+
None,
598+
Some(infer::ValuePairs::Sigs(ExpectedFound {
599+
expected: tcx.liberate_late_bound_regions(fn_id, expected_sig),
600+
found: tcx.liberate_late_bound_regions(fn_id, actual_sig),
601+
})),
602+
err,
603+
false,
604+
false,
605+
);
606+
diag.emit();
607+
return;
608+
}
609+
}
610+
611+
let outlives_env = OutlivesEnvironment::new(param_env);
612+
let _ = ocx.resolve_regions_and_report_errors(local_id, &outlives_env);
613+
614+
fn extract_span_for_error_reporting<'tcx>(
615+
tcx: TyCtxt<'tcx>,
616+
err: TypeError<'_>,
617+
cause: &ObligationCause<'tcx>,
618+
fn_id: LocalDefId,
619+
) -> rustc_span::Span {
620+
let mut args = {
621+
let node = tcx.hir().expect_owner(fn_id);
622+
let decl = node.fn_decl().unwrap_or_else(|| bug!("expected fn decl, found {:?}", node));
623+
decl.inputs.iter().map(|t| t.span).chain(std::iter::once(decl.output.span()))
624+
};
625+
626+
match err {
627+
TypeError::ArgumentMutability(i)
628+
| TypeError::ArgumentSorts(ExpectedFound { .. }, i) => args.nth(i).unwrap(),
629+
_ => cause.span(),
630+
}
631+
}
632+
}

compiler/rustc_hir_analysis/src/lib.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,14 @@ use rustc_errors::ErrorGuaranteed;
9999
use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
100100
use rustc_fluent_macro::fluent_messages;
101101
use rustc_hir as hir;
102-
use rustc_infer::infer::TyCtxtInferExt;
103102
use rustc_middle::middle;
104103
use rustc_middle::query::Providers;
105104
use rustc_middle::ty::{self, Ty, TyCtxt};
106105
use rustc_middle::util;
107106
use rustc_session::parse::feature_err;
108107
use rustc_span::{symbol::sym, Span, DUMMY_SP};
109108
use rustc_target::spec::abi::Abi;
110-
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
111-
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt};
109+
use rustc_trait_selection::traits;
112110

113111
use astconv::{AstConv, OnlySelfBounds};
114112
use bounds::Bounds;
@@ -151,28 +149,6 @@ fn require_c_abi_if_c_variadic(tcx: TyCtxt<'_>, decl: &hir::FnDecl<'_>, abi: Abi
151149
tcx.sess.emit_err(errors::VariadicFunctionCompatibleConvention { span, conventions });
152150
}
153151

154-
fn require_same_types<'tcx>(
155-
tcx: TyCtxt<'tcx>,
156-
cause: &ObligationCause<'tcx>,
157-
param_env: ty::ParamEnv<'tcx>,
158-
expected: Ty<'tcx>,
159-
actual: Ty<'tcx>,
160-
) {
161-
let infcx = &tcx.infer_ctxt().build();
162-
let ocx = ObligationCtxt::new(infcx);
163-
match ocx.eq(cause, param_env, expected, actual) {
164-
Ok(()) => {
165-
let errors = ocx.select_all_or_error();
166-
if !errors.is_empty() {
167-
infcx.err_ctxt().report_fulfillment_errors(&errors);
168-
}
169-
}
170-
Err(err) => {
171-
infcx.err_ctxt().report_mismatched_types(cause, expected, actual, err).emit();
172-
}
173-
}
174-
}
175-
176152
pub fn provide(providers: &mut Providers) {
177153
collect::provide(providers);
178154
coherence::provide(providers);

compiler/rustc_hir_typeck/messages.ftl

-10
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ hir_typeck_int_to_fat_label_nightly = consider casting this expression to `*cons
8383
8484
hir_typeck_invalid_callee = expected function, found {$ty}
8585
86-
hir_typeck_lang_start_expected_sig_note = the `start` lang item should have the signature `fn(fn() -> T, isize, *const *const u8, u8) -> isize`
87-
hir_typeck_lang_start_incorrect_number_params = incorrect number of parameters for the `start` lang item
88-
hir_typeck_lang_start_incorrect_number_params_note_expected_count = the `start` lang item should have four parameters, but found {$found_param_count}
89-
90-
hir_typeck_lang_start_incorrect_param = parameter {$param_num} of the `start` lang item is incorrect
91-
.suggestion = change the type from `{$found_ty}` to `{$expected_ty}`
92-
93-
hir_typeck_lang_start_incorrect_ret_ty = the return type of the `start` lang item is incorrect
94-
.suggestion = change the type from `{$found_ty}` to `{$expected_ty}`
95-
9686
hir_typeck_lossy_provenance_int2ptr =
9787
strict provenance disallows casting integer `{$expr_ty}` to pointer `{$cast_ty}`
9888
.suggestion = use `.with_addr()` to adjust a valid pointer in the same allocation, to this address

0 commit comments

Comments
 (0)