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() { }

0 commit comments

Comments
 (0)