Skip to content

Commit e999d8b

Browse files
committed
Auto merge of rust-lang#119047 - mu001999:fix/118772, r=wesleywiser
Check generic params after sigature for main-fn-ty Fixes rust-lang#118772
2 parents 59096cd + a7d6f42 commit e999d8b

File tree

6 files changed

+69
-52
lines changed

6 files changed

+69
-52
lines changed

compiler/rustc_hir_analysis/src/check/entry.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,6 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
9292

9393
let mut error = false;
9494
let main_diagnostics_def_id = main_fn_diagnostics_def_id(tcx, main_def_id, main_span);
95-
let main_fn_generics = tcx.generics_of(main_def_id);
96-
let main_fn_predicates = tcx.predicates_of(main_def_id);
97-
if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
98-
let generics_param_span = main_fn_generics_params_span(tcx, main_def_id);
99-
tcx.sess.emit_err(errors::MainFunctionGenericParameters {
100-
span: generics_param_span.unwrap_or(main_span),
101-
label_span: generics_param_span,
102-
});
103-
error = true;
104-
} else if !main_fn_predicates.predicates.is_empty() {
105-
// generics may bring in implicit predicates, so we skip this check if generics is present.
106-
let generics_where_clauses_span = main_fn_where_clauses_span(tcx, main_def_id);
107-
tcx.sess.emit_err(errors::WhereClauseOnMain {
108-
span: generics_where_clauses_span.unwrap_or(main_span),
109-
generics_span: generics_where_clauses_span,
110-
});
111-
error = true;
112-
}
11395

11496
let main_asyncness = tcx.asyncness(main_def_id);
11597
if main_asyncness.is_async() {
@@ -142,10 +124,6 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
142124
if let Some(term_did) = tcx.lang_items().termination() {
143125
let return_ty = main_fnsig.output();
144126
let return_ty_span = main_fn_return_type_span(tcx, main_def_id).unwrap_or(main_span);
145-
if !return_ty.bound_vars().is_empty() {
146-
tcx.sess.emit_err(errors::MainFunctionReturnTypeGeneric { span: return_ty_span });
147-
error = true;
148-
}
149127
let return_ty = return_ty.skip_binder();
150128
let infcx = tcx.infer_ctxt().build();
151129
let cause = traits::ObligationCause::new(
@@ -180,7 +158,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
180158
Abi::Rust,
181159
));
182160

183-
check_function_signature(
161+
if check_function_signature(
184162
tcx,
185163
ObligationCause::new(
186164
main_span,
@@ -189,7 +167,28 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
189167
),
190168
main_def_id,
191169
expected_sig,
192-
);
170+
)
171+
.is_err()
172+
{
173+
return;
174+
}
175+
176+
let main_fn_generics = tcx.generics_of(main_def_id);
177+
let main_fn_predicates = tcx.predicates_of(main_def_id);
178+
if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
179+
let generics_param_span = main_fn_generics_params_span(tcx, main_def_id);
180+
tcx.sess.emit_err(errors::MainFunctionGenericParameters {
181+
span: generics_param_span.unwrap_or(main_span),
182+
label_span: generics_param_span,
183+
});
184+
} else if !main_fn_predicates.predicates.is_empty() {
185+
// generics may bring in implicit predicates, so we skip this check if generics is present.
186+
let generics_where_clauses_span = main_fn_where_clauses_span(tcx, main_def_id);
187+
tcx.sess.emit_err(errors::WhereClauseOnMain {
188+
span: generics_where_clauses_span.unwrap_or(main_span),
189+
generics_span: generics_where_clauses_span,
190+
});
191+
}
193192
}
194193

195194
fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
@@ -255,7 +254,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
255254
Abi::Rust,
256255
));
257256

258-
check_function_signature(
257+
let _ = check_function_signature(
259258
tcx,
260259
ObligationCause::new(
261260
start_span,

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn equate_intrinsic_type<'tcx>(
5555
&& gen_count_ok(own_counts.consts, n_cts, "const")
5656
{
5757
let it_def_id = it.owner_id.def_id;
58-
check_function_signature(
58+
let _ = check_function_signature(
5959
tcx,
6060
ObligationCause::new(it.span, it_def_id, ObligationCauseCode::IntrinsicType),
6161
it_def_id.into(),

compiler/rustc_hir_analysis/src/check/mod.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ use std::num::NonZeroU32;
7777

7878
use check::check_mod_item_types;
7979
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
80+
use rustc_errors::ErrorGuaranteed;
8081
use rustc_errors::{pluralize, struct_span_err, Diagnostic, DiagnosticBuilder};
8182
use rustc_hir::def_id::{DefId, LocalDefId};
8283
use rustc_hir::intravisit::Visitor;
@@ -570,7 +571,26 @@ pub fn check_function_signature<'tcx>(
570571
mut cause: ObligationCause<'tcx>,
571572
fn_id: DefId,
572573
expected_sig: ty::PolyFnSig<'tcx>,
573-
) {
574+
) -> Result<(), ErrorGuaranteed> {
575+
fn extract_span_for_error_reporting<'tcx>(
576+
tcx: TyCtxt<'tcx>,
577+
err: TypeError<'_>,
578+
cause: &ObligationCause<'tcx>,
579+
fn_id: LocalDefId,
580+
) -> rustc_span::Span {
581+
let mut args = {
582+
let node = tcx.hir().expect_owner(fn_id);
583+
let decl = node.fn_decl().unwrap_or_else(|| bug!("expected fn decl, found {:?}", node));
584+
decl.inputs.iter().map(|t| t.span).chain(std::iter::once(decl.output.span()))
585+
};
586+
587+
match err {
588+
TypeError::ArgumentMutability(i)
589+
| TypeError::ArgumentSorts(ExpectedFound { .. }, i) => args.nth(i).unwrap(),
590+
_ => cause.span(),
591+
}
592+
}
593+
574594
let local_id = fn_id.as_local().unwrap_or(CRATE_DEF_ID);
575595

576596
let param_env = ty::ParamEnv::empty();
@@ -587,8 +607,7 @@ pub fn check_function_signature<'tcx>(
587607
Ok(()) => {
588608
let errors = ocx.select_all_or_error();
589609
if !errors.is_empty() {
590-
infcx.err_ctxt().report_fulfillment_errors(errors);
591-
return;
610+
return Err(infcx.err_ctxt().report_fulfillment_errors(errors));
592611
}
593612
}
594613
Err(err) => {
@@ -610,30 +629,14 @@ pub fn check_function_signature<'tcx>(
610629
false,
611630
false,
612631
);
613-
diag.emit();
614-
return;
632+
return Err(diag.emit());
615633
}
616634
}
617635

618636
let outlives_env = OutlivesEnvironment::new(param_env);
619-
let _ = ocx.resolve_regions_and_report_errors(local_id, &outlives_env);
620-
621-
fn extract_span_for_error_reporting<'tcx>(
622-
tcx: TyCtxt<'tcx>,
623-
err: TypeError<'_>,
624-
cause: &ObligationCause<'tcx>,
625-
fn_id: LocalDefId,
626-
) -> rustc_span::Span {
627-
let mut args = {
628-
let node = tcx.hir().expect_owner(fn_id);
629-
let decl = node.fn_decl().unwrap_or_else(|| bug!("expected fn decl, found {:?}", node));
630-
decl.inputs.iter().map(|t| t.span).chain(std::iter::once(decl.output.span()))
631-
};
632-
633-
match err {
634-
TypeError::ArgumentMutability(i)
635-
| TypeError::ArgumentSorts(ExpectedFound { .. }, i) => args.nth(i).unwrap(),
636-
_ => cause.span(),
637-
}
637+
if let Err(e) = ocx.resolve_regions_and_report_errors(local_id, &outlives_env) {
638+
return Err(e);
638639
}
640+
641+
Ok(())
639642
}

compiler/rustc_hir_typeck/src/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>
261261
bounds,
262262
);
263263

264-
check_function_signature(
264+
let _ = check_function_signature(
265265
tcx,
266266
ObligationCause::new(
267267
tcx.def_span(fn_id),
@@ -300,7 +300,7 @@ fn check_lang_start_fn<'tcx>(tcx: TyCtxt<'tcx>, fn_sig: ty::FnSig<'tcx>, def_id:
300300
Abi::Rust,
301301
));
302302

303-
check_function_signature(
303+
let _ = check_function_signature(
304304
tcx,
305305
ObligationCause::new(
306306
tcx.def_span(def_id),

tests/ui/entry-point/issue-118772.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main(_: &i32) { //~ ERROR `main` function has wrong type
2+
println!("Hello, world!");
3+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0580]: `main` function has wrong type
2+
--> $DIR/issue-118772.rs:1:1
3+
|
4+
LL | fn main(_: &i32) {
5+
| ^^^^^^^^^^^^^^^^ incorrect number of function parameters
6+
|
7+
= note: expected signature `fn()`
8+
found signature `for<'a> fn(&'a i32)`
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0580`.

0 commit comments

Comments
 (0)