Skip to content

Commit fe85342

Browse files
committed
Auto merge of #25688 - nham:E0055_E0192, r=alexcrichton
- Adds explanations for E0055, E0089, E0192, E0261-E0263, E0318 - Improves explanations for E0250, E0368, E0372. - Converts 15 diagnostics to have error codes (E0380-E0394). Adds an explanation for E0380. - The E0087-E0090 messages currently look like "expected {} parameter(s) found {} parameter(s)". This changes them to either use "parameter" or "parameters", based on the number. This is, in part, more progress towards #24407
2 parents cccc137 + 570a043 commit fe85342

File tree

13 files changed

+240
-95
lines changed

13 files changed

+240
-95
lines changed

src/librustc/diagnostics.rs

+51-4
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,54 @@ enum Method { GET, POST }
396396
```
397397
"##,
398398

399+
E0261: r##"
400+
When using a lifetime like `'a` in a type, it must be declared before being
401+
used.
402+
403+
These two examples illustrate the problem:
404+
405+
```
406+
// error, use of undeclared lifetime name `'a`
407+
fn foo(x: &'a str) { }
408+
409+
struct Foo {
410+
// error, use of undeclared lifetime name `'a`
411+
x: &'a str,
412+
}
413+
```
414+
415+
These can be fixed by declaring lifetime parameters:
416+
417+
```
418+
fn foo<'a>(x: &'a str) { }
419+
420+
struct Foo<'a> {
421+
x: &'a str,
422+
}
423+
```
424+
"##,
425+
426+
E0262: r##"
427+
Declaring certain lifetime names in parameters is disallowed. For example,
428+
because the `'static` lifetime is a special built-in lifetime name denoting
429+
the lifetime of the entire program, this is an error:
430+
431+
```
432+
// error, illegal lifetime parameter name `'static`
433+
fn foo<'static>(x: &'static str) { }
434+
```
435+
"##,
436+
437+
E0263: r##"
438+
A lifetime name cannot be declared more than once in the same scope. For
439+
example:
440+
441+
```
442+
// error, lifetime name `'a` declared twice in the same scope
443+
fn foo<'a, 'b, 'a>(x: &'a str, y: &'b str) { }
444+
```
445+
"##,
446+
399447
E0265: r##"
400448
This error indicates that a static or constant references itself.
401449
All statics and constants need to resolve to a value in an acyclic manner.
@@ -814,9 +862,6 @@ register_diagnostics! {
814862
E0136,
815863
E0138,
816864
E0139,
817-
E0261, // use of undeclared lifetime name
818-
E0262, // illegal lifetime parameter name
819-
E0263, // lifetime name declared twice in same scope
820865
E0264, // unknown external lang item
821866
E0266, // expected item
822867
E0269, // not all control paths return a value
@@ -846,5 +891,7 @@ register_diagnostics! {
846891
E0315, // cannot invoke closure outside of its lifetime
847892
E0316, // nested quantification of lifetimes
848893
E0370, // discriminant overflow
849-
E0378 // method calls limited to constant inherent methods
894+
E0378, // method calls limited to constant inherent methods
895+
E0394 // cannot refer to other statics by value, use the address-of
896+
// operator or a constant instead
850897
}

src/librustc/middle/check_const.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,9 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'tcx> {
762762
// statics cannot be consumed by value at any time, that would imply
763763
// that they're an initializer (what a const is for) or kept in sync
764764
// over time (not feasible), so deny it outright.
765-
self.tcx.sess.span_err(consume_span,
766-
"cannot refer to other statics by value, use the \
767-
address-of operator or a constant instead");
765+
span_err!(self.tcx.sess, consume_span, E0394,
766+
"cannot refer to other statics by value, use the \
767+
address-of operator or a constant instead");
768768
}
769769
break;
770770
}

src/librustc_borrowck/borrowck/mod.rs

+33-38
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
603603

604604
let (ol, moved_lp_msg) = match the_move.kind {
605605
move_data::Declared => {
606-
self.tcx.sess.span_err(
607-
use_span,
608-
&format!("{} of possibly uninitialized variable: `{}`",
609-
verb,
610-
self.loan_path_to_string(lp)));
606+
span_err!(
607+
self.tcx.sess, use_span, E0381,
608+
"{} of possibly uninitialized variable: `{}`",
609+
verb,
610+
self.loan_path_to_string(lp));
611+
611612
(self.loan_path_to_string(moved_lp),
612613
String::new())
613614
}
@@ -644,12 +645,10 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
644645
let msg = if !has_fork && partial { "partially " }
645646
else if has_fork && !has_common { "collaterally "}
646647
else { "" };
647-
self.tcx.sess.span_err(
648-
use_span,
649-
&format!("{} of {}moved value: `{}`",
650-
verb,
651-
msg,
652-
nl));
648+
span_err!(
649+
self.tcx.sess, use_span, E0382,
650+
"{} of {}moved value: `{}`",
651+
verb, msg, nl);
653652
(ol, moved_lp_msg)
654653
}
655654
};
@@ -762,23 +761,21 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
762761
&self,
763762
span: Span,
764763
lp: &LoanPath<'tcx>) {
765-
self.tcx
766-
.sess
767-
.span_err(span,
768-
&format!("partial reinitialization of uninitialized \
769-
structure `{}`",
770-
self.loan_path_to_string(lp)));
764+
span_err!(
765+
self.tcx.sess, span, E0383,
766+
"partial reinitialization of uninitialized structure `{}`",
767+
self.loan_path_to_string(lp));
771768
}
772769

773770
pub fn report_reassigned_immutable_variable(&self,
774771
span: Span,
775772
lp: &LoanPath<'tcx>,
776773
assign:
777774
&move_data::Assignment) {
778-
self.tcx.sess.span_err(
779-
span,
780-
&format!("re-assignment of immutable variable `{}`",
781-
self.loan_path_to_string(lp)));
775+
span_err!(
776+
self.tcx.sess, span, E0384,
777+
"re-assignment of immutable variable `{}`",
778+
self.loan_path_to_string(lp));
782779
self.tcx.sess.span_note(assign.span, "prior assignment occurs here");
783780
}
784781

@@ -896,21 +893,19 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
896893

897894
match cause {
898895
mc::AliasableOther => {
899-
self.tcx.sess.span_err(
900-
span,
901-
&format!("{} in an aliasable location",
902-
prefix));
896+
span_err!(
897+
self.tcx.sess, span, E0385,
898+
"{} in an aliasable location", prefix);
903899
}
904900
mc::AliasableReason::UnaliasableImmutable => {
905-
self.tcx.sess.span_err(
906-
span,
907-
&format!("{} in an immutable container",
908-
prefix));
901+
span_err!(
902+
self.tcx.sess, span, E0386,
903+
"{} in an immutable container", prefix);
909904
}
910905
mc::AliasableClosure(id) => {
911-
self.tcx.sess.span_err(span,
912-
&format!("{} in a captured outer \
913-
variable in an `Fn` closure", prefix));
906+
span_err!(
907+
self.tcx.sess, span, E0387,
908+
"{} in a captured outer variable in an `Fn` closure", prefix);
914909
if let BorrowViolation(euv::ClosureCapture(_)) = kind {
915910
// The aliasability violation with closure captures can
916911
// happen for nested closures, so we know the enclosing
@@ -925,14 +920,14 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
925920
}
926921
mc::AliasableStatic(..) |
927922
mc::AliasableStaticMut(..) => {
928-
self.tcx.sess.span_err(
929-
span,
930-
&format!("{} in a static location", prefix));
923+
span_err!(
924+
self.tcx.sess, span, E0388,
925+
"{} in a static location", prefix);
931926
}
932927
mc::AliasableBorrowed => {
933-
self.tcx.sess.span_err(
934-
span,
935-
&format!("{} in a `&` reference", prefix));
928+
span_err!(
929+
self.tcx.sess, span, E0389,
930+
"{} in a `&` reference", prefix);
936931
}
937932
}
938933

src/librustc_borrowck/diagnostics.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@
1010

1111
#![allow(non_snake_case)]
1212

13+
register_long_diagnostics! {
14+
15+
E0381: r##"
16+
It is not allowed to use or capture an uninitialized variable. For example:
17+
18+
```
19+
fn main() {
20+
let x: i32;
21+
let y = x; // error, use of possibly uninitialized variable
22+
```
23+
24+
To fix this, ensure that any declared variables are initialized before being
25+
used.
26+
"##
27+
28+
}
29+
1330
register_diagnostics! {
14-
E0373 // closure may outlive current fn, but it borrows {}, which is owned by current fn
31+
E0373, // closure may outlive current fn, but it borrows {}, which is owned by current fn
32+
E0382, // use of partially/collaterally moved value
33+
E0383, // partial reinitialization of uninitialized structure
34+
E0384, // reassignment of immutable variable
35+
E0385, // {} in an aliasable location
36+
E0386, // {} in an immutable container
37+
E0387, // {} in a captured outer variable in an `Fn` closure
38+
E0388, // {} in a static location
39+
E0389 // {} in a `&` reference
1540
}

src/librustc_typeck/astconv.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,12 @@ fn create_substs_for_ast_path<'tcx>(
437437
// defaults. This will lead to an ICE if we are not
438438
// careful!
439439
if self_ty.is_none() && ty::type_has_self(default) {
440-
tcx.sess.span_err(
441-
span,
442-
&format!("the type parameter `{}` must be explicitly specified \
443-
in an object type because its default value `{}` references \
444-
the type `Self`",
445-
param.name.user_string(tcx),
446-
default.user_string(tcx)));
440+
span_err!(tcx.sess, span, E0393,
441+
"the type parameter `{}` must be explicitly specified \
442+
in an object type because its default value `{}` references \
443+
the type `Self`",
444+
param.name.user_string(tcx),
445+
default.user_string(tcx));
447446
substs.types.push(TypeSpace, tcx.types.err);
448447
} else {
449448
// This is a default type parameter.

src/librustc_typeck/check/mod.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -4688,9 +4688,12 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
46884688
} else if i == type_count {
46894689
span_err!(fcx.tcx().sess, typ.span, E0087,
46904690
"too many type parameters provided: \
4691-
expected at most {} parameter(s), \
4692-
found {} parameter(s)",
4693-
type_count, data.types.len());
4691+
expected at most {} parameter{}, \
4692+
found {} parameter{}",
4693+
type_count,
4694+
if type_count == 1 {""} else {"s"},
4695+
data.types.len(),
4696+
if data.types.len() == 1 {""} else {"s"});
46944697
substs.types.truncate(space, 0);
46954698
break;
46964699
}
@@ -4713,9 +4716,11 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
47134716
} else if i == region_count {
47144717
span_err!(fcx.tcx().sess, lifetime.span, E0088,
47154718
"too many lifetime parameters provided: \
4716-
expected {} parameter(s), found {} parameter(s)",
4719+
expected {} parameter{}, found {} parameter{}",
47174720
region_count,
4718-
data.lifetimes.len());
4721+
if region_count == 1 {""} else {"s"},
4722+
data.lifetimes.len(),
4723+
if data.lifetimes.len() == 1 {""} else {"s"});
47194724
substs.mut_regions().truncate(space, 0);
47204725
break;
47214726
}
@@ -4805,9 +4810,12 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
48054810
let qualifier =
48064811
if desired.len() != required_len { "at least " } else { "" };
48074812
span_err!(fcx.tcx().sess, span, E0089,
4808-
"too few type parameters provided: expected {}{} parameter(s) \
4809-
, found {} parameter(s)",
4810-
qualifier, required_len, provided_len);
4813+
"too few type parameters provided: expected {}{} parameter{}, \
4814+
found {} parameter{}",
4815+
qualifier, required_len,
4816+
if required_len == 1 {""} else {"s"},
4817+
provided_len,
4818+
if provided_len == 1 {""} else {"s"});
48114819
substs.types.replace(space, repeat(fcx.tcx().types.err).take(desired.len()).collect());
48124820
return;
48134821
}
@@ -4858,9 +4866,12 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
48584866
// Otherwise, too few were provided. Report an error and then
48594867
// use inference variables.
48604868
span_err!(fcx.tcx().sess, span, E0090,
4861-
"too few lifetime parameters provided: expected {} parameter(s), \
4862-
found {} parameter(s)",
4863-
desired.len(), provided_len);
4869+
"too few lifetime parameters provided: expected {} parameter{}, \
4870+
found {} parameter{}",
4871+
desired.len(),
4872+
if desired.len() == 1 {""} else {"s"},
4873+
provided_len,
4874+
if provided_len == 1 {""} else {"s"});
48644875

48654876
substs.mut_regions().replace(
48664877
space,

src/librustc_typeck/check/wf.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
124124
reject_non_type_param_bounds(ccx.tcx, item.span, &trait_predicates);
125125
if ty::trait_has_default_impl(ccx.tcx, local_def(item.id)) {
126126
if !items.is_empty() {
127-
ccx.tcx.sess.span_err(
128-
item.span,
129-
"traits with default impls (`e.g. unsafe impl Trait for ..`) must \
130-
have no methods or associated items")
127+
span_err!(ccx.tcx.sess, item.span, E0380,
128+
"traits with default impls (`e.g. unsafe impl \
129+
Trait for ..`) must have no methods or associated items")
131130
}
132131
}
133132
}
@@ -353,10 +352,8 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
353352
span: Span,
354353
param_name: ast::Name)
355354
{
356-
self.tcx().sess.span_err(
357-
span,
358-
&format!("parameter `{}` is never used",
359-
param_name.user_string(self.tcx())));
355+
span_err!(self.tcx().sess, span, E0392,
356+
"parameter `{}` is never used", param_name.user_string(self.tcx()));
360357

361358
let suggested_marker_id = self.tcx().lang_items.phantom_data();
362359
match suggested_marker_id {

src/librustc_typeck/coherence/orphan.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
4848
match lang_def_id {
4949
Some(lang_def_id) if lang_def_id == impl_def_id => { /* OK */ },
5050
_ => {
51-
self.tcx.sess.span_err(
52-
span,
53-
&format!("only a single inherent implementation marked with `#[lang = \"{}\"]` \
54-
is allowed for the `{}` primitive", lang, ty));
51+
span_err!(self.tcx.sess, span, E0390,
52+
"only a single inherent implementation marked with `#[lang = \"{}\"]` \
53+
is allowed for the `{}` primitive", lang, ty);
5554
}
5655
}
5756
}

src/librustc_typeck/collect.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,8 @@ impl<'a,'tcx> CrateCtxt<'a,'tcx> {
236236
assert!(!cycle.is_empty());
237237
let tcx = self.tcx;
238238

239-
tcx.sess.span_err(
240-
span,
241-
&format!("unsupported cyclic reference between types/traits detected"));
239+
span_err!(tcx.sess, span, E0391,
240+
"unsupported cyclic reference between types/traits detected");
242241

243242
match cycle[0] {
244243
AstConvRequest::GetItemTypeScheme(def_id) |

0 commit comments

Comments
 (0)