Skip to content

Commit 7d218fc

Browse files
committed
Auto merge of #51084 - simartin:issue_51022, r=estebank
Issue #51022: Improve E0131 message when lifetimes are involved. Fixes #51022
2 parents 3fd82a5 + 023137d commit 7d218fc

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/librustc_typeck/lib.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,18 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
188188
hir::ItemFn(.., ref generics, _) => {
189189
let mut error = false;
190190
if !generics.params.is_empty() {
191-
struct_span_err!(tcx.sess, generics.span, E0131,
192-
"`main` function is not allowed to have type parameters")
193-
.span_label(generics.span,
194-
"`main` cannot have type parameters")
191+
let param_type = if generics.is_lt_parameterized() {
192+
"lifetime"
193+
} else {
194+
"type"
195+
};
196+
let msg =
197+
format!("`main` function is not allowed to have {} parameters",
198+
param_type);
199+
let label =
200+
format!("`main` cannot have {} parameters", param_type);
201+
struct_span_err!(tcx.sess, generics.span, E0131, "{}", msg)
202+
.span_label(generics.span, label)
195203
.emit();
196204
error = true;
197205
}

src/test/compile-fail/issue-51022.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: `main` function is not allowed to have lifetime parameters
12+
fn main<'a>() { }

0 commit comments

Comments
 (0)