Skip to content

Overhaul const-checking diagnostics #77354

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 23 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4d343a5
Useful derives on `mir::LocalKind`
ecstatic-morse Sep 30, 2020
325b7d4
Continue const-checking after errors when easy
ecstatic-morse Sep 29, 2020
25c7753
Continue after `impl Trait` in `const fn`
ecstatic-morse Sep 30, 2020
c38aca0
`delay_span_bug` if const-checking an `async` function
ecstatic-morse Sep 30, 2020
20e07e7
Forbid generator-specific MIR in all const-contexts
ecstatic-morse Sep 30, 2020
782a595
Return a `DiagnosticBuilder` from structured errors
ecstatic-morse Sep 29, 2020
ce50939
Fix "unstable in stable" error
ecstatic-morse Sep 29, 2020
de35c42
Remove `ops::non_const`
ecstatic-morse Sep 29, 2020
b518ccb
Give `MutDeref` a real error message
ecstatic-morse Sep 29, 2020
a23297f
Bless mut tests
ecstatic-morse Sep 29, 2020
5b31455
Priority levels
ecstatic-morse Sep 30, 2020
b400871
Don't emit duplicate errors for the return place
ecstatic-morse Sep 30, 2020
51fbd55
Bless tests
ecstatic-morse Sep 30, 2020
37f37dc
Emit multiple function pointer errors from const-checker
ecstatic-morse Sep 30, 2020
879d379
Bless output
ecstatic-morse Sep 30, 2020
e02ea83
Don't stop const-checking after erroneous trait bound
ecstatic-morse Sep 30, 2020
4bbc79c
Bless tests
ecstatic-morse Sep 30, 2020
287993c
Remove machinery for halting error output
ecstatic-morse Sep 30, 2020
bed7b29
Update `compile-fail` test
ecstatic-morse Sep 30, 2020
1513904
Remove default `build_error` impl
ecstatic-morse Sep 30, 2020
7c6d685
Rewrite E0019 example
ecstatic-morse Sep 30, 2020
0c26144
Better span for attribute suggestions
ecstatic-morse Sep 30, 2020
1301f43
Remove E0019, use E0015 for inline assembly in a const
ecstatic-morse Sep 30, 2020
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
2 changes: 0 additions & 2 deletions compiler/rustc_mir/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,6 @@ pub mod ty {
#[derive(Debug)]
pub struct MutRef;
impl NonConstOp for MutRef {
const STOPS_CONST_CHECKING: bool = true;

fn status_in_item(&self, _ccx: &ConstCx<'_, '_>) -> Status {
Status::Unstable(sym::const_mut_refs)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// New test for #53818: modifying static memory at compile-time is not allowed.
// The test should never compile successfully

#![feature(const_raw_ptr_deref)]
#![feature(const_raw_ptr_deref, const_mut_refs)]

Comment on lines -4 to 5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was only failing pre-monomorphization due to a missing feature gate. It seemed like we wanted to exhaustively test this, especially since there is another test without the feature gate modified in this commit. This led to #77353.

use std::cell::UnsafeCell;

Expand All @@ -13,7 +13,7 @@ unsafe impl Sync for Foo {}
static FOO: Foo = Foo(UnsafeCell::new(42));

static BAR: () = unsafe {
*FOO.0.get() = 5; //~ ERROR contains unimplemented expression type
*FOO.0.get() = 5; //~ ERROR
};

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
error[E0019]: static contains unimplemented expression type
error[E0080]: could not evaluate static initializer
--> $DIR/assign-to-static-within-other-static-2.rs:16:5
|
LL | *FOO.0.get() = 5;
| ^^^^^^^^^^^^^^^^
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
| ^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer

error: aborting due to previous error

For more information about this error, try `rustc --explain E0019`.
For more information about this error, try `rustc --explain E0080`.
7 changes: 1 addition & 6 deletions src/test/ui/consts/const-eval/mod-static-with-const-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ unsafe impl Sync for Foo {}

static FOO: Foo = Foo(UnsafeCell::new(42));

fn foo() {}

static BAR: () = unsafe {
*FOO.0.get() = 5;
//~^ contains unimplemented expression

foo();
//~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
//~^ mutation through a reference
};

fn main() {
Expand Down
16 changes: 5 additions & 11 deletions src/test/ui/consts/const-eval/mod-static-with-const-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
error[E0019]: static contains unimplemented expression type
--> $DIR/mod-static-with-const-fn.rs:18:5
error[E0658]: mutation through a reference is not allowed in statics
--> $DIR/mod-static-with-const-fn.rs:15:5
|
LL | *FOO.0.get() = 5;
| ^^^^^^^^^^^^^^^^
|
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> $DIR/mod-static-with-const-fn.rs:21:5
|
LL | foo();
| ^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors have detailed explanations: E0015, E0019.
For more information about an error, try `rustc --explain E0015`.
For more information about this error, try `rustc --explain E0658`.
8 changes: 3 additions & 5 deletions src/test/ui/consts/const_let_assign3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ struct S {

impl S {
const fn foo(&mut self, x: u32) {
//~^ ERROR mutable references
//~^ ERROR mutable reference
self.state = x;
}
}

const FOO: S = {
let mut s = S { state: 42 };
s.foo(3); //~ ERROR mutable references are not allowed in constants
s.foo(3); //~ ERROR mutable reference
s
};

type Array = [u32; {
let mut x = 2;
let y = &mut x;
//~^ ERROR mutable references are not allowed in constants
let y = &mut x; //~ ERROR mutable reference
*y = 42;
//~^ ERROR constant contains unimplemented expression type
*y
}];

Expand Down
14 changes: 3 additions & 11 deletions src/test/ui/consts/const_let_assign3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ error[E0764]: mutable references are not allowed in constants
LL | let y = &mut x;
| ^^^^^^ `&mut` is only allowed in `const fn`

error[E0019]: constant contains unimplemented expression type
--> $DIR/const_let_assign3.rs:24:5
|
LL | *y = 42;
| ^^^^^^^
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable

error: aborting due to 4 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0019, E0658, E0764.
For more information about an error, try `rustc --explain E0019`.
Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`.
1 change: 0 additions & 1 deletion src/test/ui/consts/min_const_fn/bad_const_fn_body_ice.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fn foo(a: i32) -> Vec<i32> {
vec![1, 2, 3]
//~^ ERROR allocations are not allowed
//~| ERROR unimplemented expression type
//~| ERROR calls in constant functions
}

Expand Down
13 changes: 2 additions & 11 deletions src/test/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ LL | vec![1, 2, 3]
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0019]: constant function contains unimplemented expression type
--> $DIR/bad_const_fn_body_ice.rs:2:5
|
LL | vec![1, 2, 3]
| ^^^^^^^^^^^^^
|
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
--> $DIR/bad_const_fn_body_ice.rs:2:5
|
Expand All @@ -23,7 +14,7 @@ LL | vec![1, 2, 3]
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0010, E0015, E0019.
Some errors have detailed explanations: E0010, E0015.
For more information about an error, try `rustc --explain E0010`.
12 changes: 12 additions & 0 deletions src/test/ui/consts/min_const_fn/min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,37 @@ impl<T> Foo<T> {
const fn get(&self) -> &T { &self.0 }
const fn get_mut(&mut self) -> &mut T { &mut self.0 }
//~^ mutable references
//~| mutable references
//~| mutable references
//~| mutable references
}
impl<'a, T> Foo<T> {
const fn new_lt(t: T) -> Self { Foo(t) }
const fn into_inner_lt(self) -> T { self.0 } //~ destructors cannot be evaluated
const fn get_lt(&'a self) -> &T { &self.0 }
const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 }
//~^ mutable references
//~| mutable references
//~| mutable references
//~| mutable references
}
impl<T: Sized> Foo<T> {
const fn new_s(t: T) -> Self { Foo(t) }
const fn into_inner_s(self) -> T { self.0 } //~ ERROR destructors
const fn get_s(&self) -> &T { &self.0 }
const fn get_mut_s(&mut self) -> &mut T { &mut self.0 }
//~^ mutable references
//~| mutable references
//~| mutable references
//~| mutable references
}
impl<T: ?Sized> Foo<T> {
const fn get_sq(&self) -> &T { &self.0 }
const fn get_mut_sq(&mut self) -> &mut T { &mut self.0 }
//~^ mutable references
//~| mutable references
//~| mutable references
//~| mutable references
}


Expand Down
Loading