Skip to content

Commit ab09557

Browse files
committed
Rollup merge of rust-lang#25873 - nham:update_E0015, r=Aatch
The E0397 explanation, as I've written it, isn't really an explanation, but I'm not sure what to put here. I will happily take suggestions. Partially addresses rust-lang#25851
2 parents 2f6dc44 + fdf3ce7 commit ab09557

File tree

4 files changed

+106
-14
lines changed

4 files changed

+106
-14
lines changed

src/librustc/diagnostics.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,13 @@ const Y: i32 = A;
196196
"##,
197197

198198
E0015: r##"
199-
The only function calls allowed in static or constant expressions are enum
200-
variant constructors or struct constructors (for unit or tuple structs). This
201-
is because Rust currently does not support compile-time function execution.
199+
The only functions that can be called in static or constant expressions are
200+
`const` functions. Rust currently does not support more general compile-time
201+
function execution.
202+
203+
See [RFC 911] for more details on the design of `const fn`s.
204+
205+
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
202206
"##,
203207

204208
E0018: r##"
@@ -842,6 +846,53 @@ struct Foo<T: 'static> {
842846
foo: &'static T
843847
}
844848
```
849+
"##,
850+
851+
E0378: r##"
852+
Method calls that aren't calls to inherent `const` methods are disallowed
853+
in statics, constants, and constant functions.
854+
855+
For example:
856+
857+
```
858+
const BAZ: i32 = Foo(25).bar(); // error, `bar` isn't `const`
859+
860+
struct Foo(i32);
861+
862+
impl Foo {
863+
const fn foo(&self) -> i32 {
864+
self.bar() // error, `bar` isn't `const`
865+
}
866+
867+
fn bar(&self) -> i32 { self.0 }
868+
}
869+
```
870+
871+
For more information about `const fn`'s, see [RFC 911].
872+
873+
[RFC 911]: https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md
874+
"##,
875+
876+
E0394: r##"
877+
From [RFC 246]:
878+
879+
> It is illegal for a static to reference another static by value. It is
880+
> required that all references be borrowed.
881+
882+
[RFC 246]: https://github.com/rust-lang/rfcs/pull/246
883+
"##,
884+
885+
E0397: r##"
886+
It is not allowed for a mutable static to allocate or have destructors. For
887+
example:
888+
889+
```
890+
// error: mutable statics are not allowed to have boxes
891+
static mut FOO: Option<Box<usize>> = None;
892+
893+
// error: mutable statics are not allowed to have destructors
894+
static mut BAR: Option<Vec<i32>> = None;
895+
```
845896
"##
846897

847898
}
@@ -891,9 +942,6 @@ register_diagnostics! {
891942
E0315, // cannot invoke closure outside of its lifetime
892943
E0316, // nested quantification of lifetimes
893944
E0370, // discriminant overflow
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
897945
E0395, // pointer comparison in const-expr
898946
E0396 // pointer dereference in const-expr
899947
}

src/librustc/middle/check_const.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
249249
let suffix = if tcontents.has_dtor() {
250250
"destructors"
251251
} else if tcontents.owns_owned() {
252-
"owned pointers"
252+
"boxes"
253253
} else {
254254
return
255255
};
256256

257-
self.tcx.sess.span_err(e.span, &format!("mutable statics are not allowed \
258-
to have {}", suffix));
257+
span_err!(self.tcx.sess, e.span, E0397,
258+
"mutable statics are not allowed to have {}", suffix);
259259
}
260260

261261
fn check_static_type(&self, e: &ast::Expr) {

src/librustc_typeck/diagnostics.rs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,31 @@ Reference:
170170
http://doc.rust-lang.org/reference.html#trait-objects
171171
"##,
172172

173+
E0040: r##"
174+
It is not allowed to manually call destructors in Rust. It is also not
175+
necessary to do this since `drop` is called automatically whenever a value goes
176+
out of scope.
177+
178+
Here's an example of this error:
179+
180+
```
181+
struct Foo {
182+
x: i32,
183+
}
184+
185+
impl Drop for Foo {
186+
fn drop(&mut self) {
187+
println!("kaboom");
188+
}
189+
}
190+
191+
fn main() {
192+
let mut x = Foo { x: -7 };
193+
x.drop(); // error: explicit use of destructor method
194+
}
195+
```
196+
"##,
197+
173198
E0046: r##"
174199
When trying to make some type implement a trait `Foo`, you must, at minimum,
175200
provide implementations for all of `Foo`'s required methods (meaning the
@@ -241,7 +266,7 @@ impl Foo for Bar {
241266
fn foo(x: i16) { }
242267
243268
// error, values differ in mutability
244-
fn foo(&mut self) { }
269+
fn bar(&mut self) { }
245270
}
246271
```
247272
"##,
@@ -542,6 +567,21 @@ enum Empty {}
542567
```
543568
"##,
544569

570+
E0087: r##"
571+
Too many type parameters were supplied for a function. For example:
572+
573+
```
574+
fn foo<T>() {}
575+
576+
fn main() {
577+
foo::<f64, bool>(); // error, expected 1 parameter, found 2 parameters
578+
}
579+
```
580+
581+
The number of supplied parameters much exactly match the number of defined type
582+
parameters.
583+
"##,
584+
545585
E0089: r##"
546586
Not enough type parameters were supplied for a function. For example:
547587
@@ -1098,6 +1138,13 @@ Trait2 { ... }`) does not work if the trait is not object-safe. Please see the
10981138
[RFC 255]: https://github.com/rust-lang/rfcs/pull/255
10991139
"##,
11001140

1141+
E0379: r##"
1142+
Trait methods cannot be declared `const` by design. For more information, see
1143+
[RFC 911].
1144+
1145+
[RFC 911]: https://github.com/rust-lang/rfcs/pull/911
1146+
"##,
1147+
11011148
E0380: r##"
11021149
Default impls are only allowed for traits with no methods or associated items.
11031150
For more information see the [opt-in builtin traits RFC](https://github.com/rust
@@ -1113,7 +1160,6 @@ register_diagnostics! {
11131160
E0034, // multiple applicable methods in scope
11141161
E0035, // does not take type parameters
11151162
E0036, // incorrect number of type parameters given for this method
1116-
E0040, // explicit use of destructor method
11171163
E0044, // foreign items may not have type parameters
11181164
E0045, // variadic function must have C calling convention
11191165
E0057, // method has an incompatible type for trait
@@ -1128,7 +1174,6 @@ register_diagnostics! {
11281174
E0077,
11291175
E0085,
11301176
E0086,
1131-
E0087,
11321177
E0088,
11331178
E0090,
11341179
E0091,
@@ -1235,7 +1280,6 @@ register_diagnostics! {
12351280
// between structures
12361281
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
12371282
// between structures with the same definition
1238-
E0379, // trait fns cannot be const
12391283
E0390, // only a single inherent implementation marked with
12401284
// `#[lang = \"{}\"]` is allowed for the `{}` primitive
12411285
E0391, // unsupported cyclic reference between types/traits detected

src/test/compile-fail/static-mut-not-constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
static mut a: Box<isize> = box 3;
1414
//~^ ERROR allocations are not allowed in statics
15-
//~^^ ERROR mutable statics are not allowed to have owned pointers
15+
//~^^ ERROR mutable statics are not allowed to have boxes
1616

1717
fn main() {}

0 commit comments

Comments
 (0)