Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 74cde5f

Browse files
committed
Run const_prop_lint in check builds, too
1 parent 8c0b4f6 commit 74cde5f

File tree

127 files changed

+1293
-531
lines changed

Some content is hidden

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

127 files changed

+1293
-531
lines changed

compiler/rustc_abi/src/layout.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ where
426426
}
427427
}
428428
}
429-
_ => assert!(
430-
start == Bound::Unbounded && end == Bound::Unbounded,
431-
"nonscalar layout for layout_scalar_valid_range type: {st:#?}",
432-
),
429+
_ => {
430+
if start != Bound::Unbounded || end != Bound::Unbounded {
431+
return None;
432+
}
433+
}
433434
}
434435

435436
Some(st)

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
738738
// Run unsafety check because it's responsible for stealing and
739739
// deallocating THIR.
740740
tcx.ensure().check_unsafety(def_id);
741+
tcx.ensure().const_prop_lint(def_id);
741742
tcx.ensure().mir_borrowck(def_id)
742743
});
743744
});

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,12 @@ rustc_queries! {
10131013
cache_on_disk_if(tcx) { tcx.is_typeck_child(key.to_def_id()) }
10141014
}
10151015

1016+
/// Run the const prop lints on the `mir_promoted` of an item.
1017+
query const_prop_lint(key: LocalDefId) {
1018+
desc { |tcx| "checking const prop lints for `{}`", tcx.def_path_str(key) }
1019+
cache_on_disk_if { true }
1020+
}
1021+
10161022
/// Gets a complete map from all types to their inherent impls.
10171023
/// Not meant to be used directly outside of coherence.
10181024
query crate_inherent_impls(k: ()) -> Result<&'tcx CrateInherentImpls, ErrorGuaranteed> {

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn provide(providers: &mut Providers) {
142142
is_ctfe_mir_available: |tcx, did| is_mir_available(tcx, did),
143143
mir_callgraph_reachable: inline::cycle::mir_callgraph_reachable,
144144
mir_inliner_callees: inline::cycle::mir_inliner_callees,
145+
const_prop_lint,
145146
promoted_mir,
146147
deduced_param_attrs: deduce_param_attrs::deduced_param_attrs,
147148
..*providers
@@ -394,6 +395,27 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
394395
body
395396
}
396397

398+
fn const_prop_lint(tcx: TyCtxt<'_>, def: LocalDefId) {
399+
let (body, _) = tcx.mir_promoted(def);
400+
let body = body.borrow();
401+
402+
let mir_borrowck = tcx.mir_borrowck(def);
403+
404+
// If there are impossible bounds on the body being const prop linted,
405+
// the const eval logic used in const prop may ICE unexpectedly.
406+
let predicates = tcx
407+
.predicates_of(body.source.def_id())
408+
.predicates
409+
.iter()
410+
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
411+
if !traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect())
412+
&& mir_borrowck.tainted_by_errors.is_none()
413+
&& body.tainted_by_errors.is_none()
414+
{
415+
const_prop_lint::ConstPropLint.run_lint(tcx, &body);
416+
}
417+
}
418+
397419
/// Obtain just the main MIR (no promoteds) and run some cleanups on it. This also runs
398420
/// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't
399421
/// end up missing the source MIR due to stealing happening.
@@ -402,6 +424,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
402424
tcx.ensure_with_value().mir_coroutine_witnesses(def);
403425
}
404426
let mir_borrowck = tcx.mir_borrowck(def);
427+
tcx.ensure().const_prop_lint(def);
405428

406429
let is_fn_like = tcx.def_kind(def).is_fn_like();
407430
if is_fn_like {
@@ -534,7 +557,6 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
534557
&elaborate_box_derefs::ElaborateBoxDerefs,
535558
&coroutine::StateTransform,
536559
&add_retag::AddRetag,
537-
&Lint(const_prop_lint::ConstPropLint),
538560
];
539561
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
540562
}

tests/ui/array-slice-vec/array_const_index-0.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
44
LL | const B: i32 = (&A)[1];
55
| ^^^^^^^ index out of bounds: the length is 0 but the index is 1
66

7+
note: erroneous constant encountered
8+
--> $DIR/array_const_index-0.rs:7:13
9+
|
10+
LL | let _ = B;
11+
| ^
12+
13+
note: erroneous constant encountered
14+
--> $DIR/array_const_index-0.rs:7:13
15+
|
16+
LL | let _ = B;
17+
| ^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
721
error: aborting due to 1 previous error
822

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

tests/ui/array-slice-vec/array_const_index-1.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
44
LL | const B: i32 = A[1];
55
| ^^^^ index out of bounds: the length is 0 but the index is 1
66

7+
note: erroneous constant encountered
8+
--> $DIR/array_const_index-1.rs:7:13
9+
|
10+
LL | let _ = B;
11+
| ^
12+
13+
note: erroneous constant encountered
14+
--> $DIR/array_const_index-1.rs:7:13
15+
|
16+
LL | let _ = B;
17+
| ^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
721
error: aborting due to 1 previous error
822

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

tests/ui/associated-consts/defaults-cyclic-fail.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// build-fail
2-
31
// Cyclic assoc. const defaults don't error unless *used*
42
trait Tr {
53
const A: u8 = Self::B;

tests/ui/associated-consts/defaults-cyclic-fail.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
error[E0391]: cycle detected when simplifying constant for the type system `Tr::A`
2-
--> $DIR/defaults-cyclic-fail.rs:5:5
2+
--> $DIR/defaults-cyclic-fail.rs:3:5
33
|
44
LL | const A: u8 = Self::B;
55
| ^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating + checking `Tr::A`...
8-
--> $DIR/defaults-cyclic-fail.rs:5:19
8+
--> $DIR/defaults-cyclic-fail.rs:3:19
99
|
1010
LL | const A: u8 = Self::B;
1111
| ^^^^^^^
1212
note: ...which requires simplifying constant for the type system `Tr::B`...
13-
--> $DIR/defaults-cyclic-fail.rs:8:5
13+
--> $DIR/defaults-cyclic-fail.rs:6:5
1414
|
1515
LL | const B: u8 = Self::A;
1616
| ^^^^^^^^^^^
1717
note: ...which requires const-evaluating + checking `Tr::B`...
18-
--> $DIR/defaults-cyclic-fail.rs:8:19
18+
--> $DIR/defaults-cyclic-fail.rs:6:19
1919
|
2020
LL | const B: u8 = Self::A;
2121
| ^^^^^^^
2222
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
2323
note: cycle used when const-evaluating + checking `main::promoted[1]`
24-
--> $DIR/defaults-cyclic-fail.rs:16:16
24+
--> $DIR/defaults-cyclic-fail.rs:14:16
2525
|
2626
LL | assert_eq!(<() as Tr>::A, 0);
2727
| ^^^^^^^^^^^^^

tests/ui/associated-consts/defaults-not-assumed-fail.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// build-fail
2-
31
trait Tr {
42
const A: u8 = 255;
53

tests/ui/associated-consts/defaults-not-assumed-fail.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error[E0080]: evaluation of `<() as Tr>::B` failed
2-
--> $DIR/defaults-not-assumed-fail.rs:8:19
2+
--> $DIR/defaults-not-assumed-fail.rs:6:19
33
|
44
LL | const B: u8 = Self::A + 1;
55
| ^^^^^^^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow
66

77
note: erroneous constant encountered
8-
--> $DIR/defaults-not-assumed-fail.rs:33:16
8+
--> $DIR/defaults-not-assumed-fail.rs:31:16
99
|
1010
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
1111
| ^^^^^^^^^^^^^
1212

1313
note: erroneous constant encountered
14-
--> $DIR/defaults-not-assumed-fail.rs:33:5
14+
--> $DIR/defaults-not-assumed-fail.rs:31:5
1515
|
1616
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
|
1919
= note: this note originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2020

2121
note: erroneous constant encountered
22-
--> $DIR/defaults-not-assumed-fail.rs:33:5
22+
--> $DIR/defaults-not-assumed-fail.rs:31:5
2323
|
2424
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)