Skip to content

Commit 94329f6

Browse files
committed
Always evaluate free constants and statics, even if previous errors occurred
1 parent 81b757c commit 94329f6

File tree

88 files changed

+842
-480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+842
-480
lines changed

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
194194
collect::test_opaque_hidden_types(tcx)?;
195195
}
196196

197+
tcx.hir().par_body_owners(|item_def_id| {
198+
let def_kind = tcx.def_kind(item_def_id);
199+
match def_kind {
200+
DefKind::Static(_) => tcx.ensure().eval_static_initializer(item_def_id.into()),
201+
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
202+
_ => (),
203+
}
204+
});
205+
197206
// Freeze definitions as we don't add new ones at this point. This improves performance by
198207
// allowing lock-free access to them.
199208
tcx.untracked().definitions.freeze();

compiler/rustc_lint/src/builtin.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,32 +1542,6 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
15421542
}
15431543
}
15441544

1545-
declare_lint_pass!(
1546-
/// Lint constants that are erroneous.
1547-
/// Without this lint, we might not get any diagnostic if the constant is
1548-
/// unused within this crate, even though downstream crates can't use it
1549-
/// without producing an error.
1550-
UnusedBrokenConst => []
1551-
);
1552-
1553-
impl<'tcx> LateLintPass<'tcx> for UnusedBrokenConst {
1554-
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
1555-
match it.kind {
1556-
hir::ItemKind::Const(_, _, body_id) => {
1557-
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
1558-
// trigger the query once for all constants since that will already report the errors
1559-
// FIXME(generic_const_items): Does this work properly with generic const items?
1560-
cx.tcx.ensure().const_eval_poly(def_id);
1561-
}
1562-
hir::ItemKind::Static(_, _, body_id) => {
1563-
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
1564-
cx.tcx.ensure().eval_static_initializer(def_id);
1565-
}
1566-
_ => {}
1567-
}
1568-
}
1569-
}
1570-
15711545
declare_lint! {
15721546
/// The `trivial_bounds` lint detects trait bounds that don't depend on
15731547
/// any type parameters.

compiler/rustc_lint/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ late_lint_methods!(
212212
ExplicitOutlivesRequirements: ExplicitOutlivesRequirements,
213213
InvalidValue: InvalidValue,
214214
DerefNullPtr: DerefNullPtr,
215-
// May Depend on constants elsewhere
216-
UnusedBrokenConst: UnusedBrokenConst,
217215
UnstableFeatures: UnstableFeatures,
218216
UngatedAsyncFnTrackCaller: UngatedAsyncFnTrackCaller,
219217
ArrayIntoIter: ArrayIntoIter::default(),

compiler/rustc_middle/src/mir/interpret/queries.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ impl<'tcx> TyCtxtAt<'tcx> {
192192
self,
193193
def_id: DefId,
194194
) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> {
195+
if let Some(def_id) = def_id.as_local() {
196+
if let Some(err) = self
197+
.mir_borrowck(def_id)
198+
.tainted_by_errors
199+
.or(self.mir_const_qualif(def_id).tainted_by_errors)
200+
{
201+
return Err(err.into());
202+
}
203+
}
195204
trace!("eval_static_initializer: Need to compute {:?}", def_id);
196205
assert!(self.is_static(def_id));
197206
let instance = ty::Instance::mono(*self, def_id);

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ trait Foo {
44
const BAR: u32;
55
}
66

7-
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
7+
const IMPL_REF_BAR: u32 = GlobalImplRef::BAR; //~ ERROR E0391
88

99
struct GlobalImplRef;
1010

1111
impl GlobalImplRef {
12-
const BAR: u32 = IMPL_REF_BAR; //~ ERROR E0391
12+
const BAR: u32 = IMPL_REF_BAR;
1313
}
1414

1515
fn main() {}

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`
2-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:22
3-
|
4-
LL | const BAR: u32 = IMPL_REF_BAR;
5-
| ^^^^^^^^^^^^
6-
|
7-
note: ...which requires simplifying constant for the type system `IMPL_REF_BAR`...
1+
error[E0391]: cycle detected when simplifying constant for the type system `IMPL_REF_BAR`
82
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
93
|
104
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
115
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
127
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
138
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:27
149
|
@@ -29,7 +24,12 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
2924
|
3025
LL | const BAR: u32 = IMPL_REF_BAR;
3126
| ^^^^^^^^^^^^^^
32-
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`, completing the cycle
27+
note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
28+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:22
29+
|
30+
LL | const BAR: u32 = IMPL_REF_BAR;
31+
| ^^^^^^^^^^^^
32+
= note: ...which again requires simplifying constant for the type system `IMPL_REF_BAR`, completing the cycle
3333
= note: cycle used when running analysis passes on this crate
3434
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3535

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
1+
error[E0391]: cycle detected when caching mir of `FooDefault::BAR` for CTFE
2+
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
3+
|
4+
LL | const BAR: u32 = DEFAULT_REF_BAR;
5+
| ^^^^^^^^^^^^^^
6+
|
7+
note: ...which requires elaborating drops for `FooDefault::BAR`...
28
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:22
39
|
410
LL | const BAR: u32 = DEFAULT_REF_BAR;
511
| ^^^^^^^^^^^^^^^
6-
|
712
note: ...which requires simplifying constant for the type system `DEFAULT_REF_BAR`...
813
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
914
|
@@ -24,13 +29,12 @@ note: ...which requires const-evaluating + checking `FooDefault::BAR`...
2429
|
2530
LL | const BAR: u32 = DEFAULT_REF_BAR;
2631
| ^^^^^^^^^^^^^^
27-
note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
32+
= note: ...which again requires caching mir of `FooDefault::BAR` for CTFE, completing the cycle
33+
note: cycle used when const-evaluating + checking `FooDefault::BAR`
2834
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
2935
|
3036
LL | const BAR: u32 = DEFAULT_REF_BAR;
3137
| ^^^^^^^^^^^^^^
32-
= note: ...which again requires elaborating drops for `FooDefault::BAR`, completing the cycle
33-
= note: cycle used when running analysis passes on this crate
3438
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3539

3640
error: aborting due to 1 previous error

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ trait Foo {
44
const BAR: u32;
55
}
66

7-
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
7+
const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR; //~ ERROR E0391
88

99
struct GlobalTraitRef;
1010

1111
impl Foo for GlobalTraitRef {
12-
const BAR: u32 = TRAIT_REF_BAR; //~ ERROR E0391
12+
const BAR: u32 = TRAIT_REF_BAR;
1313
}
1414

1515
fn main() {}

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`
2-
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:22
3-
|
4-
LL | const BAR: u32 = TRAIT_REF_BAR;
5-
| ^^^^^^^^^^^^^
6-
|
7-
note: ...which requires simplifying constant for the type system `TRAIT_REF_BAR`...
1+
error[E0391]: cycle detected when simplifying constant for the type system `TRAIT_REF_BAR`
82
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
93
|
104
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
115
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
127
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
138
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:28
149
|
@@ -29,7 +24,12 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
2924
|
3025
LL | const BAR: u32 = TRAIT_REF_BAR;
3126
| ^^^^^^^^^^^^^^
32-
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`, completing the cycle
27+
note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`...
28+
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:22
29+
|
30+
LL | const BAR: u32 = TRAIT_REF_BAR;
31+
| ^^^^^^^^^^^^^
32+
= note: ...which again requires simplifying constant for the type system `TRAIT_REF_BAR`, completing the cycle
3333
= note: cycle used when running analysis passes on this crate
3434
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3535

tests/ui/check-static-immutable-mut-slices.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
static TEST: &'static mut [isize] = &mut [];
44
//~^ ERROR mutable references are not allowed
5+
//~| ERROR undefined behavior to use this value
56

67
pub fn main() { }

tests/ui/check-static-immutable-mut-slices.stderr

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ error[E0764]: mutable references are not allowed in the final value of statics
44
LL | static TEST: &'static mut [isize] = &mut [];
55
| ^^^^^^^
66

7-
error: aborting due to 1 previous error
7+
error[E0080]: it is undefined behavior to use this value
8+
--> $DIR/check-static-immutable-mut-slices.rs:3:1
9+
|
10+
LL | static TEST: &'static mut [isize] = &mut [];
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered mutable reference in a `const` or `static`
12+
|
13+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
14+
= note: the raw bytes of the constant (size: 16, align: 8) {
15+
╾ALLOC0╼ 00 00 00 00 00 00 00 00 │ ╾──────╼........
16+
}
17+
18+
error: aborting due to 2 previous errors
819

9-
For more information about this error, try `rustc --explain E0764`.
20+
Some errors have detailed explanations: E0080, E0764.
21+
For more information about an error, try `rustc --explain E0080`.

tests/ui/check-static-values-constraints.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,6 @@ LL | static STATIC19: Vec<isize> = vec![3];
129129
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
130130
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
131131

132-
error[E0507]: cannot move out of static item `x`
133-
--> $DIR/check-static-values-constraints.rs:119:9
134-
|
135-
LL | x
136-
| ^ move occurs because `x` has type `Vec<isize>`, which does not implement the `Copy` trait
137-
|
138-
help: consider borrowing here
139-
|
140-
LL | &x
141-
| +
142-
143132
error[E0010]: allocations are not allowed in statics
144133
--> $DIR/check-static-values-constraints.rs:117:32
145134
|
@@ -158,6 +147,17 @@ LL | static x: Vec<isize> = vec![3];
158147
= note: consider wrapping this expression in `Lazy::new(|| ...)` from the `once_cell` crate: https://crates.io/crates/once_cell
159148
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
160149

150+
error[E0507]: cannot move out of static item `x`
151+
--> $DIR/check-static-values-constraints.rs:119:9
152+
|
153+
LL | x
154+
| ^ move occurs because `x` has type `Vec<isize>`, which does not implement the `Copy` trait
155+
|
156+
help: consider borrowing here
157+
|
158+
LL | &x
159+
| +
160+
161161
error: aborting due to 17 previous errors
162162

163163
Some errors have detailed explanations: E0010, E0015, E0493, E0507.

tests/ui/const-generics/issues/issue-100313.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ impl <const B: &'static bool> T<B> {
99
unsafe {
1010
*(B as *const bool as *mut bool) = false;
1111
//~^ ERROR evaluation of constant value failed [E0080]
12-
//~| ERROR assigning to `&T` is undefined behavior
1312
}
1413
}
1514
}
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
2-
--> $DIR/issue-100313.rs:10:13
3-
|
4-
LL | *(B as *const bool as *mut bool) = false;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: for more information, visit <https://doc.rust-lang.org/book/ch15-05-interior-mutability.html>
8-
= note: `#[deny(invalid_reference_casting)]` on by default
9-
101
error[E0080]: evaluation of constant value failed
112
--> $DIR/issue-100313.rs:10:13
123
|
@@ -19,11 +10,11 @@ note: inside `T::<&true>::set_false`
1910
LL | *(B as *const bool as *mut bool) = false;
2011
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2112
note: inside `_`
22-
--> $DIR/issue-100313.rs:19:5
13+
--> $DIR/issue-100313.rs:18:5
2314
|
2415
LL | x.set_false();
2516
| ^^^^^^^^^^^^^
2617

27-
error: aborting due to 2 previous errors
18+
error: aborting due to 1 previous error
2819

2920
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-array-oob.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const FOO: [usize; 3] = [1, 2, 3];
2-
const BAR: usize = FOO[5]; // no error, because the error below occurs before regular const eval
2+
const BAR: usize = FOO[5];
3+
//~^ ERROR: evaluation of constant value failed
34

45
const BLUB: [u32; FOO[4]] = [5, 6];
56
//~^ ERROR evaluation of constant value failed [E0080]
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/const-array-oob.rs:4:19
2+
--> $DIR/const-array-oob.rs:5:19
33
|
44
LL | const BLUB: [u32; FOO[4]] = [5, 6];
55
| ^^^^^^ index out of bounds: the length is 3 but the index is 4
66

7-
error: aborting due to 1 previous error
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/const-array-oob.rs:2:20
9+
|
10+
LL | const BAR: usize = FOO[5];
11+
| ^^^^^^ index out of bounds: the length is 3 but the index is 5
12+
13+
error: aborting due to 2 previous errors
814

915
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-eval/const-eval-query-stack.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ LL | const X: i32 = 1 / 0;
77
query stack during panic:
88
#0 [eval_to_allocation_raw] const-evaluating + checking `X`
99
#1 [eval_to_const_value_raw] simplifying constant for the type system `X`
10-
#2 [lint_mod] linting top-level module
11-
#3 [analysis] running analysis passes on this crate
10+
#2 [analysis] running analysis passes on this crate
1211
end of query stack
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
warning: skipping const checks
22
|
3-
help: skipping check that does not even have a feature gate
4-
--> $DIR/const_fn_ptr.rs:11:5
5-
|
6-
LL | X(x)
7-
| ^^^^
83
help: skipping check that does not even have a feature gate
94
--> $DIR/const_fn_ptr.rs:15:5
105
|
@@ -15,6 +10,11 @@ help: skipping check that does not even have a feature gate
1510
|
1611
LL | x(y)
1712
| ^^^^
13+
help: skipping check that does not even have a feature gate
14+
--> $DIR/const_fn_ptr.rs:11:5
15+
|
16+
LL | X(x)
17+
| ^^^^
1818

1919
warning: 1 warning emitted
2020

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
error[E0597]: `x` does not live long enough
2+
--> $DIR/generic-slice.rs:27:5
3+
|
4+
LL | let x: &[_] = &[];
5+
| - binding `x` declared here
6+
LL | &x
7+
| ^^
8+
| |
9+
| borrowed value does not live long enough
10+
| using this value as a static requires that `x` is borrowed for `'static`
11+
LL |
12+
LL | };
13+
| - `x` dropped here while still borrowed
14+
115
error[E0597]: `x` does not live long enough
216
--> $DIR/generic-slice.rs:15:9
317
|
@@ -15,20 +29,6 @@ LL |
1529
LL | };
1630
| - `x` dropped here while still borrowed
1731

18-
error[E0597]: `x` does not live long enough
19-
--> $DIR/generic-slice.rs:27:5
20-
|
21-
LL | let x: &[_] = &[];
22-
| - binding `x` declared here
23-
LL | &x
24-
| ^^
25-
| |
26-
| borrowed value does not live long enough
27-
| using this value as a static requires that `x` is borrowed for `'static`
28-
LL |
29-
LL | };
30-
| - `x` dropped here while still borrowed
31-
3232
error: aborting due to 2 previous errors
3333

3434
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)