From a4444aa78079665658b0d656c6d243d88a9ac5cc Mon Sep 17 00:00:00 2001 From: Nick Hamann Date: Mon, 11 May 2015 23:36:54 -0500 Subject: [PATCH 1/4] Add error explanations for E0066 and E0069. This also updates the error messages for both. For E0066, it removes mention of "managed heap", which was removed in 8a91d33. For E0069, I just tweaked the wording to make it a bit more explicit. --- src/librustc_typeck/check/mod.rs | 9 +++++---- src/librustc_typeck/diagnostics.rs | 27 +++++++++++++++++++++++++-- src/test/compile-fail/issue-14084.rs | 2 +- src/test/compile-fail/ret-non-nil.rs | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 554f3d4b5a0c7..3cdbaec15284b 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3082,8 +3082,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, let mut checked = false; opt_place.as_ref().map(|place| match place.node { ast::ExprPath(None, ref path) => { - // FIXME(pcwalton): For now we hardcode the two permissible - // places: the exchange heap and the managed heap. + // FIXME(pcwalton): For now we hardcode the only permissible + // place: the exchange heap. let definition = lookup_full_def(tcx, path.span, place.id); let def_id = definition.def_id(); let referent_ty = fcx.expr_ty(&**subexpr); @@ -3097,7 +3097,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, if !checked { span_err!(tcx.sess, expr.span, E0066, - "only the managed heap and exchange heap are currently supported"); + "only the exchange heap is currently supported"); fcx.write_ty(id, tcx.types.err); } } @@ -3317,7 +3317,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>, if let Err(_) = fcx.mk_eqty(false, infer::Misc(expr.span), result_type, ty::mk_nil(fcx.tcx())) { span_err!(tcx.sess, expr.span, E0069, - "`return;` in function returning non-nil"); + "`return;` in a function whose return type is \ + not `()`"); }, Some(ref e) => { check_expr_coercable_to_type(fcx, &**e, result_type); diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index a11a4edbd316b..1613122ecced2 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -91,6 +91,16 @@ enum variant, one of the fields was not provided. Each field should be specified exactly once. "##, +E0066: r##" +Box placement expressions (like C++'s "placement new") do not support any +place expression except the exchange heap (i.e. `std::boxed::HEAP`). +Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC +470][rfc470] and [RFC 809][rfc809] for more details. + +[rfc470]: https://github.com/rust-lang/rfcs/pull/470 +[rfc809]: https://github.com/rust-lang/rfcs/pull/809 +"##, + E0067: r##" The left-hand side of an assignment operator must be an lvalue expression. An lvalue expression represents a memory location and includes item paths (ie, @@ -108,6 +118,21 @@ LinkedList::new() += 1; ``` "##, +E0069: r##" +The compiler found a function whose body contains a `return;` statement but +whose return type is not `()`. An example of this is: + +``` +// error +fn foo() -> u8 { + return; +} +``` + +Since `return;` is just like `return ();`, there is a mismatch between the +function's return type and the value being returned. +"##, + E0081: r##" Enum discriminants are used to differentiate enum variants stored in memory. This error indicates that the same value was used for two or more variants, @@ -484,9 +509,7 @@ register_diagnostics! { E0059, E0060, E0061, - E0066, E0068, - E0069, E0070, E0071, E0072, diff --git a/src/test/compile-fail/issue-14084.rs b/src/test/compile-fail/issue-14084.rs index 92e0dd3ad0e52..003c6644f7f02 100644 --- a/src/test/compile-fail/issue-14084.rs +++ b/src/test/compile-fail/issue-14084.rs @@ -12,5 +12,5 @@ fn main() { box ( () ) 0; - //~^ ERROR: only the managed heap and exchange heap are currently supported + //~^ ERROR: only the exchange heap is currently supported } diff --git a/src/test/compile-fail/ret-non-nil.rs b/src/test/compile-fail/ret-non-nil.rs index 4ee3cf4abac4e..6be98fbd82773 100644 --- a/src/test/compile-fail/ret-non-nil.rs +++ b/src/test/compile-fail/ret-non-nil.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// error-pattern: `return;` in function returning non-nil +// error-pattern: `return;` in a function whose return type is not `()` fn f() { return; } From 06aef339f11091f178957350b5159081e7b4534a Mon Sep 17 00:00:00 2001 From: Nick Hamann Date: Wed, 13 May 2015 22:32:52 -0500 Subject: [PATCH 2/4] Add error explanations for E0053, E0251, E0252, E0255, E0256, E0368. --- src/librustc_resolve/diagnostics.rs | 90 +++++++++++++++++++++++++++-- src/librustc_typeck/diagnostics.rs | 82 +++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 8 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 1e0b627a8e031..cd941c65b9da3 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -49,6 +49,88 @@ about what constitutes an Item declaration and what does not: http://doc.rust-lang.org/reference.html#statements "##, +E0251: r##" +Two items of the same name cannot be imported without rebinding one of the +items under a new local name. + +An example of this error: + +``` +use foo::baz; +use bar::*; // error, do `use foo::baz as quux` instead on the previous line + +fn main() {} + +mod foo { + pub struct baz; +} + +mod bar { + pub mod baz {} +} +``` +"##, + +E0252: r##" +Two items of the same name cannot be imported without rebinding one of the +items under a new local name. + +An example of this error: + +``` +use foo::baz; +use bar::baz; // error, do `use bar::baz as quux` instead + +fn main() {} + +mod foo { + pub struct baz; +} + +mod bar { + pub mod baz {} +} +``` +"##, + +E0255: r##" +You can't import a value whose name is the same as another value defined in the +module. + +An example of this error: + +``` +use foo::FOO; // error, do `use foo::FOO as BAR` instead + +fn FOO() {} + +mod foo { + pub const FOO: bool = true; +} + +fn main() {} +``` +"##, + +E0256: r##" +You can't import a type or module when the name of the item being imported is +the same as another type or submodule defined in the module. + +An example of this error: + +``` +use foo::Bar; // error + +struct Bar; + +mod foo { + pub mod Bar { } +} + +fn main() {} +``` +"##, + E0259: r##" The name chosen for an external crate conflicts with another external crate that has been imported into the current module. @@ -122,14 +204,10 @@ http://doc.rust-lang.org/reference.html#types register_diagnostics! { E0157, E0153, - E0251, // a named type or value has already been imported in this module - E0252, // a named type or value has already been imported in this module E0253, // not directly importable E0254, // import conflicts with imported crate in this module - E0255, // import conflicts with value in this module - E0256, // import conflicts with type in this module - E0257, // inherent implementations are only allowed on types defined in the current module - E0258, // import conflicts with existing submodule + E0257, + E0258, E0364, // item is private E0365 // item is private } diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 1613122ecced2..d19bb859c7750 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -64,6 +64,44 @@ impl Foo for Bar { ``` "##, +E0053: r##" +In a trait method implementation, the function signature must match exactly. +This error indicates a mutability mismatch between a trait method signature +and the signature of the implementation. + +Here's an example where the mutability of the `self` parameter is wrong: + +``` +trait Foo { fn foo(&self); } + +struct Bar; + +impl Foo for Bar { + // error, the signature should be `fn foo(&self)` instead + fn foo(&mut self) { } +} + +fn main() {} +``` + +Here's another example, this time for a non-`self` parameter: + +``` +trait Foo { fn foo(x: &mut bool) -> bool; } + +struct Bar; + +impl Foo for Bar { + // error, the type of `x` should be `&mut bool` instead + fn foo(x: &bool) -> bool { *x } +} + +fn main() {} +``` + + +"##, + E0054: r##" It is not allowed to cast to a bool. If you are trying to cast a numeric type to a bool, you can compare it with zero instead: @@ -483,6 +521,48 @@ The `Sized` trait is a special trait built-in to the compiler for types with a constant size known at compile-time. This trait is automatically implemented for types as needed by the compiler, and it is currently disallowed to explicitly implement it for a type. +"##, + +E0368: r##" +This error indicates that a binary assignment operator like `+=` or `^=` was +applied to the wrong types. + +A couple examples of this are as follows: + +``` +let mut x: u16 = 5; +x ^= true; // error, `^=` cannot be applied to types `u16` and `bool` +x += (); // error, `+=` cannot be applied to types `u16` and `()` +``` + +Another problem you might be facing is this: suppose you've overloaded the `+` +operator for some type `Foo` by implementing the `std::ops::Add` trait for +`Foo`, but you find that using `+=` does not work, as in this example: + +``` +use std::ops::Add; + +struct Foo(u32); + +impl Add for Foo { + type Output = Foo; + + fn add(self, rhs: Foo) -> Foo { + Foo(self.0 + rhs.0) + } +} + +fn main() { + let mut x: Foo = Foo(5); + x += Foo(7); // error, `+= cannot be applied to types `Foo` and `Foo` +} +``` + +This is because the binary assignment operators currently do not work off of +traits, so it is not possible to overload them. See [RFC 953] for a proposal +to change this. + +[RFC 953]: https://github.com/rust-lang/rfcs/pull/953 "## } @@ -503,7 +583,6 @@ register_diagnostics! { E0040, // explicit use of destructor method E0044, // foreign items may not have type parameters E0045, // variadic function must have C calling convention - E0053, E0055, // method has an incompatible type for trait E0057, // method has an incompatible type for trait E0059, @@ -629,7 +708,6 @@ register_diagnostics! { E0328, // cannot implement Unsize explicitly E0366, // dropck forbid specialization to concrete type or region E0367, // dropck forbid specialization to predicate not in struct/enum - E0368, // binary operation `=` cannot be applied to types E0369, // binary operation `` cannot be applied to types E0371, // impl Trait for Trait is illegal E0372, // impl Trait for Trait where Trait is not object safe From d636b5cf659771d74a5e225b751898f69a35ae9a Mon Sep 17 00:00:00 2001 From: Nick Hamann Date: Thu, 14 May 2015 02:50:39 -0500 Subject: [PATCH 3/4] Improve examples in the E0255/E0256 error explanations. --- src/librustc_resolve/diagnostics.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index cd941c65b9da3..15ddcbc80749c 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -100,12 +100,12 @@ module. An example of this error: ``` -use foo::FOO; // error, do `use foo::FOO as BAR` instead +use bar::foo; // error, do `use bar::foo as baz` instead -fn FOO() {} +fn foo() {} -mod foo { - pub const FOO: bool = true; +mod bar { + pub fn foo() {} } fn main() {} @@ -121,7 +121,7 @@ An example of this error: ``` use foo::Bar; // error -struct Bar; +type Bar = u32; mod foo { pub mod Bar { } From 588777f5471db2d03b5a376efbc21c1dc10fc364 Mon Sep 17 00:00:00 2001 From: Nick Hamann Date: Thu, 14 May 2015 03:22:55 -0500 Subject: [PATCH 4/4] Improve E0053 and E0066 error explanations. --- src/librustc_typeck/diagnostics.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index d19bb859c7750..2e5b389c2850e 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -65,9 +65,8 @@ impl Foo for Bar { "##, E0053: r##" -In a trait method implementation, the function signature must match exactly. -This error indicates a mutability mismatch between a trait method signature -and the signature of the implementation. +For any given method of a trait, the mutabilities of the parameters must match +between the trait definition and the implementation. Here's an example where the mutability of the `self` parameter is wrong: @@ -130,13 +129,13 @@ exactly once. "##, E0066: r##" -Box placement expressions (like C++'s "placement new") do not support any +Box placement expressions (like C++'s "placement new") do not yet support any place expression except the exchange heap (i.e. `std::boxed::HEAP`). -Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC -470][rfc470] and [RFC 809][rfc809] for more details. +Furthermore, the syntax is changing to use `in` instead of `box`. See [RFC 470] +and [RFC 809] for more details. -[rfc470]: https://github.com/rust-lang/rfcs/pull/470 -[rfc809]: https://github.com/rust-lang/rfcs/pull/809 +[RFC 470]: https://github.com/rust-lang/rfcs/pull/470 +[RFC 809]: https://github.com/rust-lang/rfcs/pull/809 "##, E0067: r##"