Skip to content
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

Clarify suggestion for E0013 #68079

Merged
merged 1 commit into from
Jan 11, 2020
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
12 changes: 7 additions & 5 deletions src/librustc_mir/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,18 @@ impl NonConstOp for StaticAccess {
item.tcx.sess,
span,
E0013,
"{}s cannot refer to statics, use \
a constant instead",
"{}s cannot refer to statics",
item.const_kind()
);
err.help(
"consider extracting the value of the `static` to a `const`, and referring to that",
);
if item.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(
"Static and const variables can refer to other const variables. \
But a const variable cannot refer to a static variable.",
"`static` and `const` variables can refer to other `const` variables. \
A `const` variable, however, cannot refer to a `static` variable.",
);
err.help("To fix this, the value can be extracted as a const and then used.");
err.help("To fix this, the value can be extracted to a `const` and then used.");
}
err.emit();
}
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/consts/const-fn-not-safe-for-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ error[E0015]: calls in constant functions are limited to constant functions, tup
LL | random()
| ^^^^^^^^

error[E0013]: constant functions cannot refer to statics, use a constant instead
error[E0013]: constant functions cannot refer to statics
--> $DIR/const-fn-not-safe-for-const.rs:20:5
|
LL | Y
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error[E0013]: constant functions cannot refer to statics, use a constant instead
error[E0013]: constant functions cannot refer to statics
--> $DIR/const-fn-not-safe-for-const.rs:25:6
|
LL | &Y
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error: aborting due to 3 previous errors

Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/issues/issue-17718-const-bad-values.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ LL | const C1: &'static mut [usize] = &mut [];
= note: for more information, see https://github.com/rust-lang/rust/issues/57349
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-17718-const-bad-values.rs:5:46
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-17718-const-bad-values.rs:5:46
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error[E0658]: references in constants may only refer to immutable values
--> $DIR/issue-17718-const-bad-values.rs:5:41
Expand Down
12 changes: 9 additions & 3 deletions src/test/ui/issues/issue-17718-references.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-17718-references.rs:9:29
|
LL | const T2: &'static usize = &S;
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-17718-references.rs:14:19
|
LL | const T6: usize = S;
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-17718-references.rs:19:33
|
LL | const T10: Struct = Struct { a: S };
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error: aborting due to 3 previous errors

Expand Down
3 changes: 1 addition & 2 deletions src/test/ui/issues/issue-18118-2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub fn main() {
const z: &'static isize = {
static p: isize = 3;
&p
//~^ ERROR constants cannot refer to statics, use a constant instead
&p //~ ERROR constants cannot refer to statics
};
}
4 changes: 3 additions & 1 deletion src/test/ui/issues/issue-18118-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-18118-2.rs:4:10
|
LL | &p
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error: aborting due to previous error

Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/issues/issue-52060.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0013]: constants cannot refer to statics, use a constant instead
error[E0013]: constants cannot refer to statics
--> $DIR/issue-52060.rs:4:26
|
LL | static B: [u32; 1] = [0; A.len()];
| ^
|
= help: consider extracting the value of the `static` to a `const`, and referring to that

error[E0080]: evaluation of constant value failed
--> $DIR/issue-52060.rs:4:26
Expand Down