-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add E0400 error explanation #30167
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
Add E0400 error explanation #30167
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1899,6 +1899,50 @@ contain references (with a maximum lifetime of `'a`). | |
[1]: https://github.com/rust-lang/rfcs/pull/1156 | ||
"##, | ||
|
||
E0400: r##" | ||
A user-defined dereference was attempted in an invalid context. Erroneous | ||
code example: | ||
|
||
``` | ||
use std::ops::Deref; | ||
|
||
struct A; | ||
|
||
impl Deref for A { | ||
type Target = str; | ||
|
||
fn deref(&self)-> &str { "foo" } | ||
} | ||
|
||
const S: &'static str = &A; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you meant *A? |
||
// error: user-defined dereference operators are not allowed in constants | ||
|
||
fn main() { | ||
let foo = S; | ||
} | ||
``` | ||
|
||
You cannot directly use a dereference operation whilst initializing a constant | ||
or a static. To fix this error, restructure your code to avoid this dereference, | ||
perharps moving it inline: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, I arrived a little too late to point out the spelling error: "perharps" before bors started its run... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, I'll fix it next error explanation I create. :) |
||
|
||
``` | ||
use std::ops::Deref; | ||
|
||
struct A; | ||
|
||
impl Deref for A { | ||
type Target = str; | ||
|
||
fn deref(&self)-> &str { "foo" } | ||
} | ||
|
||
fn main() { | ||
let foo : &str = &A; | ||
} | ||
``` | ||
"##, | ||
|
||
E0492: r##" | ||
A borrow of a constant containing interior mutability was attempted. Erroneous | ||
code example: | ||
|
@@ -2175,7 +2219,6 @@ register_diagnostics! { | |
E0314, // closure outlives stack frame | ||
E0315, // cannot invoke closure outside of its lifetime | ||
E0316, // nested quantification of lifetimes | ||
E0400, // overloaded derefs are not allowed in constants | ||
E0452, // malformed lint attribute | ||
E0453, // overruled by outer forbid | ||
E0471, // constant evaluation error: .. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You didn't change the code.
When I say that it should be moved inline, I mean directly writing *A in the code where S was going to be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what you're asking in here.