diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index a1fa1834ef4..a3577981c1e 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -196,9 +196,13 @@ struct X { x: (), } "##, E0015: r##" -The only function calls allowed in static or constant expressions are enum -variant constructors or struct constructors (for unit or tuple structs). This -is because Rust currently does not support compile-time function execution. +The only functions that can be called in static or constant expressions are +`const` functions. Rust currently does not support more general compile-time +function execution. + +See [RFC 911] for more details on the design of `const fn`s. + +[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md "##, E0018: r##" @@ -842,6 +846,53 @@ struct Foo { foo: &'static T } ``` +"##, + +E0378: r##" +Method calls that aren't calls to inherent `const` methods are disallowed +in statics, constants, and constant functions. + +For example: + +``` +const BAZ: i32 = Foo(25).bar(); // error, `bar` isn't `const` + +struct Foo(i32); + +impl Foo { + const fn foo(&self) -> i32 { + self.bar() // error, `bar` isn't `const` + } + + fn bar(&self) -> i32 { self.0 } +} +``` + +For more information about `const fn`'s, see [RFC 911]. + +[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md +"##, + +E0394: r##" +From [RFC 246]: + + > It is illegal for a static to reference another static by value. It is + > required that all references be borrowed. + +[RFC 246]: https://github.com/rust-lang/rfcs/pull/246 +"##, + +E0397: r##" +It is not allowed for a mutable static to allocate or have destructors. For +example: + +``` +// error: mutable statics are not allowed to have boxes +static mut FOO: Option> = None; + +// error: mutable statics are not allowed to have destructors +static mut BAR: Option> = None; +``` "## } @@ -891,9 +942,6 @@ struct Foo { E0315, // cannot invoke closure outside of its lifetime E0316, // nested quantification of lifetimes E0370, // discriminant overflow - E0378, // method calls limited to constant inherent methods - E0394, // cannot refer to other statics by value, use the address-of - // operator or a constant instead E0395, // pointer comparison in const-expr E0396 // pointer dereference in const-expr } diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs index ff7c570284f..7403462df8a 100644 --- a/src/librustc/middle/check_const.rs +++ b/src/librustc/middle/check_const.rs @@ -273,13 +273,13 @@ fn check_static_mut_type(&self, e: &ast::Expr) { let suffix = if tcontents.has_dtor() { "destructors" } else if tcontents.owns_owned() { - "owned pointers" + "boxes" } else { return }; - self.tcx.sess.span_err(e.span, &format!("mutable statics are not allowed \ - to have {}", suffix)); + span_err!(self.tcx.sess, e.span, E0397, + "mutable statics are not allowed to have {}", suffix); } fn check_static_type(&self, e: &ast::Expr) { diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index edfad77d588..c822a7faa19 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -170,6 +170,31 @@ struct Dog { http://doc.rust-lang.org/reference.html#trait-objects "##, +E0040: r##" +It is not allowed to manually call destructors in Rust. It is also not +necessary to do this since `drop` is called automatically whenever a value goes +out of scope. + +Here's an example of this error: + +``` +struct Foo { + x: i32, +} + +impl Drop for Foo { + fn drop(&mut self) { + println!("kaboom"); + } +} + +fn main() { + let mut x = Foo { x: -7 }; + x.drop(); // error: explicit use of destructor method +} +``` +"##, + E0046: r##" When trying to make some type implement a trait `Foo`, you must, at minimum, provide implementations for all of `Foo`'s required methods (meaning the @@ -241,7 +266,7 @@ impl Foo for Bar { fn foo(x: i16) { } // error, values differ in mutability - fn foo(&mut self) { } + fn bar(&mut self) { } } ``` "##, @@ -542,6 +567,21 @@ enum Empty {} ``` "##, +E0087: r##" +Too many type parameters were supplied for a function. For example: + +``` +fn foo() {} + +fn main() { + foo::(); // error, expected 1 parameter, found 2 parameters +} +``` + +The number of supplied parameters much exactly match the number of defined type +parameters. +"##, + E0089: r##" Not enough type parameters were supplied for a function. For example: @@ -1098,6 +1138,13 @@ impl Baz for Bar { } // Note: This is OK [RFC 255]: https://github.com/rust-lang/rfcs/pull/255 "##, +E0379: r##" +Trait methods cannot be declared `const` by design. For more information, see +[RFC 911]. + +[RFC 911]: https://github.com/rust-lang/rfcs/pull/911 +"##, + E0380: r##" Default impls are only allowed for traits with no methods or associated items. For more information see the [opt-in builtin traits RFC](https://github.com/rust @@ -1113,7 +1160,6 @@ impl Baz for Bar { } // Note: This is OK E0034, // multiple applicable methods in scope E0035, // does not take type parameters E0036, // incorrect number of type parameters given for this method - E0040, // explicit use of destructor method E0044, // foreign items may not have type parameters E0045, // variadic function must have C calling convention E0057, // method has an incompatible type for trait @@ -1128,7 +1174,6 @@ impl Baz for Bar { } // Note: This is OK E0077, E0085, E0086, - E0087, E0088, E0090, E0091, @@ -1235,7 +1280,6 @@ impl Baz for Bar { } // Note: This is OK // between structures E0377, // the trait `CoerceUnsized` may only be implemented for a coercion // between structures with the same definition - E0379, // trait fns cannot be const E0390, // only a single inherent implementation marked with // `#[lang = \"{}\"]` is allowed for the `{}` primitive E0391, // unsupported cyclic reference between types/traits detected diff --git a/src/test/compile-fail/static-mut-not-constant.rs b/src/test/compile-fail/static-mut-not-constant.rs index 08148328edc..e3bb01e6970 100644 --- a/src/test/compile-fail/static-mut-not-constant.rs +++ b/src/test/compile-fail/static-mut-not-constant.rs @@ -12,6 +12,6 @@ static mut a: Box = box 3; //~^ ERROR allocations are not allowed in statics -//~^^ ERROR mutable statics are not allowed to have owned pointers +//~^^ ERROR mutable statics are not allowed to have boxes fn main() {}