Skip to content

Commit

Permalink
Check generic params after sigature for main-fn-ty
Browse files Browse the repository at this point in the history
  • Loading branch information
mu001999 committed Dec 17, 2023
1 parent 5e70254 commit 588d37f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
39 changes: 17 additions & 22 deletions compiler/rustc_hir_analysis/src/check/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,6 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {

let mut error = false;
let main_diagnostics_def_id = main_fn_diagnostics_def_id(tcx, main_def_id, main_span);
let main_fn_generics = tcx.generics_of(main_def_id);
let main_fn_predicates = tcx.predicates_of(main_def_id);
if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
let generics_param_span = main_fn_generics_params_span(tcx, main_def_id);
tcx.sess.emit_err(errors::MainFunctionGenericParameters {
span: generics_param_span.unwrap_or(main_span),
label_span: generics_param_span,
});
error = true;
} else if !main_fn_predicates.predicates.is_empty() {
// generics may bring in implicit predicates, so we skip this check if generics is present.
let generics_where_clauses_span = main_fn_where_clauses_span(tcx, main_def_id);
tcx.sess.emit_err(errors::WhereClauseOnMain {
span: generics_where_clauses_span.unwrap_or(main_span),
generics_span: generics_where_clauses_span,
});
error = true;
}

let main_asyncness = tcx.asyncness(main_def_id);
if main_asyncness.is_async() {
Expand Down Expand Up @@ -142,10 +124,6 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
if let Some(term_did) = tcx.lang_items().termination() {
let return_ty = main_fnsig.output();
let return_ty_span = main_fn_return_type_span(tcx, main_def_id).unwrap_or(main_span);
if !return_ty.bound_vars().is_empty() {
tcx.sess.emit_err(errors::MainFunctionReturnTypeGeneric { span: return_ty_span });
error = true;
}
let return_ty = return_ty.skip_binder();
let infcx = tcx.infer_ctxt().build();
let cause = traits::ObligationCause::new(
Expand Down Expand Up @@ -190,6 +168,23 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
main_def_id,
expected_sig,
);

let main_fn_generics = tcx.generics_of(main_def_id);
let main_fn_predicates = tcx.predicates_of(main_def_id);
if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
let generics_param_span = main_fn_generics_params_span(tcx, main_def_id);
tcx.sess.emit_err(errors::MainFunctionGenericParameters {
span: generics_param_span.unwrap_or(main_span),
label_span: generics_param_span,
});
} else if !main_fn_predicates.predicates.is_empty() {
// generics may bring in implicit predicates, so we skip this check if generics is present.
let generics_where_clauses_span = main_fn_where_clauses_span(tcx, main_def_id);
tcx.sess.emit_err(errors::WhereClauseOnMain {
span: generics_where_clauses_span.unwrap_or(main_span),
generics_span: generics_where_clauses_span,
});
}
}

fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/entry-point/issue-118772.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main(_: &i32) { //~ ERROR `main` function has wrong type
//~^ ERROR `main` function is not allowed to have generic parameters
println!("Hello, world!");
}
19 changes: 19 additions & 0 deletions tests/ui/entry-point/issue-118772.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0580]: `main` function has wrong type
--> $DIR/issue-118772.rs:1:1
|
LL | fn main(_: &i32) {
| ^^^^^^^^^^^^^^^^ incorrect number of function parameters
|
= note: expected signature `fn()`
found signature `for<'a> fn(&'a i32)`

error[E0131]: `main` function is not allowed to have generic parameters
--> $DIR/issue-118772.rs:1:8
|
LL | fn main(_: &i32) {
| ^ `main` cannot have generic parameters

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0131, E0580.
For more information about an error, try `rustc --explain E0131`.

0 comments on commit 588d37f

Please sign in to comment.