From d6227dcd6e139c3f598cd6e937cc59214e254ca6 Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Sat, 18 Apr 2015 08:25:43 -0400 Subject: [PATCH 1/4] Added error explanations for E0308, E0309, and E0310 --- src/librustc/diagnostics.rs | 56 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index e1eb8d7418695..2d1bf16727617 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -303,10 +303,63 @@ number cannot be negative. E0307: r##" The length of an array is part of its type. For this reason, this length must be a compile-time constant. +"##, + +E0308: r##" +This error occurs when the compiler was unable to infer the concrete type of a +variable. This error can occur for several cases, the most common of which is +that there is a mismatch in the expected type that the compiler inferred, and +the actual type that the user defined a variable as. + +let a: char = 7; // An integral type can't contained in a character, so + // there is a mismatch. + +let b: u32 = 7; // Either use the right type... +let c = 7; // ...or let the compiler infer it. + +let d: char = c; // This also causes a mismatch because c is some sort + // of number whereas d is definitely a character. +"##, + +E0309: r##" +Types in type definitions have lifetimes associated with them that represent +how long the data stored within them is guaranteed to be live. This lifetime +must be as long as the data needs to be alive, and missing the constraint that +denotes this will cause this error. + +// This won't compile because T is not constrained, meaning the data +// stored in it is not guaranteed to last as long as the reference +struct Foo<'a, T> { + foo: &'a T +} + +// This will compile, because it has the constraint on the type parameter +struct Foo<'a, T: 'a> { + foo: &'a T +} +"##, + +E0310: r##" +Types in type definitions have lifetimes associated with them that represent +how long the data stored within them is guaranteed to be live. This lifetime +must be as long as the data needs to be alive, and missing the constraint that +denotes this will cause this error. + +// This won't compile because T is not constrained to the static lifetime +// the reference needs +struct Foo { + foo: &'static T +} + +// This will compile, because it has the constraint on the type parameter +struct Foo { + foo: &'static T +} "## } + register_diagnostics! { E0009, E0010, @@ -363,9 +416,6 @@ register_diagnostics! { E0300, // unexpanded macro E0304, // expected signed integer constant E0305, // expected constant - E0308, - E0309, // thing may not live long enough - E0310, // thing may not live long enough E0311, // thing may not live long enough E0312, // lifetime of reference outlives lifetime of borrowed content E0313, // lifetime of borrowed pointer outlives lifetime of captured variable From 99d2552c4ef409dc7232a47fbd92bb9df8c98cdc Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Sat, 18 Apr 2015 20:25:24 -0400 Subject: [PATCH 2/4] Fix typo --- src/librustc/diagnostics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 2d1bf16727617..12b65b7feb550 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -311,7 +311,7 @@ variable. This error can occur for several cases, the most common of which is that there is a mismatch in the expected type that the compiler inferred, and the actual type that the user defined a variable as. -let a: char = 7; // An integral type can't contained in a character, so +let a: char = 7; // An integral type can't be contained in a character, so // there is a mismatch. let b: u32 = 7; // Either use the right type... From 4420f318080e116dc4bf75a1a3568fe8bf4157b7 Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Sun, 19 Apr 2015 01:59:29 -0400 Subject: [PATCH 3/4] Fix trailing whitespaces --- src/librustc/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 12b65b7feb550..68dd83a3fe5f2 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -303,7 +303,7 @@ number cannot be negative. E0307: r##" The length of an array is part of its type. For this reason, this length must be a compile-time constant. -"##, +"##, E0308: r##" This error occurs when the compiler was unable to infer the concrete type of a @@ -311,7 +311,7 @@ variable. This error can occur for several cases, the most common of which is that there is a mismatch in the expected type that the compiler inferred, and the actual type that the user defined a variable as. -let a: char = 7; // An integral type can't be contained in a character, so +let a: char = 7; // An integral type can't be contained in a character, so // there is a mismatch. let b: u32 = 7; // Either use the right type... From 4174aa40f40e05195d73d3eb09b12d9359444f5d Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Wed, 6 May 2015 05:43:05 -0400 Subject: [PATCH 4/4] Reword with pnkfelix's suggestion --- src/librustc/diagnostics.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 68dd83a3fe5f2..b6828f1846d85 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -307,18 +307,20 @@ a compile-time constant. E0308: r##" This error occurs when the compiler was unable to infer the concrete type of a -variable. This error can occur for several cases, the most common of which is -that there is a mismatch in the expected type that the compiler inferred, and -the actual type that the user defined a variable as. - -let a: char = 7; // An integral type can't be contained in a character, so - // there is a mismatch. - -let b: u32 = 7; // Either use the right type... -let c = 7; // ...or let the compiler infer it. - -let d: char = c; // This also causes a mismatch because c is some sort - // of number whereas d is definitely a character. +variable. This error can occur for several cases, the most common of which is a +mismatch in the expected type that the compiler inferred for a variable's +initializing expression, and the actual type explicitly assigned to the +variable. + +For example: + +let x: i32 = "I am not a number!"; +// ~~~ ~~~~~~~~~~~~~~~~~~~~ +// | | +// | initializing expression; +// | compiler infers type `&str` +// | +// type `i32` assigned to variable `x` "##, E0309: r##"