From 4830325a142952b69cf31c128d755e87ad5817d8 Mon Sep 17 00:00:00 2001 From: r0cky Date: Fri, 22 Dec 2023 23:25:54 +0800 Subject: [PATCH 1/2] Emits error if has bound regions --- .../rustc_hir_analysis/src/check/entry.rs | 5 ++++- .../entry-point/return-ty-has-bound-vars.rs | 5 +++++ .../return-ty-has-bound-vars.stderr | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/ui/entry-point/return-ty-has-bound-vars.rs create mode 100644 tests/ui/entry-point/return-ty-has-bound-vars.stderr diff --git a/compiler/rustc_hir_analysis/src/check/entry.rs b/compiler/rustc_hir_analysis/src/check/entry.rs index 1d737e17e821b..a82853a13034d 100644 --- a/compiler/rustc_hir_analysis/src/check/entry.rs +++ b/compiler/rustc_hir_analysis/src/check/entry.rs @@ -124,7 +124,10 @@ 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); - let return_ty = return_ty.skip_binder(); + let Some(return_ty) = return_ty.no_bound_vars() else { + tcx.sess.emit_err(errors::MainFunctionReturnTypeGeneric { span: return_ty_span }); + return; + }; let infcx = tcx.infer_ctxt().build(); let cause = traits::ObligationCause::new( return_ty_span, diff --git a/tests/ui/entry-point/return-ty-has-bound-vars.rs b/tests/ui/entry-point/return-ty-has-bound-vars.rs new file mode 100644 index 0000000000000..2c621e7885131 --- /dev/null +++ b/tests/ui/entry-point/return-ty-has-bound-vars.rs @@ -0,0 +1,5 @@ +// issue-119209 + +type Foo<'a> = impl PartialEq; //~ERROR `impl Trait` in type aliases is unstable + +fn main<'a>(_: &'a i32) -> Foo<'a> {} //~ERROR `main` function return type is not allowed to have generic parameters diff --git a/tests/ui/entry-point/return-ty-has-bound-vars.stderr b/tests/ui/entry-point/return-ty-has-bound-vars.stderr new file mode 100644 index 0000000000000..a7aa69f9017f9 --- /dev/null +++ b/tests/ui/entry-point/return-ty-has-bound-vars.stderr @@ -0,0 +1,19 @@ +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/return-ty-has-bound-vars.rs:3:16 + | +LL | type Foo<'a> = impl PartialEq; + | ^^^^^^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error[E0131]: `main` function return type is not allowed to have generic parameters + --> $DIR/return-ty-has-bound-vars.rs:5:28 + | +LL | fn main<'a>(_: &'a i32) -> Foo<'a> {} + | ^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0131, E0658. +For more information about an error, try `rustc --explain E0131`. From d3f466a3a77d6d32e8e31ab9ccb61435938928a8 Mon Sep 17 00:00:00 2001 From: r0cky Date: Sat, 23 Dec 2023 00:09:37 +0800 Subject: [PATCH 2/2] Update test --- .../entry-point/return-ty-has-bound-vars.rs | 4 +--- .../return-ty-has-bound-vars.stderr | 20 +++++-------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/tests/ui/entry-point/return-ty-has-bound-vars.rs b/tests/ui/entry-point/return-ty-has-bound-vars.rs index 2c621e7885131..0995ce0616092 100644 --- a/tests/ui/entry-point/return-ty-has-bound-vars.rs +++ b/tests/ui/entry-point/return-ty-has-bound-vars.rs @@ -1,5 +1,3 @@ // issue-119209 -type Foo<'a> = impl PartialEq; //~ERROR `impl Trait` in type aliases is unstable - -fn main<'a>(_: &'a i32) -> Foo<'a> {} //~ERROR `main` function return type is not allowed to have generic parameters +fn main<'a>(_: &'a i32) -> &'a () { &() } //~ERROR `main` function return type is not allowed to have generic parameters diff --git a/tests/ui/entry-point/return-ty-has-bound-vars.stderr b/tests/ui/entry-point/return-ty-has-bound-vars.stderr index a7aa69f9017f9..e7aab839f3171 100644 --- a/tests/ui/entry-point/return-ty-has-bound-vars.stderr +++ b/tests/ui/entry-point/return-ty-has-bound-vars.stderr @@ -1,19 +1,9 @@ -error[E0658]: `impl Trait` in type aliases is unstable - --> $DIR/return-ty-has-bound-vars.rs:3:16 - | -LL | type Foo<'a> = impl PartialEq; - | ^^^^^^^^^^^^^^ - | - = note: see issue #63063 for more information - = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable - error[E0131]: `main` function return type is not allowed to have generic parameters - --> $DIR/return-ty-has-bound-vars.rs:5:28 + --> $DIR/return-ty-has-bound-vars.rs:3:28 | -LL | fn main<'a>(_: &'a i32) -> Foo<'a> {} - | ^^^^^^^ +LL | fn main<'a>(_: &'a i32) -> &'a () { &() } + | ^^^^^^ -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0131, E0658. -For more information about an error, try `rustc --explain E0131`. +For more information about this error, try `rustc --explain E0131`.