Skip to content

Add 5 more error explanations. Update E0015's explanation. Add an error code. #25873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,13 @@ const Y: i32 = A;
"##,

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##"
Expand Down Expand Up @@ -842,6 +846,53 @@ struct Foo<T: 'static> {
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<Box<usize>> = None;

// error: mutable statics are not allowed to have destructors
static mut BAR: Option<Vec<i32>> = None;
```
"##

}
Expand Down Expand Up @@ -891,9 +942,6 @@ register_diagnostics! {
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
}
6 changes: 3 additions & 3 deletions src/librustc/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ impl<'a, 'tcx> CheckCrateVisitor<'a, 'tcx> {
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) {
Expand Down
52 changes: 48 additions & 4 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ Reference:
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
Expand Down Expand Up @@ -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) { }
}
```
"##,
Expand Down Expand Up @@ -542,6 +567,21 @@ enum Empty {}
```
"##,

E0087: r##"
Too many type parameters were supplied for a function. For example:

```
fn foo<T>() {}

fn main() {
foo::<f64, bool>(); // 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:

Expand Down Expand Up @@ -1098,6 +1138,13 @@ Trait2 { ... }`) does not work if the trait is not object-safe. Please see the
[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
Expand All @@ -1113,7 +1160,6 @@ register_diagnostics! {
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
Expand All @@ -1128,7 +1174,6 @@ register_diagnostics! {
E0077,
E0085,
E0086,
E0087,
E0088,
E0090,
E0091,
Expand Down Expand Up @@ -1235,7 +1280,6 @@ register_diagnostics! {
// 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
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/static-mut-not-constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

static mut a: Box<isize> = 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() {}